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;
}
Re: Need Help With Tic Tac Toe
Have you tried using your debugger to check variable values and such during your program's execution?
Perhaps this will give you some insight as to why it doesn't work all the time.
Furthermore, when you say "it doesn't work sometimes", what exactly does that mean? What errors are you getting, or what is happening to make you think that?
These are things we need to know which you have left out.
More or less, when programs have somewhat complicated logic like this, it is very hard, if not impossible, for us to tell exactly where the problems lies.
Dust off your debugger, or use come cout statements in your code, and try to at least narrow the problem down to specific piece of code.
Then come back, and we can help you :D
Re: Need Help With Tic Tac Toe
Well I have been messing with my program all weekend and I have narrowed down the problems. When my checkwin() runs even if there is a win or lose it doesn't recognize it and change my enumerations accordingly. Also I am having troble with the turn system. I tired so do while and while loops but that didn't work so I moved to a for loop with some if statements. It loops through the turns pretty well. However, my for loop in main() doesn't seem to end even if iCounter = 9. I think the problem might be in my enumerations, but after looking at the code I don't see anything wrong. If anyone could help me I would really appreciate it.
Here is my updated code.
Code:
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()
{
cout << "\t\t\tWelcome to Tic Tac Toe" << endl << endl;
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;
if (iwhosfirst > 5)
WhosTurn = User;
else
WhosTurn = Computer;
}
/*------------------------------------------------------------------------------
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;
WhosTurn = Computer;
}
/*------------------------------------------------------------------------------
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] == ' ' && WhosTurn == Computer)
{Board[0][2] = cComputer; WhosTurn = User;}
else if (Board[0][0] == cComputer && Board[0][2] == cComputer && Board[0][1] == ' ' && WhosTurn == Computer)
{Board[0][1] = cComputer; WhosTurn = User;}
else if(Board[0][1] == cComputer && Board[0][2] == cComputer && Board[0][0] == ' ' && WhosTurn == Computer)
{Board[0][0] = cComputer; WhosTurn = User;}
//checks for wins in row 2
if (Board[1][0] == cComputer && Board[1][1] == cComputer && Board[1][2] == ' ' && WhosTurn == Computer)
{Board[1][2] = cComputer; WhosTurn = User;}
else if (Board[1][0] == cComputer && Board[1][2] == cComputer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1] = cComputer; WhosTurn = User;}
else if(Board[1][1] == cComputer && Board[1][2] == cComputer && Board[1][0] == ' ' && WhosTurn == Computer)
{Board[1][0] = cComputer; WhosTurn = User;}
//Checks for wins in Row 3
if (Board[2][0] == cComputer && Board[2][1] == cComputer && Board[2][2] == ' ' && WhosTurn == Computer)
{Board[2][2] = cComputer; WhosTurn = User;}
else if (Board[2][0] == cComputer && Board[2][2] == cComputer && Board[2][1] == ' ' && WhosTurn == Computer)
{Board[2][1] = cComputer; WhosTurn = User;}
else if(Board[2][1] == cComputer && Board[2][2] == cComputer && Board[2][0] == ' ' && WhosTurn == Computer)
{Board[2][0] = cComputer; WhosTurn = User;}
//Checks for wins in collum 1
if (Board[0][0] == cComputer && Board[1][0] == cComputer && Board[2][0] == ' ' && WhosTurn == Computer)
{Board[2][0] = cComputer; WhosTurn = User;}
else if (Board[2][0] == cComputer && Board[0][0] == cComputer && Board[1][0] == ' ' && WhosTurn == Computer)
{Board[1][0] = cComputer; WhosTurn = User;}
else if(Board[1][0] == cComputer && Board[2][0] == cComputer && Board[0][0] == ' ' && WhosTurn == Computer)
{Board[0][0] = cComputer; WhosTurn = User;}
//Checks for wins in Collum 2
if (Board[0][1] == cComputer && Board[1][1] == cComputer && Board[2][1] == ' ' && WhosTurn == Computer)
{Board[2][1] = cComputer; WhosTurn = User;}
else if (Board[2][1] == cComputer && Board[0][1] == cComputer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1] = cComputer; WhosTurn = User;}
else if(Board[1][1] == cComputer && Board[2][1] == cComputer && Board[0][1] == ' ' && WhosTurn == Computer)
{Board[0][1] = cComputer; WhosTurn = User;}
//Checks for wins in collum 3
if (Board[0][2] == cComputer && Board[1][2] == cComputer && Board[2][2] == ' ' && WhosTurn == Computer)
{Board[2][2] = cComputer; WhosTurn = User;}
else if (Board[2][2] == cComputer && Board[0][2] == cComputer && Board[1][2] == ' ' && WhosTurn == Computer)
{Board[1][2] = cComputer; WhosTurn = User;}
else if(Board[1][2] == cComputer && Board[2][2] == cComputer && Board[0][2] == ' ' && WhosTurn == Computer)
{Board[0][2] = cComputer; WhosTurn = User;}
//Checks for diagnol wins
if (Board[0][0] == cComputer && Board[1][1] == cComputer && Board[2][2] == ' ' && WhosTurn == Computer)
{Board[2][2] = cComputer; WhosTurn = User;}
else if (Board[1][1] == cComputer && Board[2][2] == cComputer && Board[0][0] == ' ' && WhosTurn == Computer)
{Board[0][0] = cComputer; WhosTurn = User;}
else if(Board[2][2] == cComputer && Board[0][0] == cComputer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1] = cComputer; WhosTurn = User;}
if (Board[2][0] == cComputer && Board[1][1] == cComputer && Board[0][2] == ' ' && WhosTurn == Computer)
{Board[0][2] = cComputer; WhosTurn = User;}
else if (Board[0][2] == cComputer && Board[2][0] == cComputer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1] = cComputer; WhosTurn = User;}
else if(Board[1][1] == cComputer && Board[0][2] == cComputer && Board[2][0] == ' ' && WhosTurn == Computer)
{Board[2][0] = cComputer; WhosTurn = User;}
//Checks for losses in row 1
if (Board[0][0] == cPlayer && Board[0][1] == cPlayer && Board[0][2] == ' ' && WhosTurn == Computer)
{Board[0][2] = cComputer; WhosTurn = User;}
else if (Board[0][0] == cPlayer && Board[0][2] == cPlayer && Board[0][1] == ' ' && WhosTurn == Computer)
{Board[0][1]= cComputer; WhosTurn = User;}
else if(Board[0][1] == cPlayer && Board[0][2] == cPlayer && Board[0][0] == ' ' && WhosTurn == Computer)
{Board[0][0] = cComputer; WhosTurn = User;}
//Checks for losses in row 2
if (Board[1][0] == cPlayer && Board[1][1] == cPlayer && Board[1][2] == ' ' && WhosTurn == Computer)
{Board[1][2] = cComputer; WhosTurn = User;}
else if (Board[1][0] == cPlayer && Board[1][2] == cPlayer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1]= cComputer; WhosTurn = User;}
else if(Board[1][1] == cPlayer && Board[1][2] == cPlayer && Board[1][0] == ' ' && WhosTurn == Computer)
{Board[1][0] = cComputer; WhosTurn = User;}
//Checks for losses in row 3
if (Board[2][0] == cPlayer && Board[2][1] == cPlayer && Board[2][2] == ' ' && WhosTurn == Computer)
{Board[2][2] = cComputer; WhosTurn = User;}
else if (Board[2][0] == cPlayer && Board[2][2] == cPlayer && Board[2][1] == ' ' && WhosTurn == Computer)
{Board[2][1]= cComputer; WhosTurn = User;}
else if(Board[2][1] == cPlayer && Board[2][2] == cPlayer && Board[2][0] == ' ' && WhosTurn == Computer)
{Board[2][0] = cComputer; WhosTurn = User;}
//Checks for losses in Collum 1
if (Board[0][0] == cPlayer && Board[1][0] == cPlayer && Board[2][0] == ' ' && WhosTurn == Computer)
{Board[2][0] = cComputer; WhosTurn = User;}
else if (Board[2][0] == cPlayer && Board[0][0] == cPlayer && Board[1][0] == ' ' && WhosTurn == Computer)
{Board[1][0]= cComputer; WhosTurn = User;}
else if(Board[1][0] == cPlayer && Board[2][0] == cPlayer && Board[0][0] == ' ' && WhosTurn == Computer)
{Board[0][0] = cComputer; WhosTurn = User;}
//Checks for losses in Collum 2
if (Board[0][1] == cPlayer && Board[1][1] == cPlayer && Board[2][1] == ' ' && WhosTurn == Computer)
{Board[2][1] = cComputer; WhosTurn = User;}
else if (Board[2][1] == cPlayer && Board[0][1] == cPlayer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1]= cComputer; WhosTurn = User;}
else if(Board[1][1] == cPlayer && Board[2][1] == cPlayer && Board[0][1] == ' ' && WhosTurn == Computer)
{Board[0][1] = cComputer; WhosTurn = User;}
//Checks for losses in collum 3
if (Board[0][2] == cPlayer && Board[1][2] == cPlayer && Board[2][2] == ' ' && WhosTurn == Computer)
{Board[2][2] = cComputer; WhosTurn = User;}
else if (Board[2][2] == cPlayer && Board[0][2] == cPlayer && Board[1][2] == ' ' && WhosTurn == Computer)
{Board[1][2]= cComputer; WhosTurn = User;}
else if(Board[1][2] == cPlayer && Board[2][2] == cPlayer && Board[0][2] == ' ' && WhosTurn == Computer)
{Board[0][2] = cComputer; WhosTurn = User;}
//Checks for losses diagnoly
if (Board[0][0] == cPlayer && Board[1][1] == cPlayer && Board[2][2] == ' ' && WhosTurn == Computer)
{Board[2][2] = cComputer; WhosTurn = User;}
else if (Board[1][1] == cPlayer && Board[2][2] == cPlayer && Board[0][0] == ' ' && WhosTurn == Computer)
{Board[0][0]= cComputer; WhosTurn = User;}
else if(Board[2][2] == cPlayer && Board[0][0] == cPlayer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1] = cComputer; WhosTurn = User;}
if (Board[2][0] == cPlayer && Board[1][1] == cPlayer && Board[0][2] == ' ' && WhosTurn == Computer)
{Board[0][2] = cComputer; WhosTurn = User;}
else if (Board[0][2] == cPlayer && Board[2][0] == cPlayer && Board[1][1] == ' ' && WhosTurn == Computer)
{Board[1][1]= cComputer; WhosTurn = User;}
else if(Board[1][1] == cPlayer && Board[0][2] == cPlayer && Board[2][0] == ' ' && WhosTurn == Computer)
{Board[2][0] = cComputer; WhosTurn = User;}
if (WhosTurn == Computer)
do{
iRowChoice = rand() % 3;
iColChoice = rand() % 3;
if (Board[iRowChoice][iColChoice] == ' ')
Board[iRowChoice][iColChoice] = cComputer;
WhosTurn = User;
}while(Board[iRowChoice][iColChoice] != cComputer);
}
/*------------------------------------------------------------------------------
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 == Computer)
WhosTurn = User;
else
WhosTurn = Computer;
if (WhosTurn == User)
{
cout << "It is " << cPlayer << "'s turn.";
system("PAUSE");
}
else
{
cout << "It is " << cComputer << "'s turn." << endl;
system("PAUSE");
}
}
/*------------------------------------------------------------------------------
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;
if (Board[1][0] == cPlayer && Board[1][1] == cPlayer && Board[1][2] == cPlayer)
Final = Win;
if (Board[2][0] == cPlayer && Board[2][1] == cPlayer && Board[2][2] == cPlayer)
Final = Win;
//Checks collums for wins
if (Board[0][0] == cPlayer && Board[1][0] == cPlayer && Board[2][0] == cPlayer)
Final = Win;
if (Board[0][1] == cPlayer && Board[1][1] == cPlayer && Board[2][1] == cPlayer)
Final = Win;
if (Board[0][2] == cPlayer && Board[1][2] == cPlayer && Board[2][2] == cPlayer)
Final = Win;
//Checks Diagnols for wins
if (Board[0][0] == cPlayer && Board[1][1] == cPlayer && Board[2][2] == cPlayer)
Final = Win;
if (Board[2][2] == cPlayer && Board[1][1] == cPlayer && Board[2][0] == cPlayer)
Final = Win;
//Checks rows for losses
if (Board[0][0] == cComputer && Board[0][1] == cComputer && Board[0][2] == cComputer)
Final = Lose;
if (Board[1][0] == cComputer && Board[1][1] == cComputer && Board[1][2] == cComputer)
Final = Lose;
if (Board[2][0] == cComputer && Board[2][1] == cComputer && Board[2][2] == cComputer)
Final = Lose;
//Checks collums for losses
if (Board[0][0] == cComputer && Board[1][0] == cComputer && Board[2][0] == cComputer)
Final = Lose;
if (Board[0][1] == cComputer && Board[1][1] == cComputer && Board[2][1] == cComputer)
Final = Lose;
if (Board[0][2] == cComputer && Board[1][2] == cComputer && Board[2][2] == cComputer)
Final = Lose;
//Checks Diagnols for losses
if (Board[0][0] == cComputer && Board[1][1] == cComputer && Board[2][2] == cComputer)
Final = Lose;
if (Board[2][2] == cComputer && Board[1][1] == cComputer && Board[2][0] == cComputer)
Final = Lose;
if (Final == Lose || Final == Win)
WhosTurn == End;
}
/*------------------------------------------------------------------------------
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");
}
else if (Final == Lose)
{
cout << endl << "Sorry you have been defeted and I, the Computer have Won!!!\n\n";
system("PAUSE");
}
else if (Final == Win)
{
cout << endl << "Congragulations Youve beat me!!";
system("PAUSE");
}
}
int main()
{
srand(time(NULL));
TTT Tic;
int iCounter = 0;
char cYorN;
do{
Tic.Intro();
Tic.ResetBoard();
Tic.DrawBoard();
for (int iCounter = 0; iCounter < 9; iCounter++)//this loop never ends???
{
if (WhosTurn == User)
{
Tic.PlayerTurn();
Tic.DrawBoard();
Tic.CheckWin();//even though this is called it never seems to detect any wins or losses when there are.
}
if (WhosTurn == Computer)
{
Tic.CompTurn();
Tic.DrawBoard();
Tic.CheckWin();
}
if (WhosTurn == End)
break;
}
Tic.EndGame();
cout << endl << "Would you Like to Play again? (Y/N): ";
cin >> cYorN;
}while(cYorN != 'N' || cYorN != 'n');
return 0;
}
Re: Need Help With Tic Tac Toe
When you say AI, that normally means that the computer would store a losing sequence and would avoid it next time it has a random decision to make. In addition, if the computer won, it would store the decision it made at the last point it made a random one and would ensure it always played that way unless it subsequently led to a loss. (If it leads to a draw next time though it would probably become a drawing sequence).
Writing an AI model like that is fairly tricky. It doesn't seem like the sort of project that could be written in one source file using just one class.
I remember in my Masters Degree learning a language called Prolog which was actually the ideal language for this sort of thing.