Need help with a tic tac toe program

This is a discussion on Need help with a tic tac toe program within the C++ Programming forums, part of the General Programming Boards category; For this program I have to inherit from the base class "Game". The class "Game" is given and cannot be ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Need help with a tic tac toe program

    For this program I have to inherit from the base class "Game". The class "Game" is given and cannot be modified. Here is what I have so far:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    
    const int MAX = 4;
    
    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 Game2 : public Game {					//derived class
    	  public:
    			 int iColumn;
    			 int iRow;
    			 char cRow[MAX];
    			 char cCol[MAX];
    			 char cPlayerSymbol;
    			 char cComputerSymbol;
    			 char x;
    			 char cPlayer;
    			 char cComputer;
    			 char c_player;
    			 char cAns;
    			 char move;
    			 
    			 void Turns() {
    				iTotalMoves++;
    				if(CheckWin(cPlayer)==false && iTotalMoves>9)
    				{
    					cout << "Cats Game\n";
    				}
    			 }
    			 void DrawRow(char cRow[]) {
    				 cout<<cRow[1]<<"  |  "<<cRow[2]<<"|"<<cRow[3];
    			 }
    
    			 void DrawGame() {
    				 cout << "\n   1   2   3\n\n";
    				 cout << "1 ";
    				 DrawRow(Board[1]);
    				 cout << "\n  ---|---|---\n";
    				 cout << "2 ";
    				 DrawRow(Board[2]);
    				 cout << "\n  ---|---|---\n";
    				 cout << "3 ";
    				 DrawRow(Board[3]);
    				 cout << "\n     \n";
    			 }
    
    			 void UserInput(char cPlayerSymbol) {
    				 cout << cPlayerSymbol <<"\n";
    				 cout << "Row\n";
    				 cin >> iRow;
    				 cout << "Column\n";
    				 cin >> iColumn;
    				 CheckMove(iRow, iColumn);
    				 
    				 if (CheckMove(iRow, iColumn)==false) {
    					 cout << "that is not within the range (1,3)\n";
    					 UserInput(cPlayerSymbol);
    				 }
    				 else {
    					 Board[iRow][iColumn]=cPlayerSymbol;
    				 }
    			 }
    
    			 bool CheckMove(int iRow, int iColumn) {
    				 if (iRow>3|| iRow<1||iColumn>3||iColumn<1){
    					 return false;
    				 }
    				 if (Board[iRow][iColumn]==' ') {
    					 return true;
    				 }
    				 else {
    					 return false;
    				 }
    			 }
    
    			 void ComputerMove(char cPlayer, char cComputer) {
    				 if (Board[1][1]==' '&&Board[1][2]==cComputerSymbol&&Board[1][3]==cComputerSymbol) {
    					 Board[1][2]=cComputerSymbol;
    				 }
    				 else if (Board[2][1]==' '&&Board[2][2]==cComputerSymbol&&Board[2][3]==cComputerSymbol) {
    					 Board[2][2]=cComputerSymbol;
    				 }
    				 else if (Board[3][1]==' '&&Board[3][2]==cComputerSymbol&&Board[3][3]==cComputerSymbol) {
    					 Board[3][2]=cComputerSymbol;
    				 }
    			 }
    
    			 bool CheckWin(char cPlayer) {
    				 for (int i = 1; i < 4; i++) {
    					 if (Board[i][1]==cPlayerSymbol&&Board[i][2]==cPlayerSymbol&&Board[i][3]==cPlayerSymbol) {
    						 cout << cPlayerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 for (int j = 1; j< 4; j++) {
    					 if (Board[1][j]==cPlayerSymbol&&Board[2][j]==cPlayerSymbol&&Board[3][j]==cPlayerSymbol) {
    						 cout << cPlayerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 if (Board[1][1]==cPlayerSymbol&&Board[2][2]==cPlayerSymbol&&Board[3][3]==cPlayerSymbol) {
    					 cout << cPlayerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 if (Board[1][3]==cPlayerSymbol&&Board[2][2]==cPlayerSymbol&&Board[3][1]==cPlayerSymbol) {
    					 cout << cPlayerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 for (int i = 1; i < 4; i++) {
    					 if (Board[i][1]==cComputerSymbol&&Board[i][2]==cComputerSymbol&&Board[i][3]==cComputerSymbol) {
    						 cout << cComputerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 for (int j = 1; j < 4; j++) {
    					 if (Board[1][j]==cComputerSymbol&&Board[2][j]==cComputerSymbol&&Board[3][j]==cComputerSymbol) {
    						 cout << cComputerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 if (Board[1][1]==cComputerSymbol&&Board[2][2]==cComputerSymbol&&Board[3][3]==cComputerSymbol) {
    					 cout << cComputerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 if (Board[1][3]==cComputerSymbol&&Board[2][2]==cComputerSymbol&&Board[3][1]==cComputerSymbol) {
    					 cout << cComputerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 return false;
    			 }
    
    			 void PlayGame() {
    				 do {
    					 cout<<"	Do you want to be X or O?\n";
    					 cin>>cPlayerSymbol;
    					 if (cPlayerSymbol=='O') {
    						 cComputerSymbol='X';
    					 }
    					 else {
    						 cComputerSymbol='O';
    					 }
    					 DrawGame();
    					 UserInput(cPlayerSymbol);
    					 system("CLS");
    					 DrawGame();
    				 }
    				 while(cAns=='y' || cAns=='Y');
    			 }
    };
    
    int main() {
    	cout<<"Let's play a game of Tic-Tac-Toe\n\n";
    	Game2 TicTacToe;
    	TicTacToe.PlayGame();
    	system("pause");
    	return 0;
    }

    *I need use a pointer to instantiate my tic-tac-toe object. (Use the new and -> operator.) How should I use this?

    *I also need help with my AI psuedocode for the computer player, and I don't know how to finish this.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    Game2* myGame = new Game2();
    is how you use instantiate.
    Now you can use the members of myGame as
    Code:
    myGame->var;
    myGame->fun();
    For the A.I. you can go in a lot of ways. One is make the computer choose the best moves. For tic-tac toe this shouldn't take too long.
    Or make the computer put random, but block when you try to put 3 in a row. Or select randomly on places where he can make 3 in a row.
    The later could be done as
    1) Check if he can put a pawn and win
    2) Check if opponent can win, thus search if there are 2 pawns of his opponent that are next to each other (i.e. x1 == x2 +-1, y1 == y2+-1). If they are check if they can complete 3 in a row. If they can, put a pawn there.
    2) If not, then choose randomly a pawn already placed. Choose a square around that pawn in order to be able to make 3 in a row.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    Here is an updated version:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    
    const int MAX = 4;
    
    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 Game2 : public Game {					//derived class
    	  public:
    			 int iColumn;
    			 int iRow;
    			 char cRow[MAX];
    			 char cCol[MAX];
    			 char cPlayerSymbol;
    			 char cComputerSymbol;
    			 char x;
    			 char cPlayer;
    			 char cComputer;
    			 char c_player;
    			 char cAns;
    			 char move;
    			 
    			 
    			 
    			 void Turns() {
    				iTotalMoves++;
    				if(CheckWin(cPlayer)==false && iTotalMoves>9)
    				{
    					cout << "Cats Game\n";
    				}
    			 }
    			 void DrawRow(char cRow[]) {
    				 cout<<cRow[1]<<"  |  "<<cRow[2]<<"|"<<cRow[3];
    			 }
    
    			 void DrawGame() {
    				 cout << "\n   1   2   3\n\n";
    				 cout << "1 ";
    				 DrawRow(Board[1]);
    				 cout << "\n  ---|---|---\n";
    				 cout << "2 ";
    				 DrawRow(Board[2]);
    				 cout << "\n  ---|---|---\n";
    				 cout << "3 ";
    				 DrawRow(Board[3]);
    				 cout << "\n     \n";
    			 }
    
    			 void UserInput(char cPlayerSymbol) {
    				 cout << "It is " << cPlayerSymbol <<"'s turn.\n";
    				 cout << "Row\n";
    				 cin >> iRow;
    				 cout << "Column\n";
    				 cin >> iColumn;
    				 CheckMove(iRow, iColumn);
    				 
    				 if (CheckMove(iRow, iColumn)==false) {
    					 cout << "That is not within the range (1,3)\n\n";
    					 UserInput(cPlayerSymbol);
    				 }
    				 else {
    					 Board[iRow][iColumn]=cPlayerSymbol;
    				 }
    			 }
    
    			 bool CheckMove(int iRow, int iColumn) {
    				 if (iRow>3|| iRow<1||iColumn>3||iColumn<1){
    					 return false;
    				 }
    				 if (Board[iRow][iColumn]==' ') {
    					 return true;
    				 }
    				 else {
    					 return false;
    				 }
    			 }
    
    			 void ComputerMove(char cPlayer, char cComputer) {
    				 if (Board[1][1]==' '&&Board[1][2]==cComputerSymbol&&Board[1][3]==cComputerSymbol) {
    					 Board[1][2]=cComputerSymbol;
    				 }
    				 else if (Board[2][1]==' '&&Board[2][2]==cComputerSymbol&&Board[2][3]==cComputerSymbol) {
    					 Board[2][2]=cComputerSymbol;
    				 }
    				 else if (Board[3][1]==' '&&Board[3][2]==cComputerSymbol&&Board[3][3]==cComputerSymbol) {
    					 Board[3][2]=cComputerSymbol;
    				 }
    				 
    			 }
    
    			 bool CheckWin(char cPlayer) {
    				 for (int i = 1; i < 4; i++) {
    					 if (Board[i][1]==cPlayerSymbol&&Board[i][2]==cPlayerSymbol&&Board[i][3]==cPlayerSymbol) {
    						 cout << cPlayerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 for (int j = 1; j< 4; j++) {
    					 if (Board[1][j]==cPlayerSymbol&&Board[2][j]==cPlayerSymbol&&Board[3][j]==cPlayerSymbol) {
    						 cout << cPlayerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 if (Board[1][1]==cPlayerSymbol&&Board[2][2]==cPlayerSymbol&&Board[3][3]==cPlayerSymbol) {
    					 cout << cPlayerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 if (Board[1][3]==cPlayerSymbol&&Board[2][2]==cPlayerSymbol&&Board[3][1]==cPlayerSymbol) {
    					 cout << cPlayerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 for (int i = 1; i < 4; i++) {
    					 if (Board[i][1]==cComputerSymbol&&Board[i][2]==cComputerSymbol&&Board[i][3]==cComputerSymbol) {
    						 cout << cComputerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 for (int j = 1; j < 4; j++) {
    					 if (Board[1][j]==cComputerSymbol&&Board[2][j]==cComputerSymbol&&Board[3][j]==cComputerSymbol) {
    						 cout << cComputerSymbol << " has won!\n";
    						 return true;
    						 break;
    					 }
    				 }
    				 if (Board[1][1]==cComputerSymbol&&Board[2][2]==cComputerSymbol&&Board[3][3]==cComputerSymbol) {
    					 cout << cComputerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 if (Board[1][3]==cComputerSymbol&&Board[2][2]==cComputerSymbol&&Board[3][1]==cComputerSymbol) {
    					 cout << cComputerSymbol << " has won!\n";
    					 return true;
    					 system("pause");
    					 exit(0);
    				 }
    				 return false;
    			 }
    				 	
    	
    
    
    			 void StartGame() {
    				 do {
    					 cout<<"	Do you want to be X or O?\n";
    					 cin>>cPlayerSymbol;
    					 if (cPlayerSymbol=='O') {
    						 cComputerSymbol='X';
    					 }
    					 else {
    						 cComputerSymbol='O';
    					 }
    				 }
    				 while(cPlayerSymbol!='x'&&cPlayerSymbol!='o');
    				 PlayGame();
    			 }
    
    			 void PlayGame() {
    				 if (cPlayerSymbol=='x' || cPlayerSymbol == 'X') {
    					 do {
    						 DrawGame();
    						 UserInput(cPlayerSymbol);
    						 CheckMove(iRow, iColumn);
    						 ComputerMove(cPlayer, cComputer);
    						 iTotalMoves++;
    						 CheckWin(cPlayer);
    						 DrawGame();
    					 }
    					 while (cAns=='y'||cAns=='Y');
    				 }
    				 if (cPlayerSymbol=='o' || cPlayerSymbol == 'O') {
    					 do {
    						 DrawGame();
    						 UserInput(cPlayerSymbol);
    						 CheckMove(iRow, iColumn);
    						 ComputerMove(cPlayer, cComputer);
    						 iTotalMoves++;
    						 CheckWin(cPlayer);
    						 DrawGame();
    					 }
    					 while (cAns=='y'||cAns=='Y');
    					 }
    				
    			 }
    };
    
    int main() {
    	
    	cout<<"Let's play a game of Tic-Tac-Toe\n\n";
    	Game2 myGame;
    	myGame.StartGame();
    	myGame.PlayGame();
    	myGame.PlayGame();
    	system("pause");
    	return 0;
    }
    Any help?
    Last edited by Dewen; 05-24-2010 at 11:12 PM. Reason: better choice of words

  4. #4
    Registered User Xishem's Avatar
    Join Date
    Jan 2010
    Location
    Kansas, USA
    Posts
    1
    Well, what are you still needing help with?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe
    By zackKidd in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2009, 12:58 PM
  2. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 05:40 PM
  3. Check tic tac toe winner
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 12:41 PM
  4. My First c++ program : TIC TAC TOE !
    By renderstream in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2004, 04:58 PM
  5. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 08:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21