Tic tac toe BOOL wrap around

Hi Im writing a tic tac toe board game for my programing book challenge but I cant get the bool to alternate between 1 and 0 for the player numbers. I keep getting "warning: no ‘operator++(int)’ declared for postfix ‘++’, trying prefix operator instead [-fpermissive]
player++;" but I try -fpermissive on g++ and it still gives the same message. How can I get the player to alternate between 1 and 0 every time the for loop that goes up to 9 is executed.

Please help I am a noob and I don't know how to move forward with this.

Thanks.

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
  #include<iostream>

using namespace std;

int main()
{

char a='0';
char b='x';
char c='-';

char tttboardarray[3][3]={c,c,c,c,c,c,c,c,c};

cout << "This is the array : " << endl;




        for(int k=0;k<3;k++)
        {
        cout << "\n";
        for(int l=0;l<3;l++)
        {
        cout <<tttboardarray[k][l];
        }

        }

cout <<"\n\n";

int o;

enum BOOL {FALSE,TRUE};

BOOL player = FALSE;



for (o=0;o<9;o++)
{

cout <<"o = " << o << endl;

cout << "Player 0 enter your 1st board co-ordinate : \n";
int i;
cin >>i;

cout << "Player " <<player<< "enter your 2cnd  board co_ordinate : \n";
int j;
cin >>j;

tttboardarray[i][j]=a;

cout << "Here is the array.\n";

for(int k=0;k<3;k++)
        {
        cout << "\n";
        for(int l=0;l<3;l++)
        {
        cout <<tttboardarray[k][l];
        }

        }

player++;

        }

cout <<"\n\n";

}
Last edited on
This code seems confused. The player being true or false makes no sense to me.

I would expect instead that the player could be player zero, or player one.

So make player an int. Set it to zero to begin. Every time you want to switch to the other player,

1
2
3
4
5
6
7
8
9
// switch player
if (player == 0)
{
  player = 1;
}
else if (player == 1)
{
  player = 0;
}


Also, C++ comes with a bool. You don't need to make your own out of an enum. If you ever want a bool, just use bool.
I had an enum Move in mine. The first player to go would be 'X', whether it was the human or the AI. This snippet also shows board initialization with a default symbol of choice (I wanted something fancy so that's probably why I went with string instead of char), the prompting of player move with some error checking based on the default symbol, and an example of player toggling.

You could do something similar with player toggling, but yeah, your enum is strangely named.
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
class TicTacToe
{
    
enum Move
{
    X,
    O
};
    
public:
    TicTacToe() :
        to_move_(Move::X),
        num_moves_(0),
        game_over_(false),
        winner_("No One")
    {
        //empty_row_ = string(" ")*side_ + ("|" + string(" ")*side_)*(side_-1);
        //space_ = string(" ")*(side_/2);
        //divider_ = string("-")*(side_*side_ + side_-1);
        
        board_.reserve(side_);
        for (int x=1; x<=side_; ++x)
        {
            vector<string> vec;
            vec.reserve(side_);
            for (int y=1; y<=side_; ++y)
                vec.push_back(default_symbol_);
            board_.push_back(vec);
        }
    }

    // Prompt row and column of move
    void PromptPlayerMove()
    {
        if (game_over_)
            return;

        int row, col;
        
        while (true)
        {
            cout << "Row? ";
            cin >> row;
            cout << "Col? ";
            cin >> col;
        
            if (! (0<=row && row<side_ && 0<=col && col<side_) )
            {
                cout << "Input out of range (0-"<<(side_-1)<<")\n";
                continue;
            }
            if (board_[row][col]!=default_symbol_)
            {
                cout << "Square ["<<row<<", "<<col<<"] is taken!\n";
                continue;
            }
            break;
        }
        board_[row][col] = to_move_ == Move::X ? "X" : "O";
        num_moves_ += 1;
        //CheckGameOver(row, col);
        to_move_ = (to_move_ == Move::X ? Move::O : Move::X);
    }   
    

private:
    vector<vector<string>> board_;
    const unsigned int side_ = 3;
    const string default_symbol_ = "˘";

    string empty_row_;
    string divider_;
    string space_;
    Move to_move_;
    int num_moves_;
    bool game_over_;
    string winner_;
};
Topic archived. No new replies allowed.