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.



LinkBack URL
About LinkBacks


