Troubles with C++ assignment

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>

using namespace 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...
      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();           

            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 &currentPlayer){
         //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';
}
Your problem is the if statement:

1
2
3
4
5
if (currentPlayer == 'O')
        cout << "It's Player 1 turn" << endl;
    else
        (currentPlayer != 'O');
	cout << "It's Player 2 turn" << endl;


In an 'else' statement, one does not need a conditional. Remove that line, see if it works.

And great tictactoe game, btw. :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.
Topic archived. No new replies allowed.