I can't switch players

I have a tic tac toe game for class and I thought I had it all done but it will not switch from 1 player to the other where am I going wrong. Any help would be appreciated.

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

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){
    
    TicTacToe Game1;
    Game1.playOneGame();
}


    
        
    

void TicTacToe::playOneGame(void){
    
    const int MaxMoves = 9;
    char currentPlayer = 'O';
    int row = 0;
    int clmn = 0;
    char theWinner = ' ';
    int nmbrOfMoves = 0; 

    do{
        switchPlayer(currentPlayer);
        showBoard();
        cout << "\n\nPlayer " << currentPlayer << endl; 
        cout << "\nEnter your row (1, 2, 3): ";
        cin >> row;
        cout << "\nEnter your column (1, 2, 3): ";
        cin >> clmn;

        postMove(row, clmn, currentPlayer); 

        theWinner = determineWinner(); 

        nmbrOfMoves++;
    }while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

    showBoard();

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

TicTacToe::TicTacToe(void){
    
    
    for(int row= 0; row< 3; row ++){
        for (int clmn=0; clmn<3; clmn++){
        theBoard[row][clmn]=' ';
        }
    }
}


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

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

    

void TicTacToe::postMove(int row, int col, char value){
    
    char currentPlayer = ' ';
    theBoard[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(theBoard[row][col]== currentPlayer)
    {
        cout << "\nAlready Taken, Try Again" << endl;;
    }

    
}



char TicTacToe::determineWinner(void){
      
      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];
            }
      }

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

     
       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';

}
The following is illogical - read through it again and see:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void TicTacToe::switchPlayer(char &currentPlayer){
    
    
    currentPlayer = ' '; //<<   ???? Why
        
    if (currentPlayer ='X') //  =  or ==
    {        
        (currentPlayer = 'O');
        cout << "\nIt's player 1 turn" << endl;
        
    }
    else
        (currentPlayer = 'X');        
        
}
Still not figureing what I done wrong? This is why I am having a problem with this I am so confused. I got this far but getting the rest I am so confused.
guestgulkan was trying to point out a few problems.

For the first one, you have the line:
currentPlayer = ' ';
In that line, you're clearing currentPlayer. It doesn't make sense that your very next line is an if statement about currentPlayer because you already know exactly what currentPlayer is: currentPlayer = ' '.

That's kinda like deleting everything in a document and then wondering what's in the document. It doesn't make sense. Because you just cleared it, you know that there's nothing in it. Same deal with currentPlayer. Since you just set it ' ', you know that it's ' '.



The other big problem is that you're using
if (currentPlayer ='X').

You probably meant
if (currentPlayer == 'X').

If you've used something like Basic or Visual Basic before, you're probably used to using the equals sign for both assignments and comparisons. In C++, though, it's different. = is for assignments only. == is for comparisons.

In C++, if (currentPlayer ='X') means:
1. Set currentPlayer equal to 'X'.
2. Do whatever is in the if statement if the previous check was true. Otherwise, do whatever's in the else statement. (But because assignments to anything other than NULL are considered to be TRUE, setting something to 'X' will always be TRUE. So you're always going to go the TRUE path, never the FALSE one.)

What you want is:
if (currentPlayer == 'X')
This means:
1. Check to see if currentPlayer is 'X' or not. Return true if so, false if not.
2. Do whatever is in the if statement if the previous check was true. Otherwise, do whatever's in the else statement.
Last edited on
Ahhhh Thanks chemical that was a little more explanatory. I am new and struggling emmensily with this I was lucky I got this far. After guestgulkan I stared and moved things around trying to figure out exactly what was going on and finally posted again because I was so frustrated and confused. I actually didn't know that when I had currentPlayer = ' '; that this was like clearing everything, I thought it was to let me declare it later like int = 0.

I have it working now Thanks for the time and very detailed answer. I actually understand more of the statements and signs now and should help me on my next project now that I can grasp more of it.
Last edited on
Topic archived. No new replies allowed.