I've been working on this code for a tic tac toe game for the past day. I can get it to compile but here is what I cannot figure out.
1. As soon as the player enters a move it does not display and declares the player who made the move the winner.
2. I think I might need to use boolean to decide which players turn it is but don't understand exactly how to accomplish this.
3.I haven't been able to figure out why when I compile my code it display's Player 1 and player 2 turn at the same time.
Yes this is a project for my class and I don't want a handout. I want to learn what mistakes I have made to better undrstand C++. I have only been learning C++ for the past five weeks with no prior experience so if anyone can help me or push me in the right direction I would greatly apperciated.I'm totally lost. Thanks Here is my code
#include <iostream>
#include <string>
usingnamespace std;
class TicTacToe{
private:
char theBoard [3][3];
public:
TicTacToe(void);
void playOneGame(void);
void switchPlayer(char &);
void showBoard(void);
void postMove(int, int, char);
char determineWinner(void);
};
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...
constint 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();
cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
cout << "Enter row of move (1, 2, 3): ";
cin >> row;
cout << "Enter column of move (1, 2, 3): ";
cin >> clmn;
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
theBoard [3][3];
for(int i = 0; i < 3;i++)
for (int j = 0; j < 3; j++);
}
void TicTacToe::switchPlayer(char ¤tPlayer){
//switches the current player
if (currentPlayer == 'O')
cout << "It's Player 1 turn" << endl;
else
(currentPlayer != 'O');
cout << "It's Player 2 turn" << endl;
}
void TicTacToe::showBoard(){
//displays the board
cout << "[ ] [ ] [ ] \n";
cout << "[ ] [ ] [ ] \n";
cout << "[ ] [ ] [ ] \n";
}
void TicTacToe::postMove(int row, int col, char value){
//gets the users move and posts it to the board
theBoard[row-1][col-1] = value;
}
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];
}
if (theBoard[2][0] == theBoard[1][1]
&& theBoard[1][1] == theBoard[0][2]
&& theBoard[2][0] != ' ') {
return theBoard[2][0];
}
return'D';
}
ok that took care of that problem thanks. But another issue I am having is that when I make a move it doesn't display on the board and immediatly puts out the winner is. I'm not sure how to get this function working.
Your switch player function doesn't actually switch the character being used.
ShowBoard doesn't print out the actual state of the board. (But you probably know this)
The 'instant win' probably could be caused by the use of cin>>stuff;. Either use getline() or make sure the stream is empty of excess '\n' or garbage before you keep going.