Good evening, I am writing a program in C++ for a Tic tac toe game.

Here are my directions:
Create a C++ console application that allows two players to participate in a tic-tac-toe game. The program will be responsible for deciding who wins the game.

The program should include the following class, properties and methods:

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);
};

I have it written and I have a few bugs I cannot figure out. It plays and works just fine except when Player X or Player O chooses a space that is already taken it tells them they have to choose another spot but then switches players so the current player has no chance to choose again. I know this is probably a very simple problem but I can't for the life of me figure it out. Here are my switch user function and my post move function.

void TicTacToe:: switchPlayer(char &currentPlayer){
     if(currentPlayer == 'O'){
          currentPlayer = 'X';
     }
     else
          currentPlayer = 'O';
}

void TicTacToe::postMove(int row, int col, char value){
     if (theBoard[row][col] == '-'){
          theBoard[row][col] = value;
     }
     else
          cout << "Space is already taken, please choose a different one." << endl;

I have tried several different if statements in my switchPlayer function but none seemed to work. Can anyone point me in the right direction?

Well what in my eyes seems to be the problem is that you are just printing that the space was taken after which your main game loops just goes on as if that was a valid turn.
You have to add a segment of code after the cout so that the player can retry the move.

//K0ns3rv

void TicTacToe::postMove(int row, int col, char value){
     if (theBoard[row][col] == '-'){
          theBoard[row][col] = value;
     }
     else
          cout << "Space is already taken, please choose a different one." << endl;

make the return type of the above function as bool. Return false if the space is already occupied.
Dont call the switchPlayer() method if postMove() returns false.

its a good suggestion by dkalita.. if u dont want to change the return type u can call the switchuser function inside the if block as follow ...

void TicTacToe::postMove(int row, int col, char value)
{
if (theBoard[row][col] == '-')
{
theBoard[row][col] = value;
//call the switch user function here...then u can return to the block  // which called this function
}

else

cout << "Space is already taken, please choose a different one." << endl;
}

Cheers ,
saradha

its a good suggestion by dkalita.. if u dont want to change the return type u can call the switchuser function inside the if block as follow ...

void TicTacToe::postMove(int row, int col, char value)
{
if (theBoard[row][col] == '-')
{
theBoard[row][col] = value;
//call the switch user function here...then u can return to the block  // which called this function
}

else

cout << "Space is already taken, please choose a different one." << endl;
}

Cheers ,
saradha

u can do that. But it's not a good OOP practice.

make the return type of the above function as bool. Return false if the space is already occupied.
Dont call the switchPlayer() method if postMove() returns false.

Thanks so much for your help. I have changed the function to bool type as suggested. I am having a bit of trouble figuring out where to put the if statement or while loop to call the switchPlayer or not.

Here's the main and play one game functions along with the other two.

class TicTacToe{
private:
    char theBoard[3][3];
public:
    TicTacToe(void);
    void playOneGame(void);
    void switchPlayer(char &);
    void showBoard(void);
    bool postMove(int, int, char);
    char determineWinner(void);

};
int main (void){
    //test the class by playing a 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 = ' ';
    char value = '-';
    int nmbrOfMoves = 0; //keep track of the number of moves

    do{
        switchPlayer(currentPlayer);//changes player from x to o or vice versa
        showBoard();
        cout << "\n\nPlayer " << currentPlayer << endl;
        //get players move
        cout << "Enter your row (1, 2, 3): ";
        cin >> row;
        cout << "Enter your column(1, 2, 3): ";
        cin >> clmn;

        //post the move to the board
        postMove(row-1, clmn-1, currentPlayer);
        //check to see if someone won
        theWinner = determineWinner();
        //keep track of moves
        nmbrOfMoves++;
    }


    while((theWinner == 'D')&&(nmbrOfMoves < MaxMoves));
    //show the ending board
    showBoard();

    //declare a winner
    if (theWinner != 'D')
        cout << "\n\nThe winner is player " << theWinner << endl;
    else
        cout << "\n\nThe game was a draw ";
}


void TicTacToe::switchPlayer(char &currentPlayer){
    //switches current player
    if(currentPlayer == 'O'){
        currentPlayer ='X';
    }       
    else
        currentPlayer = 'O';
}







bool TicTacToe::postMove(int row, int col, char value){
    //gets the users move and posts it to the board
    if (theBoard[row][col] == '-'){
        theBoard[row][col] = value;
        return true;
    }
    else{
        cout << "Space is already taken, please choose a different one." << endl;
        return false;
        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.