The program runs but only switches the player once

#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
	public:
        TicTacToe(void);//constructor
        void playOneGame(void);//member function
        void switchPlayer(char &);//member function
        void showBoard(void);// member function 
        void postMove(int, int, char);// member function
        char determineWinner(void);// member function

    private:
        char Board[3][3];            
};


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 column = 0;
    char Winner = ' ';
    int numberOfMoves = 0; //kep track of the number

    do{
        showBoard();
        cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
        cout << "\nEnter your row (1, 2, 3): ";
        cin >> row;
        cout << "\nEnter your column (1, 2, 3): ";
        cin >> column;

        postMove(row, column, currentPlayer); //post the move to the board

		switchPlayer(currentPlayer);//change player from x to o or vice versa

        Winner = determineWinner(); //see if anyone won the game

        numberOfMoves++;//keeptrack of the number of moves
    }
	while((Winner=='D')&&(numberOfMoves < MaxMoves));

    showBoard();//show the ending board

    if(Winner != 'D') //declare a winner
        cout << "\n\nTheWinner is player " <<Winner;
    else
        cout << "\n\nThe Game was a Draw" << endl;
}

TicTacToe::TicTacToe(void){
    //intialize the array contents
    
    for(int row= 0; row< 3; row ++){
        for (int column=0; column<3; column++){
        Board[row][column]=' ';
        }
    }
}

void TicTacToe::switchPlayer(char &currentPlayer){
    //switchs the current player
    
    currentPlayer = ' ';
        
    if (currentPlayer ='X')
		cout << "\nIt's player 1 turn" << endl;
           
	else (currentPlayer = 'O');
        cout << "\nIt's player 2 turn" << endl;
              
        
}

void TicTacToe::showBoard(){
    //displays the board
    cout << "     Tic Tac Toe!" << endl << endl;
    cout << "     1     2     3" << endl << endl;
    cout << "1    " << Board[0][0] << "  |  " << Board[0][1] << "  |  " << Board[0][2] << endl;
    cout << "   -----------------" << endl;
    cout << "2    " << Board[1][0] << "  |  " << Board[1][1] << "  |  " << Board[1][2] << endl;
    cout << "   -----------------" << endl;
    cout << "3    " << Board[2][0] << "  |  " << Board[2][1] << "  |  " << Board[2][2] << endl << endl;
}

    
void TicTacToe::postMove(int row, int col, char value){
    //gets the users move and post it to the board//showXO
    char currentPlayer = ' ';
    Board[row-1][col-1]= value;
    if (row!=0 && row != 1 && row != 2)
    {
        cout << "\nInvalid move, try Again" << endl;
    }
    else if (col != 0 && col != 1 && col != 2)
    {
        cout << "\nInvalid move, try Again" << endl;;
    }
    else if(Board[row][col]== currentPlayer)
    {
        cout << "\nSpace Taken, Try Again" << endl;;
    }

    
}

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 (Board[i][0] == Board[i][1]
                 && Board[i][1] == Board[i][2]
                 && Board[i][0] != ' '){
                 return Board[i][0];
            }
      }

      //check the clmns
      for (int i = 0; i < 3; i++){
             if (Board[0][i] == Board[1][i]
                   && Board[1][i] == Board[2][i]
                   && Board[0][i] != ' '){
                   return Board[0][i];
             }
       }

       //check the diagnals
       if (Board[0][0] == Board[1][1]
              && Board[1][1] == Board[2][2]
              && Board[0][0] != ' ') {
              return Board[0][0];
       }

       if (Board[2][0] == Board[1][1]
               && Board[1][1] == Board[0][2]
               && Board[2][0] != ' ') {
               return Board[2][0];
       }

       return 'D';

}

Recommended Answers

All 4 Replies

I believe your problems are here:

//line #78
if (currentPlayer ='X')

//and line #81
else (currentPlayer = 'O');

You are making a common mistake when it comes to boolean comparison.. take a minute, think about it, and tell us what ye' come up with.

In fact this error is so common, I believe compilers should throw warnings whenever this is detected.

If your talking about the semi colon I have already tried removing it and the code wouldn't run

I have never seen an assignment operator in boolean if/else logic... ever.

I have only seen boolean operators, such as: <, >, <=, >=, ==, !, && and ||.

And yes, that semicolon should not be there.

also, i think there is no such statement as

else (CONDITION)

it must be

else {
 // code here
}

or

else if (CONDITION) {
  // code here
}
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.