Join 244,291 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 937 people online right now. Registration is fast and FREE... Join Now!
int main (void) { //test the class by playing one game TicTacToe Game1; Game1.playOneGame();
}
void TicTacToe::playOneGame(void){ //start a game and play until someone wins or a draw occurs... const int MaxMoves = 9; char currentPlayer = 'O'; int row = 0; int clmn = 0; char theWinner = ' '; int nmbrOfMoves = 0; //keep track of the number of moves max is 9
do { switchPlayer(currentPlayer); //change player from x to o or vice versa showBoard();
postMove(row, clmn, currentPlayer); //post the move to the board theWinner = determineWinner(); //see if anyone won the game nmbrOfMoves++; //keep track of the number of moves
} while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));
showBoard(); //show the ending board
if (theWinner != 'D') //declare a winner cout << "\n\nThe Winner is player " << theWinner << endl; else cout << "\n\nThe Game was a Draw"; }
TicTacToe::TicTacToe(void) { //intialize the array contents int i, j;
for (i = 0; i < 3; i++) for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
}
void TicTacToe::switchPlayer(char ¤tPlayer) { //switches the current player
}
void TicTacToe::showBoard() { //displays the board void showBoard(void); { int t; for(t = 0; t < 3; t++) { printf (" %c | %c | %c ",theBoard[t][0], theBoard[t][1], theBoard[t][2]); if (t != 2) printf("\n---|---|---\n"); } printf ("\n"); } }
void TicTacToe::postMove(int row, int col, char value) { //gets the users move and posts it to the board int x, y;
printf("Enter Row (1,2,3),Column (1,2,3) for your move: "); scanf_s("%d%*c%d", &x, &y);
char TicTacToe::determineWinner(void){ //analyzes the board to see if there is a winner //returns a X, O indicating the winner //if the game is a draw then D is returned
//check the rows for (int i = 0; i < 3; i++){ if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2] && theBoard[i][0] != ' '){ return theBoard[i][0]; } }
//check the clmns for (int i = 0; i < 3; i++){ if (theBoard[0][i] == theBoard[1][i] && theBoard[1][i] == theBoard[2][i] && theBoard[0][i] != ' '){ return theBoard[0][i]; } }
//check the diagnals if (theBoard[0][0] == theBoard[1][1] && theBoard[1][1] == theBoard[2][2] && theBoard[0][0] != ' ') { return theBoard[0][0]; }
Since you keep track of the number of moves taken, when the number of moves is even set the currentPlayer char to 'X' and when odd set the char to 'O'.
Thank you very much, that is much appreciated....one more question.
I added that line and now it switches the part that says player 'X' or 'O', but when I play a turn it just puts a 'O', not quite sure what I need to place after that to make it work right. I'll post my code again just incase.
int main (void) { //test the class by playing one game TicTacToe Game1; Game1.playOneGame();
}
void TicTacToe::playOneGame(void){ //start a game and play until someone wins or a draw occurs... const int MaxMoves = 9; char currentPlayer = 'O'; int row = 0; int clmn = 0; char theWinner = ' '; int nmbrOfMoves = 0; //keep track of the number of moves max is 9
do { switchPlayer(currentPlayer); //change player from x to o or vice versa showBoard();
postMove(row, clmn, currentPlayer); //post the move to the board theWinner = determineWinner(); //see if anyone won the game nmbrOfMoves++; //keep track of the number of moves
} while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));
showBoard(); //show the ending board
if (theWinner != 'D') //declare a winner cout << "\n\nThe Winner is player " << theWinner << endl; else cout << "\n\nThe Game was a Draw"; }
TicTacToe::TicTacToe(void) { //intialize the array contents int i, j;
for (i = 0; i < 3; i++) for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
}
void TicTacToe::switchPlayer(char ¤tPlayer) { //switches the current player
currentPlayer = currentPlayer = 'O' ? 'X' : 'O';
}
void TicTacToe::showBoard() { //displays the board void showBoard(void); { int t; for(t = 0; t < 3; t++) { printf (" %c | %c | %c ",theBoard[t][0], theBoard[t][1], theBoard[t][2]); if (t != 2) printf("\n---|---|---\n"); } printf ("\n"); } }
void TicTacToe::postMove(int row, int col, char value) { //gets the users move and posts it to the board int x, y;
printf("Enter Row (1,2,3),Column (1,2,3) for your move: "); scanf_s("%d%*c%d", &x, &y);
char TicTacToe::determineWinner(void){ //analyzes the board to see if there is a winner //returns a X, O indicating the winner //if the game is a draw then D is returned
//check the rows for (int i = 0; i < 3; i++){ if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2] && theBoard[i][0] != ' '){ return theBoard[i][0]; } }
//check the clmns for (int i = 0; i < 3; i++){ if (theBoard[0][i] == theBoard[1][i] && theBoard[1][i] == theBoard[2][i] && theBoard[0][i] != ' '){ return theBoard[0][i]; } }
//check the diagnals if (theBoard[0][0] == theBoard[1][1] && theBoard[1][1] == theBoard[2][2] && theBoard[0][0] != ' ') { return theBoard[0][0]; }
Ok, I have added some code to the switch user part and it works....at first. the problem isn't with the turns anymore it is now that it is displaying either the wrong persons turn or it is not displaying it at all.
int main (void) { //test the class by playing one game TicTacToe Game1; Game1.playOneGame();
}
void TicTacToe::playOneGame(void){ //start a game and play until someone wins or a draw occurs... const int MaxMoves = 9; char currentPlayer = 'O'; int row = 0; int clmn = 0; char theWinner = ' '; int nmbrOfMoves = 0; //keep track of the number of moves max is 9
do { switchPlayer(currentPlayer); //change player from x to o or vice versa showBoard();
postMove(row, clmn, currentPlayer); //post the move to the board theWinner = determineWinner(); //see if anyone won the game nmbrOfMoves++; //keep track of the number of moves
} while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));
showBoard(); //show the ending board
if (theWinner != 'D') //declare a winner cout << "\n\nThe Winner is player " << theWinner << endl; else cout << "\n\nThe Game was a Draw"; }
TicTacToe::TicTacToe(void) { //intialize the array contents int i, j;
for (i = 0; i < 3; i++) for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
}
void TicTacToe::switchPlayer(char ¤tPlayer) { //switches the current player currentPlayer = currentPlayer == 'O' ? 'X' : 'O';
char TicTacToe::determineWinner(void){ //analyzes the board to see if there is a winner //returns a X, O indicating the winner //if the game is a draw then D is returned
//check the rows for (int i = 0; i < 3; i++){ if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2] && theBoard[i][0] != ' '){ return theBoard[i][0]; } }
//check the clmns for (int i = 0; i < 3; i++){ if (theBoard[0][i] == theBoard[1][i] && theBoard[1][i] == theBoard[2][i] && theBoard[0][i] != ' '){ return theBoard[0][i]; } }
//check the diagnals if (theBoard[0][0] == theBoard[1][1] && theBoard[1][1] == theBoard[2][2] && theBoard[0][0] != ' ') { return theBoard[0][0]; }
Why do you have functions defined inside of methods? You are too busy cutting and pasting code randomly and it's completely screwing you up. Think about what you're doing.
Well here is my problem, it shows that the player is switching but when a turn is taken it only shows an 'X', I have tried many things to fix this and some work but with other problems attached.
int main (void) { //test the class by playing one game TicTacToe Game1; Game1.playOneGame();
}
void TicTacToe::playOneGame(void){ //start a game and play until someone wins or a draw occurs... const int MaxMoves = 9; char currentPlayer = 'O'; int row = 0; int clmn = 0; char theWinner = ' '; int nmbrOfMoves = 0; //keep track of the number of moves max is 9
do { switchPlayer(currentPlayer); //change player from x to o or vice versa showBoard();
postMove(row, clmn, currentPlayer); //post the move to the board theWinner = determineWinner(); //see if anyone won the game nmbrOfMoves++; //keep track of the number of moves
} while ((theWinner == 'D') && (nmbrOfMoves < MaxMoves));
showBoard(); //show the ending board
if (theWinner != 'D') //declare a winner cout << "\n\nThe Winner is player " << theWinner << endl; else cout << "\n\nThe Game was a Draw"; }
TicTacToe::TicTacToe(void) { //intialize the array contents int i, j;
for (i = 0; i < 3; i++) for ( j = 0; j < 3; j++) theBoard[i][j] = ' ';
}
void TicTacToe::switchPlayer(char ¤tPlayer) { //switches the current player //change player from x to o or vice versa showBoard(); currentPlayer = currentPlayer == 'O' ? 'X' : 'O';
}
void TicTacToe::showBoard() { //displays the board
char TicTacToe::determineWinner(void){ //analyzes the board to see if there is a winner //returns a X, O indicating the winner //if the game is a draw then D is returned
//check the rows for (int i = 0; i < 3; i++){ if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2] && theBoard[i][0] != ' '){ return theBoard[i][0]; } }
//check the clmns for (int i = 0; i < 3; i++){ if (theBoard[0][i] == theBoard[1][i] && theBoard[1][i] == theBoard[2][i] && theBoard[0][i] != ' '){ return theBoard[0][i]; } }
//check the diagnals if (theBoard[0][0] == theBoard[1][1] && theBoard[1][1] == theBoard[2][2] && theBoard[0][0] != ' ') { return theBoard[0][0]; }
void TicTacToe::switchPlayer(char ¤tPlayer) { if(currentPlayer == 'O')//the if statements to switch every other move currentPlayer = 'X'; else if(currentPlayer == 'X') currentPlayer = 'O';
}
void TicTacToe::postMove(int row, int col, char value) { //gets the users move and posts it to the board int x, y;
printf("Enter Row (1,2,3),Column (1,2,3) for your move: "); scanf_s("%d%*c%d", &x, &y);
x--, y--;
if (theBoard [x][y] != ' ') { printf ("Invalid Move, Try Again.\n"); postMove(1, 3, x); } else theBoard [x][y] = value;//this needed to be value to change back and forth