|
-
December 15th, 2006, 07:39 PM
#1
Need Help With Tic Tac Toe
I have written Tic Tac Toe and it compiles correctly, however the AI for the game doesn't always work and I have no idea why? It works sometimes, but not always. Anyways here is my code if you could look at it and help me find the problem I would really appreciate it. Also if there are any suggestions that you have I will also be happy to hear them. You can ignore the first class that is written. My teacher wrote that for us to inhearit from and I only use the ResetBoard() function and the protected data. Thanks
Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <ctime>
using namespace std;
const int MAX = 3;
enum NowMoveing {User, Computer};
enum WinorLose {Win, Lose, Tie};
NowMoveing WhosTurn;
WinorLose Final = Tie;
class Game {
protected:
char Board[MAX][MAX]; //Store the actually board for the game
int iTotalMoves; //How many moves have happened in the game
public:
Game() : iTotalMoves(0)
{ ResetBoard(); }
void DrawRow(char cRow[]) {
//This member function will print out one row of the 3 row tic-tac-toe board
}
void DrawGame() {
//This member function will print the entire tic-tac-toe board. This member function calls the
//DrawRow() function which will draw the rows of the board
}
void ResetBoard() {
//This member function will reset the board to all blank to start the tic-tac-toe game
for(int iRow = 0; iRow < MAX; iRow++) {
for(int iCol = 0; iCol < MAX; iCol++) {
Board[iRow][iCol] = ' ';
}
}
}
void UserInput(char cPlayerSymbol) {
//This member function will print to the user who's turn it is and ask the player for a column and row coordiante to put
//their symbol on the tic-tac-toe board. This member function will also check to see if the user's position was
//a valid place on the board to go to. In other words it checks to see if it is a space on the board and also makes
//sure to check if another X or O is already in that position.
}
bool CheckMove(int iRow, int iColumn) {
//This member function checks to see if the move was a valid move or not. This is called from UserInput.
}
void ComputerMove(char cPlayer, char cComputer) {
//This is the code for the computers strategy on how it will make its next move. Ideally you will check all
//positions on the board and make sure that you block any human player win attempts. Then if there are no
//human win attempts, make the best move possible.
}
bool CheckWin(char cPlayer) {
//This member function will check all winning combinations on the board to see if anyone won or if it is a
//"Cats" game (A tie). Also remember that there is a member variable that keeps track of how many moves
//have already taken place. There are only 9 moves in the entire game so once the moves are more than 9
//the game is over (Cats game).
}
};
class TTT : public Game
{
private:
char cPlayer, cComputer;
int iwhosfirst, iRowChoice, iColChoice;
public:
TTT() : cPlayer(' '), cComputer(' '), iwhosfirst(0), iRowChoice(0), iColChoice(0)
{ }
void Intro();
void DrawBoard();
void PlayerTurn();
void CompTurn();
void CheckWin();
void EndGame();
void CheckMove();
void SwitchTurn();
};
/*------------------------------------------------------------------------------
Function Name: Intro
Purpose: Welcome Screen and options to view rules and start game
Return Type: Void
------------------------------------------------------------------------------*/
void TTT::Intro()
{
int ioption;
cout << "\t\t\tWelcome to Tic Tac Toe" << endl << endl
<< "Please Choose one of the fallowing" << endl
<< "1. Rules" << endl << "2. Play Game" << endl << "3. Exit" << endl << "Choice: ";
cin >> ioption;
if (ioption == 1)
{
system("CLS");
cout << "\t\t\tRules\n\n"
<< "Choose a row and a collum to place three X's or O's in a row.\n";
system("PAUSE");
system("CLS");
Intro();
}
else if (ioption == 2)
{
system("CLS");
cout << "Choose X's or O's: ";
cin >> cPlayer;
if (cPlayer == 'X' || cPlayer == 'x')
{cComputer = 'O'; cPlayer = 'X';}
else
{cComputer = 'X'; cPlayer = 'O';}
iwhosfirst = rand() % 11;
DrawBoard();
if (iwhosfirst > 5)
{
WhosTurn = User;
PlayerTurn();
}
else
{
WhosTurn = Computer;
CompTurn();
}
}
else if (ioption == 3)
system("EXIT");
}
/*------------------------------------------------------------------------------
Function Name: DrawBoard
Purpose: Draws the board on screen and updates the board as the game progresses.
Return Type: void
------------------------------------------------------------------------------*/
void TTT::DrawBoard()
{
system("CLS");
cout << " | | " << endl
<< " " << Board[0][0] << " | " << Board[0][1] << " | " << Board[0][2] << " " << endl
<< " ___|___|___" << endl
<< " | | " << endl
<< " " << Board[1][0] << " | " << Board[1][1] << " | " << Board[1][2] << " " << endl
<< " ___|___|___" << endl
<< " | | " << endl
<< " " << Board[2][0] << " | " << Board[2][1] << " | " << Board[2][2] << " " << endl
<< " | | " << endl;
}
/*------------------------------------------------------------------------------
Function Name: PlayerTurn
Purpose: Allows player to choose a place to put its symbol
Return Type: void
------------------------------------------------------------------------------*/
void TTT::PlayerTurn()
{
do{
cout << "Please Enter a row number(1-3): ";
cin >> iRowChoice;
iRowChoice = iRowChoice - 1;
cout << "Please Enter a Collum Number(1-3): ";
cin >> iColChoice;
iColChoice = iColChoice - 1;
if (iRowChoice > 2 || iRowChoice < 0 || iColChoice > 2 || iColChoice < 0)
cout << "You have entered an invalid move." << endl;
}while(iRowChoice > 2 || iRowChoice < 0 || iColChoice > 2 || iColChoice < 0);
CheckMove();
Board[iRowChoice][iColChoice] = cPlayer;
DrawBoard();
CheckWin();
WhosTurn = Computer;
SwitchTurn();
}
/*------------------------------------------------------------------------------
Function Name: CompTurn
Purpose: Checks for any wining or loseing moves or makes a random move.
Return Type: void
------------------------------------------------------------------------------*/
void TTT::CompTurn()
{
//Check for Possible wins in row 1
if (Board[0][0] == cComputer && Board[0][1] == cComputer && Board[0][2] == ' ')
{Board[0][2] = cComputer;}
else if (Board[0][0] == cComputer && Board[0][2] == cComputer && Board[0][1] == ' ')
{Board[0][1] = cComputer;}
else if(Board[0][1] == cComputer && Board[0][2] == cComputer && Board[0][0] == ' ')
{Board[0][0] = cComputer;}
//checks for wins in row 2
if (Board[1][0] == cComputer && Board[1][1] == cComputer && Board[1][2] == ' ')
{Board[1][2] = cComputer;}
else if (Board[1][0] == cComputer && Board[1][2] == cComputer && Board[1][1] == ' ')
{Board[1][1] = cComputer;}
else if(Board[1][1] == cComputer && Board[1][2] == cComputer && Board[1][0] == ' ')
{Board[1][0] = cComputer;}
//Checks for wins in Row 3
if (Board[2][0] == cComputer && Board[2][1] == cComputer && Board[2][2] == ' ')
{Board[2][2] = cComputer;}
else if (Board[2][0] == cComputer && Board[2][2] == cComputer && Board[2][1] == ' ')
{Board[2][1] = cComputer;}
else if(Board[2][1] == cComputer && Board[2][2] == cComputer && Board[2][0] == ' ')
{Board[2][0] = cComputer;}
//Checks for wins in collum 1
if (Board[0][0] == cComputer && Board[1][0] == cComputer && Board[2][0] == ' ')
{Board[2][0] = cComputer;}
else if (Board[2][0] == cComputer && Board[0][0] == cComputer && Board[1][0] == ' ')
{Board[1][0] = cComputer;}
else if(Board[1][0] == cComputer && Board[2][0] == cComputer && Board[0][0] == ' ')
{Board[0][0] = cComputer;}
//Checks for wins in Collum 2
if (Board[0][1] == cComputer && Board[1][1] == cComputer && Board[2][1] == ' ')
{Board[2][1] = cComputer;}
else if (Board[2][1] == cComputer && Board[0][1] == cComputer && Board[1][1] == ' ')
{Board[1][1] = cComputer;}
else if(Board[1][1] == cComputer && Board[2][1] == cComputer && Board[0][1] == ' ')
{Board[0][1] = cComputer;}
//Checks for wins in collum 3
if (Board[0][2] == cComputer && Board[1][2] == cComputer && Board[2][2] == ' ')
{Board[2][2] = cComputer;}
else if (Board[2][2] == cComputer && Board[0][2] == cComputer && Board[1][2] == ' ')
{Board[1][2] = cComputer;}
else if(Board[1][2] == cComputer && Board[2][2] == cComputer && Board[0][2] == ' ')
{Board[0][2] = cComputer;}
//Checks for diagnol wins
if (Board[0][0] == cComputer && Board[1][1] == cComputer && Board[2][2] == ' ')
{Board[2][2] = cComputer;}
else if (Board[1][1] == cComputer && Board[2][2] == cComputer && Board[0][0] == ' ')
{Board[0][0] = cComputer;}
else if(Board[2][2] == cComputer && Board[0][0] == cComputer && Board[1][1] == ' ')
{Board[1][1] = cComputer;}
if (Board[2][0] == cComputer && Board[1][1] == cComputer && Board[0][2] == ' ')
{Board[0][2] = cComputer;}
else if (Board[0][2] == cComputer && Board[2][0] == cComputer && Board[1][1] == ' ')
{Board[1][1] = cComputer;}
else if(Board[1][1] == cComputer && Board[0][2] == cComputer && Board[2][0] == ' ')
{Board[2][0] = cComputer;}
//Checks for losses in row 1
if (Board[0][0] == cPlayer && Board[0][1] == cPlayer && Board[0][2] == ' ')
{Board[0][2] = cComputer;}
else if (Board[0][0] == cPlayer && Board[0][2] == cPlayer && Board[0][1] == ' ')
{Board[0][1]= cComputer;}
else if(Board[0][1] == cPlayer && Board[0][2] == cPlayer && Board[0][0] == ' ')
{Board[0][0] = cComputer;}
//Checks for losses in row 2
if (Board[1][0] == cPlayer && Board[1][1] == cPlayer && Board[1][2] == ' ')
{Board[1][2] = cComputer;}
else if (Board[1][0] == cPlayer && Board[1][2] == cPlayer && Board[1][1] == ' ')
{Board[1][1]= cComputer;}
else if(Board[1][1] == cPlayer && Board[1][2] == cPlayer && Board[1][0] == ' ')
{Board[1][0] = cComputer;}
//Checks for losses in row 3
if (Board[2][0] == cPlayer && Board[2][1] == cPlayer && Board[2][2] == ' ')
{Board[2][2] = cComputer;}
else if (Board[2][0] == cPlayer && Board[2][2] == cPlayer && Board[2][1] == ' ')
{Board[2][1]= cComputer;}
else if(Board[2][1] == cPlayer && Board[2][2] == cPlayer && Board[2][0] == ' ')
{Board[2][0] = cComputer;}
//Checks for losses in Collum 1
if (Board[0][0] == cPlayer && Board[1][0] == cPlayer && Board[2][0] == ' ')
{Board[2][0] = cComputer;}
else if (Board[2][0] == cPlayer && Board[0][0] == cPlayer && Board[1][0] == ' ')
{Board[1][0]= cComputer;}
else if(Board[1][0] == cPlayer && Board[2][0] == cPlayer && Board[0][0] == ' ')
{Board[0][0] = cComputer;}
//Checks for losses in Collum 2
if (Board[0][1] == cPlayer && Board[1][1] == cPlayer && Board[2][1] == ' ')
{Board[2][1] = cComputer;}
else if (Board[2][1] == cPlayer && Board[0][1] == cPlayer && Board[1][1] == ' ')
{Board[1][1]= cComputer;}
else if(Board[1][1] == cPlayer && Board[2][1] == cPlayer && Board[0][1] == ' ')
{Board[0][1] = cComputer;}
//Checks for losses in collum 3
if (Board[0][2] == cPlayer && Board[1][2] == cPlayer && Board[2][2] == ' ')
{Board[2][2] = cComputer;}
else if (Board[2][2] == cPlayer && Board[0][2] == cPlayer && Board[1][2] == ' ')
{Board[1][2]= cComputer;}
else if(Board[1][2] == cPlayer && Board[2][2] == cPlayer && Board[0][2] == ' ')
{Board[0][2] = cComputer;}
//Checks for losses diagnoly
if (Board[0][0] == cPlayer && Board[1][1] == cPlayer && Board[2][2] == ' ')
{Board[2][2] = cComputer;}
else if (Board[1][1] == cPlayer && Board[2][2] == cPlayer && Board[0][0] == ' ')
{Board[0][0]= cComputer;}
else if(Board[2][2] == cPlayer && Board[0][0] == cPlayer && Board[1][1] == ' ')
{Board[1][1] = cComputer;}
if (Board[2][0] == cPlayer && Board[1][1] == cPlayer && Board[0][2] == ' ')
{Board[0][2] = cComputer;}
else if (Board[0][2] == cPlayer && Board[2][0] == cPlayer && Board[1][1] == ' ')
{Board[1][1]= cComputer;}
else if(Board[1][1] == cPlayer && Board[0][2] == cPlayer && Board[2][0] == ' ')
{Board[2][0] = cComputer;}
do{
iRowChoice = rand() % MAX;
iColChoice = rand() % MAX;
if (Board[iRowChoice][iColChoice] == ' ')
Board[iRowChoice][iColChoice] = cComputer;
}while (Board[iRowChoice][iColChoice] != cComputer);
CheckWin();
WhosTurn = User;
SwitchTurn();
}
/*------------------------------------------------------------------------------
Function Name: CheckMove
Purpose: Checks the move the player made to see if its valid.
Return Type: void
------------------------------------------------------------------------------*/
void TTT::CheckMove()
{
do{
if (Board[iRowChoice][iColChoice] == cPlayer || Board[iRowChoice][iColChoice] == cComputer)
{
cout << "The space was already taken or does not exsist. Please choose again.\n\n";
cout << "Row number: "; cin >> iRowChoice;
iRowChoice = iRowChoice - 1;
cout << endl << "Collum number: "; cin >> iColChoice;
iColChoice = iColChoice - 1;
}
}while(iRowChoice > 2 || iRowChoice < 0 || iColChoice > 2 || iColChoice < 0);
}
/*------------------------------------------------------------------------------
Function Name: SwitchTurn
Purpose: Decides whos turn it is and switches the correct function.
Return Type: void
------------------------------------------------------------------------------*/
void TTT::SwitchTurn()
{
if (WhosTurn == User && iTotalMoves < 9)
{
cout << "It is " << cPlayer << "'s turn.";
PlayerTurn();
}
else if (WhosTurn == Computer && iTotalMoves < 9)
{
cout << "It is " << cComputer << "'s turn.";
CompTurn();
}
else if (iTotalMoves = 9)
{
CheckWin();
}
}
/*------------------------------------------------------------------------------
Function Name: CheckWin
Purpose: Checks to see if there is a winner. If not then makes the game a tie.
Return Type: void
------------------------------------------------------------------------------*/
void TTT::CheckWin()
{
//Checks rows for wins
if (Board[0][0] == cPlayer && Board[0][1] == cPlayer && Board[0][2] == cPlayer)
{Final = Win; EndGame();}
if (Board[1][0] == cPlayer && Board[1][1] == cPlayer && Board[1][2] == cPlayer)
{Final = Win; EndGame();}
if (Board[2][0] == cPlayer && Board[2][1] == cPlayer && Board[2][2] == cPlayer)
{Final = Win; EndGame();}
//Checks collums for wins
if (Board[0][0] == cPlayer && Board[1][0] == cPlayer && Board[2][0] == cPlayer)
{Final = Win; EndGame();}
if (Board[0][1] == cPlayer && Board[1][1] == cPlayer && Board[2][1] == cPlayer)
{Final = Win; EndGame();}
if (Board[0][2] == cPlayer && Board[1][2] == cPlayer && Board[2][2] == cPlayer)
{Final = Win; EndGame();}
//Checks Diagnols for wins
if (Board[0][0] == cPlayer && Board[1][1] == cPlayer && Board[2][2] == cPlayer)
{Final = Win; EndGame();}
if (Board[2][2] == cPlayer && Board[1][1] == cPlayer && Board[2][0] == cPlayer)
{Final = Win; EndGame();}
//Checks rows for losses
if (Board[0][0] == cComputer && Board[0][1] == cComputer && Board[0][2] == cComputer)
{Final = Lose; EndGame();}
if (Board[1][0] == cComputer && Board[1][1] == cComputer && Board[1][2] == cComputer)
{Final = Lose; EndGame();}
if (Board[2][0] == cComputer && Board[2][1] == cComputer && Board[2][2] == cComputer)
{Final = Lose; EndGame();}
//Checks collums for losses
if (Board[0][0] == cComputer && Board[1][0] == cComputer && Board[2][0] == cComputer)
{Final = Lose; EndGame();}
if (Board[0][1] == cComputer && Board[1][1] == cComputer && Board[2][1] == cComputer)
{Final = Lose; EndGame();}
if (Board[0][2] == cComputer && Board[1][2] == cComputer && Board[2][2] == cComputer)
{Final = Lose; EndGame();}
//Checks Diagnols for losses
if (Board[0][0] == cComputer && Board[1][1] == cComputer && Board[2][2] == cComputer)
{Final = Lose; EndGame();}
if (Board[2][2] == cComputer && Board[1][1] == cComputer && Board[2][0] == cComputer)
{Final = Lose; EndGame();}
if (Final == Tie && iTotalMoves == 9)
EndGame();
}
/*------------------------------------------------------------------------------
Function Name: EndGame
Purpose: Decides if the game is won lost or a tie
Return Type: void
------------------------------------------------------------------------------*/
void TTT::EndGame()
{
if (Final == Tie)
{
cout << endl << "Cats Game no one wins.\n\n";
system("PAUSE");
ResetBoard();
Intro();
}
else if (Final == Lose)
{
cout << endl << "Sorry you have been defeted and I, the Computer have Won!!!\n\n";
system("PAUSE");
ResetBoard();
Intro();
}
else if (Final == Win)
{
cout << endl << "Congragulations Youve beat me!!";
system("PAUSE");
ResetBoard();
Intro();
}
}
int main()
{
srand(time(NULL));
TTT Tic;
Tic.Intro();
system("PAUSE");
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|