Program outputting <player> Wins two times instead of 1.

Can someone help me out?

When Player x wins program says, X wins 2 times. I cant figure out why.


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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  #include <iostream>
#include <string>
#include <windows.h>

using namespace std;

string newgame;
int playerx;
int playero;
char board [9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
char player [2] = {'X', 'O'};
bool gamewon();
bool spot_validx();
bool spot_valido();
bool new_game();
int leaderboard_x = 0;
int leaderboard_o = 0;

//If playerx input is 1 and the spot is valid change that board piece to the players Symbol(X).
bool update_gameboardx ()
{
    if (playerx == 1 && spot_validx() == true)
    {
        board[0] = player[0];
    }
    else if  (playerx == 2 && spot_validx() == true)
    {
        board[1] = player[0];
    }
    else if  (playerx == 3 && spot_validx() == true)
    {
        board[2] = player[0];
    }
    else if  (playerx == 4 && spot_validx() == true)
    {
        board[3] = player[0];
    }
    else if  (playerx == 5 && spot_validx() == true)
    {
        board[4] = player[0];
    }
    else if  (playerx == 6 && spot_validx() == true)
    {
        board[5] = player[0];
    }
    else if  (playerx == 7 && spot_validx() == true)
    {
        board[6] = player[0];
    }
    else if  (playerx == 8 && spot_validx() == true)
    {
        board[7] = player[0];
    }
    else if  (playerx == 9 && spot_validx() == true)
    {
        board[8] = player[0];
    }

}

//If playerx input is 1 and the spot is valid change that board piece to the players Symbol(O).
bool update_gameboardo ()
{
    if (playero == 1 && spot_valido() == true)
    {
        board[0] = player[1];
    }
    else if  (playero == 2 && spot_valido() == true)
    {
        board[1] = player[1];
    }
    else if  (playero == 3 && spot_valido() == true)
    {
        board[2] = player[1];
    }
    else if  (playero == 4 && spot_valido() == true)
    {
        board[3] = player[1];
    }
    else if  (playero == 5 && spot_valido() == true)
    {
        board[4] = player[1];
    }
    else if  (playero == 6 && spot_valido() == true)
    {
        board[5] = player[1];
    }
    else if  (playero == 7 && spot_valido() == true)
    {
        board[6] = player[1];
    }
    else if  (playero == 8 && spot_valido() == true)
    {
        board[7] = player[1];
    }
    else if  (playero == 9 && spot_valido() == true)
    {
        board[8] = player[1];
    }

}

//Shows gameboard as it currently is.
void gameboard ()
{
    system ("cls");
    cout<<"          |          |            "<<endl;
    cout<<"     "<<board[0]<<"    |     "<<board[1]<<"    |     "<<board[2]<<"      "<<endl;
    cout<<"          |          |            "<<endl;
    cout<<"----------+----------+----------"<<endl;
    cout<<"          |          |            "<<endl;
    cout<<"     "<<board[3]<<"    |     "<<board[4]<<"    |     "<<board[5]<<"      "<<endl;
    cout<<"          |          |            "<<endl;
    cout<<"----------+----------+----------"<<endl;
    cout<<"          |          |            "<<endl;
    cout<<"     "<<board[6]<<"    |     "<<board[7]<<"    |     "<<board[8]<<"      "<<endl;
    cout<<"          |          |            "<<endl;
}

//The while loop to play through X and O turns
bool playerturn ()
{
    while (gamewon() == false)
    {
        cout<<"Player "<<player[0]<<" please enter a board position (1-9)."<<endl;
        cin>>playerx;
        spot_validx();
        while (spot_validx() == false)
        {
            cout<<"Please select an empty spot"<<endl;
            cin>>playerx;
            spot_validx();
        }
            update_gameboardx();
            gameboard();

        if (gamewon() == true)
        {
            continue;
        }


        cout<<"Player "<<player[1]<<" please enter a board position (1-9)."<<endl;
        cin>>playero;
        spot_valido();
        while (spot_valido() == false)
        {
            cout<<"Please select an empty spot"<<endl;
            cin>>playero;
            spot_valido();
        }

            update_gameboardo();
            gameboard();


    }


Sleep(2000);
system("cls");

    cout<<leaderboard_x - 1<<" Wins for X"<<" |  "<<leaderboard_o<<" Wins for O"<<endl;
    cout<<"             |           "<<endl;
    cout<<"             |           "<<endl;
    cout<<"             |           "<<endl;
    cout<<"             |           "<<endl;
    cout<<"             |           "<<endl;


Sleep(4000);

    new_game();
}

//Checks if spot isnt X or O
bool spot_validx()
{
    int i = playerx - 1  ;
        if (board[i] != 'X' && board[i] != 'O')
        {
            gamewon();
            return true;

        }
        else
        {
            return false;
        }



}

//Checks if spot isnt X or O
bool spot_valido()
{
    int i = playero - 1;
        if (board[i] != 'X' && board[i] != 'O')
        {
            gamewon();
            return true;
        }
        else
        {
            return false;
        }



}

//Game Win Conditions, If game is won leaderboard will be updated and player that won will be updated.
bool gamewon()
{
    //Horizontal Win Cases
    if ((board[0] == board[1]) && (board[0] == board [2]))
    {
        cout<<board[0]<<" Wins!"<<endl;
        if (board [0] == 'X')
        {
         leaderboard_x += 1;
        }
        else if (board[0] == 'O')
        {
            leaderboard_o += 1;
        }
        return true;
    }
    else if (board[3] == board[4] && board[3] == board [5])
    {
        cout<<board[3]<<" Wins!"<<endl;
        if (board [3] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[3] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    else if (board[6] == board[7] && board[6] == board [8])
    {
        cout<<board[6]<<" Wins!"<<endl;
        if (board [6] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[6] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    // Vertical Win Cases
    else if (board[0] == board[3] && board[0] == board [6])
    {
        cout<<board[0]<<" Wins!"<<endl;
        if (board [0] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[0] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    else if (board[1] == board[4] && board[1] == board [7])
    {
        cout<<board[1]<<" Wins!"<<endl;
        if (board [1] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[1] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    else if (board[2] == board[5] && board[2] == board [8])
    {
        cout<<board[2]<<" Wins!"<<endl;
        if (board [2] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[2] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    //Diagonal Win Cases
    else if (board[0] == board[4] && board[0] == board [8])
    {
        cout<<board[0]<<" Wins!"<<endl;
        if (board [0] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[0] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    else if (board[2] == board[4] && board[2] == board [6])
    {
        cout<<board[2]<<" Wins!"<<endl;
        if (board [2] == 'X')
        {
         leaderboard_x ++;
        }
        else if (board[2] == 'O')
        {
            leaderboard_o ++;
        }
        return true;
    }
    else
        return false;




}

//Will ask user if they want new game.
bool new_game()
{

        system("cls");
        cout<<"Would you like to play again? (Enter 'yes' or 'no')"<<endl;
        cin>>newgame;

        if (newgame == "yes" || newgame == "y" || newgame == "YES")
        {
            board[0] = '1';
            board[1] = '2';
            board[2] = '3';
            board[3] = '4';
            board[4] = '5';
            board[5] = '6';
            board[6] = '7';
            board[7] = '8';
            board[8] = '9';

            gameboard();
            playerturn();


        }
        else if (newgame == "no" || newgame == "n" || newgame == "NO")
        {
            system ("cls");
            cout<<endl<<endl<<endl<<"Thank you for playing!"<<endl;
        }
        else{
            cout<<"Please enter a correct response."<<endl;
            cin>>newgame;
        }

}
int main()
{
    cout<<"Tic tac Toe by Daniel"<<endl<<endl;
    gameboard();


    cout<<endl<<"Player X starts first"<<endl;

    playerturn();


}


closed account (iAk3T05o)
You made mistakes with the winning part.
It's 3 == 4 && 4 == 5, 4 == 6 && 6 == 8 etc.

You also made other mistakes.
Last edited on
I dont doubt that i've made other mistakes. I expected that however I still dont understand what the difference is between what i did:

if ((board[0] == board[1]) && (board[0] == board [2]))

and what your saying:

It's 3 == 4 && 4 == 5, 4 == 6 && 6 == 8 etc.

Both of these ways compare the same thing. If 3 spots are inhabited by the same symbol.

I also don't understand why my way would output "X wins" twice vs your way which is essentially saything the samething in a different way.





Try removing spot_valido() on line 145 (The one BEFORE the while).
Same for spot_validx() on line 127.
Last edited on
I tried removing the spot_valido before the while loop and it still outputs "X wins twice". When O wins it only outputs it one time "O wins". "X wins" still says it twice each on a new line.
Topic archived. No new replies allowed.