Tic Tac Toe program not working properly.

Hi, I just finished coding a refined version of xoax.net's Tictactoe C++ program, but there is a bug in the program. When you finish the first round of the game it asks you if you want to replay or not, if you enter 'y' it clears the board and restarts, but for some reason it won't accept the inputs. Heres the 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
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
#include <iostream>

int main()
{
  char caBoard[10]={'1','2','3','4','5','6','7','8','9'};
  bool bGameOver(false);
  bool bTie (false);
  int iPlayer = 0;
  char cMarker;
  bool bValidMove(false);
  char cReplay;


    do
        {
            std::cout << caBoard[0]<<"|" <<caBoard[1]<<"|" <<caBoard[2] <<std::endl;
            std::cout <<"-+-+-"<<std::endl;
            std::cout << caBoard[3]<<"|" <<caBoard[4]<<"|" <<caBoard[5] <<std::endl;
            std::cout <<"-+-+-"<<std::endl;
            std::cout << caBoard[6]<<"|" <<caBoard[7]<<"|" <<caBoard[8] <<std::endl;

            if (iPlayer == 0)
            {
                cMarker='X';
                iPlayer=1;
            }
            else
            {
                cMarker='O';
                iPlayer=0;
            }

            std::cout<<"Player "<<cMarker<<"'s move,"<<std::endl;

		// Loop until we get a valid move
            do
            {
                char cNextMove;
                std::cin >> cNextMove;
                if((cNextMove < '1' ||  cNextMove > '9') || caBoard[cNextMove - '1'] == 'X' || caBoard[cNextMove - '1'] == 'O')
                {
                    std::cout << "Invalid Move, fool! Try again!" << std::endl;
                    bValidMove = false;
                }
                else if (cNextMove == caBoard[cNextMove - '1'])
                {
                    caBoard[cNextMove - '1'] = cMarker;
                    bValidMove = true;
                }

            } while (!bValidMove);


            if (caBoard[0] != '1' )
            {
                if (caBoard[0] == caBoard[1] && caBoard[0] == caBoard[2])
                {
                    bGameOver = (true);
                    bTie = (false);
                }

                else if (caBoard[0] == caBoard[3] && caBoard[0] == caBoard[6])
                {
                    bGameOver = (true);
                    bTie = (false);
                }
            }

            if (caBoard[4] != '5')
            {
                if (caBoard[4] == caBoard[0] && caBoard[4] == caBoard[8])
                {
                    bGameOver = (true);
                    bTie = (false);
                }

                if (caBoard[4] == caBoard[2] && caBoard[4]== caBoard[6])
                {
                    bGameOver = (true);
                    bTie = (false);
                }

                if (caBoard[4] == caBoard[1] && caBoard[4] == caBoard[7])
                {
                    bGameOver = (true);
                    bTie = (false);
                }

                if (caBoard[4] == caBoard[3] && caBoard[4] == caBoard[5])
                {
                    bGameOver = (true);
                    bTie = (false);
                }

            }

            if (caBoard[8] != '8')
            {
                if (caBoard[8] == caBoard[2] && caBoard[8] == caBoard [6])
                {
                    bGameOver = (true);
                    bTie = (false);
                }

                if (caBoard[8] == caBoard[6] && caBoard[8] == caBoard[7])
                {
                    bGameOver = (true);
                    bTie = (false);
                }
            }

            if (caBoard[0] != '1' && caBoard[1] != '2' && caBoard[2] != '3' &&
                caBoard[3] != '4' && caBoard[4] != '5' && caBoard[5] != '6' &&
                caBoard[6] != '7' && caBoard[7] != '8' && caBoard[8] != '9' )
            {
                bGameOver = (true);
                bTie = (true);
            }

            if (bGameOver == true)
            {
            std::cout << caBoard[0]<<"|" <<caBoard[1]<<"|" <<caBoard[2] <<std::endl;
            std::cout <<"-+-+-"<<std::endl;
            std::cout << caBoard[3]<<"|" <<caBoard[4]<<"|" <<caBoard[5] <<std::endl;
            std::cout <<"-+-+-"<<std::endl;
            std::cout << caBoard[6]<<"|" <<caBoard[7]<<"|" <<caBoard[8] <<std::endl;

            std::cout <<"Game Over!"<< std::endl;
            }

            if (bGameOver == true && bTie == false)
            {
                std::cout << cMarker << "'s Won the game! Huzzah!" << std::endl;
                std::cout << "Do you want to reset the game? (y/n)" << std::endl;
                std::cin >> cReplay;
            }

            else if (bGameOver == true && bTie == true)
            {
                std::cout <<"Tie! Nobody won! Do you want to reset the game? (y/n) " << std::endl;
                std::cin >> cReplay;
            }

            if (cReplay == 'y')
            {
                for (int iX = 0; iX <9; ++iX)
                {
                    caBoard[iX] = iX+'1';
                }
                bGameOver = (false);
                bTie = (false);
                cReplay = 'y';
            }




        } while (!bGameOver);




return 0;
}


if you see the problem please tell me how to fix it. Thanks!
I'm also coding a tic-tac-toe game but can't find where bug is... I'm doing it a little different though.
I debugged your code and found the problem in like 2 minutes. I would advice you to learn how to debug as it is as important as programming.

but for some reason it won't accept the inputs

It does accept the inputs actually, the problem that the board is restarting after each input, not allowing you to see them.

The reason being, after one round, if the user enters 'y' to play again, the variable cReplay will be equal to 'y'. So each iteration, since cReplay is still equal to 'y', this if statement will be true - if (cReplay == 'y') and clear the board.

So you basically have to reset cReplay in the end of the game. Which you try to do in the if statement I mentioned above. You correctly restart bGameOver and bTie, but fail to reset cReplay.

1
2
3
4
5
6
7
8
9
10
 if (cReplay == 'y')
            {
                for (int iX = 0; iX <9; ++iX)
                {
                    caBoard[iX] = iX+'1';
                }
                bGameOver = (false);
                bTie = (false);
                cReplay = 'y'; // you have to set cReplay to literally any other character and the game will work perfectly fine
            }




Last edited on
Wowwwww I can't believe I didn't notice that!! I wanted to set cReplay to 't' and I guess I accidentally typed y!

Thanks mate!
Topic archived. No new replies allowed.