logic bug

i guys am using qt to make a simple x and o gui,

whenever I click on button number 9,it will seem to end the game

can anyone spot why? I've scanned through the code a few times and can't see why it's setting gameover to true

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


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    gameOver = false;
    draw = false;
    playerOne = true;
    playerTwo = false;

    for(int i = 0; i < 9; i++)
        buttonPushed[i] = false;
    QPalette sample_palette;
    sample_palette.setColor(QPalette::Window, Qt::transparent);
    sample_palette.setColor(QPalette::WindowText, Qt::red);

    ui->label->setAutoFillBackground(true);
    ui->label->setPalette(sample_palette);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::displayTurn(){

    if(playerOne)
    ui->label->setText("<b>Player one turn</b>");

    if(playerTwo)
    ui->label->setText("<b>Player two turn</b>");

}

void MainWindow::reset(){

    gameOver = false;
    playerOne = true;
    playerTwo = false;

    for(int i = 0; i < 9; i++)
        buttonPushed[i] = false;

    ui->pushButton->setText("  -  ");
    ui->pushButton_2->setText("  -  ");
    ui->pushButton_3->setText("  -  ");
    ui->pushButton_4->setText("  -  ");
    ui->pushButton_5->setText("  -  ");
    ui->pushButton_6->setText("  -  ");
    ui->pushButton_7->setText("  -  ");
    ui->pushButton_8->setText("  -  ");
    ui->pushButton_9->setText("  -  ");

}

void MainWindow::playAgain(){


    QMessageBox msgBox;
    msgBox.setText("do you want to play again?");
    QPushButton *yesButton = msgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noButton = msgBox.addButton(tr("No"), QMessageBox::ActionRole);
    msgBox.exec();

    if(msgBox.clickedButton() == yesButton){

        reset();
        return;
    }
    if(msgBox.clickedButton() == noButton){

        this->close();
    }
}

bool MainWindow::isGameOver(){


    if(ui->pushButton->text() == "  X  " && ui->pushButton_2->text() == "  X  "
         &&  ui->pushButton_3->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }

    if(ui->pushButton_4->text() == "  X  " && ui->pushButton_5->text() == "  X  "
            &&  ui->pushButton_6->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }
    if(ui->pushButton_7->text() == "  X  " && ui->pushButton_8->text() == "  X  "
            &&  ui->pushButton_9->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }

    if(ui->pushButton->text() == "  O  " && ui->pushButton_2->text() == "  O  "
         &&  ui->pushButton_3->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }

    if(ui->pushButton_4->text() == "  O  " && ui->pushButton_5->text() == "  O  "
            &&  ui->pushButton_6->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }
    if(ui->pushButton_7->text() == "  O  " && ui->pushButton_8->text() == "  O  "
            &&  ui->pushButton_9->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }

    if(ui->pushButton->text() == "  X  " && ui->pushButton_5->text() == "  X  "
            &&  ui->pushButton_9->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }
    if(ui->pushButton_3->text() == "  X  " && ui->pushButton_5->text() == "  X  "
            &&  ui->pushButton_7->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }

    if(ui->pushButton->text() == "  O  " && ui->pushButton_5->text() == "  O  "
            &&  ui->pushButton_9->text() == "  O  "){
                 ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }
    if(ui->pushButton_3->text() == "  O  " && ui->pushButton_5->text() == "  O  "
            &&  ui->pushButton_7->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }

    for(int i = 0; i < 9; i++){
        if(!buttonPushed[i]){
            return false;
        }
        draw = true;
        return draw;
    }

    return false;

    // IMPLEMENT DOWNWARD COLS
}

void  MainWindow::buttonsPushed(int number){


    if(playerOne && !buttonPushed[number] && !gameOver){

        buttonPushed[number] = true;

        switch(number){

          case 1:
            ui->pushButton->setText("  X  ");
            break;
           case 2:
            ui->pushButton_2->setText("  X  ");
            break;
          case 3:
            ui->pushButton_3->setText("  X  ");
            break;
        case 4:
            ui->pushButton_4->setText("  X  ");
            break;
        case 5:
            ui->pushButton_5->setText("  X  ");
            break;
        case 6:
            ui->pushButton_6->setText("  X  ");
            break;
           case 7:
            ui->pushButton_7->setText("  X  ");
            break;
        case 8:
            ui->pushButton_8->setText("  X  ");
            break;
        case 9:
            ui->pushButton_9->setText("  X  ");
            break;

        }

        if(isGameOver())
            gameOver = true;

        buttonPushed[number] = true;
        playerOne = false;
        playerTwo = true;

        if(!gameOver)
            displayTurn();
        if(gameOver){
            playAgain();
        }

        return;
    }

    if(playerTwo && !buttonPushed[number] && !gameOver){

        switch(number){

          case 1:
            ui->pushButton->setText("  O  ");
            break;
           case 2:
            ui->pushButton_2->setText("  O  ");
            break;
          case 3:
            ui->pushButton_3->setText("  O  ");
            break;
        case 4:
            ui->pushButton_4->setText("  O  ");
            break;
        case 5:
            ui->pushButton_5->setText("  O  ");
            break;
        case 6:
            ui->pushButton_6->setText("  O  ");
            break;
           case 7:
            ui->pushButton_7->setText("  O  ");
            break;
        case 8:
            ui->pushButton_8->setText("  O  ");
            break;
        case 9:
            ui->pushButton_9->setText("  O  ");
            break;

        }
        if(isGameOver())
            gameOver = true;

         buttonPushed[number] = true;
         playerOne = true;
         playerTwo = false;

         if(!gameOver)
             displayTurn();

         if(gameOver){
             if(draw){
                ui->label->setText("<b> DRAW!</b>");
             }
            playAgain();
         }

         return;
    }
}

void MainWindow::on_pushButton_clicked()
{

  buttonsPushed(1);
}



void MainWindow::on_pushButton_2_clicked()
{
  buttonsPushed(2);
}



void MainWindow::on_pushButton_3_clicked()
{
   buttonsPushed(3);
}

void MainWindow::on_pushButton_4_clicked()
{
    buttonsPushed(4);
}

void MainWindow::on_pushButton_5_clicked()
{
    buttonsPushed(5);
}

void MainWindow::on_pushButton_6_clicked()
{
    buttonsPushed(6);
}

void MainWindow::on_pushButton_7_clicked()
{
    buttonsPushed(7);
}

void MainWindow::on_pushButton_8_clicked()
{
    buttonsPushed(8);
}

void MainWindow::on_pushButton_9_clicked()
{
    buttonsPushed(9);
}

buttonPushed[number] = true; //validate number is in range here. if you had 9 or 10 for number, it could set a different variable (the machine just keeps going and modifies the memory at the location, which could be your game-over flag).

minor suggestions
its a little over-engineered in a few places
eg, instead of having isgameover feeding gameover, just call the isgameover function where you need it.
also player 1 and player 2 are technically unnecessary: either its player1s turn, or its not, and you can toggle this (player1 = !player1) every move without actually needing to track a player2. Its a tossup.. its easier to understand with 2 players, but its more code to track both and flip both etc and a lot of extra effort. This one.. depends on your mindset, I guess, but personally I "usually" find having 2 variables to represent 1 idea is a bad thing.

9 is used as a magic number a lot, consider making a named constant for it and any others. Magic numbers are generally bad practice.

thanks Jonnin

very true,I actually do go out of bounds in my bool array buttonPushed so I added -1 to the index,

great advice,I should be using constants , and also I could simplify the code a little bit :)

but the problem still persists when I click the last button or pushButton_9 it still ends the
game prematurely.


**Edit

my bad I actually forgot to change the check in the if statement also,the code now works as it should

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


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    gameOver = false;
    draw = false;
    playerOne = true;
    playerTwo = false;

    for(int i = 0; i < 9; i++)
        buttonPushed[i] = false;
    QPalette sample_palette;
    sample_palette.setColor(QPalette::Window, Qt::transparent);
    sample_palette.setColor(QPalette::WindowText, Qt::red);

    ui->label->setAutoFillBackground(true);
    ui->label->setPalette(sample_palette);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::displayTurn(){

    if(playerOne)
    ui->label->setText("<b>Player one turn</b>");

    if(playerTwo)
    ui->label->setText("<b>Player two turn</b>");

}

void MainWindow::reset(){

    gameOver = false;
    playerOne = true;
    playerTwo = false;

    for(int i = 0; i < 9; i++)
        buttonPushed[i] = false;

    ui->pushButton->setText("  -  ");
    ui->pushButton_2->setText("  -  ");
    ui->pushButton_3->setText("  -  ");
    ui->pushButton_4->setText("  -  ");
    ui->pushButton_5->setText("  -  ");
    ui->pushButton_6->setText("  -  ");
    ui->pushButton_7->setText("  -  ");
    ui->pushButton_8->setText("  -  ");
    ui->pushButton_9->setText("  -  ");

}

void MainWindow::playAgain(){


    QMessageBox msgBox;
    msgBox.setText("do you want to play again?");
    QPushButton *yesButton = msgBox.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noButton = msgBox.addButton(tr("No"), QMessageBox::ActionRole);
    msgBox.exec();

    if(msgBox.clickedButton() == yesButton){

        reset();
        return;
    }
    if(msgBox.clickedButton() == noButton){

        this->close();
    }
}

bool MainWindow::isGameOver(){


    if(ui->pushButton->text() == "  X  " && ui->pushButton_2->text() == "  X  "
         &&  ui->pushButton_3->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }

    if(ui->pushButton_4->text() == "  X  " && ui->pushButton_5->text() == "  X  "
            &&  ui->pushButton_6->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }
    if(ui->pushButton_7->text() == "  X  " && ui->pushButton_8->text() == "  X  "
            &&  ui->pushButton_9->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }

    if(ui->pushButton->text() == "  O  " && ui->pushButton_2->text() == "  O  "
         &&  ui->pushButton_3->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }

    if(ui->pushButton_4->text() == "  O  " && ui->pushButton_5->text() == "  O  "
            &&  ui->pushButton_6->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }
    if(ui->pushButton_7->text() == "  O  " && ui->pushButton_8->text() == "  O  "
            &&  ui->pushButton_9->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }

    if(ui->pushButton->text() == "  X  " && ui->pushButton_5->text() == "  X  "
            &&  ui->pushButton_9->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }
    if(ui->pushButton_3->text() == "  X  " && ui->pushButton_5->text() == "  X  "
            &&  ui->pushButton_7->text() == "  X  "){
                ui->label->setText("<b>GAME OVER PLAYER ONE WINS!</b>");
                return true;
    }

    if(ui->pushButton->text() == "  O  " && ui->pushButton_5->text() == "  O  "
            &&  ui->pushButton_9->text() == "  O  "){
                 ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }
    if(ui->pushButton_3->text() == "  O  " && ui->pushButton_5->text() == "  O  "
            &&  ui->pushButton_7->text() == "  O  "){
                ui->label->setText("<b>GAME OVER PLAYER TWO WINS!</b>");
                return true;
    }


    return false;

    // IMPLEMENT DOWNWARD COLS
}

void  MainWindow::buttonsPushed(int number){


    if(playerOne && !buttonPushed[number-1] && !gameOver){

        buttonPushed[number-1] = true;

        switch(number){

          case 1:
            ui->pushButton->setText("  X  ");
            break;
           case 2:
            ui->pushButton_2->setText("  X  ");
            break;
          case 3:
            ui->pushButton_3->setText("  X  ");
            break;
        case 4:
            ui->pushButton_4->setText("  X  ");
            break;
        case 5:
            ui->pushButton_5->setText("  X  ");
            break;
        case 6:
            ui->pushButton_6->setText("  X  ");
            break;
           case 7:
            ui->pushButton_7->setText("  X  ");
            break;
        case 8:
            ui->pushButton_8->setText("  X  ");
            break;
        case 9:
            ui->pushButton_9->setText("  X  ");


        }
        buttonPushed[number-1] = true;

        if(isGameOver())
            gameOver = true;


        playerOne = false;
        playerTwo = true;

        if(!gameOver)
            displayTurn();
        if(gameOver){
            playAgain();
        }

        return;
    }

    if(playerTwo && !buttonPushed[number-1] && !gameOver){

        buttonPushed[number-1] = true;

        switch(number){

          case 1:
            ui->pushButton->setText("  O  ");
            break;
           case 2:
            ui->pushButton_2->setText("  O  ");
            break;
          case 3:
            ui->pushButton_3->setText("  O  ");
            break;
        case 4:
            ui->pushButton_4->setText("  O  ");
            break;
        case 5:
            ui->pushButton_5->setText("  O  ");
            break;
        case 6:
            ui->pushButton_6->setText("  O  ");
            break;
           case 7:
            ui->pushButton_7->setText("  O  ");
            break;
        case 8:
            ui->pushButton_8->setText("  O  ");
            break;
        case 9:
            ui->pushButton_9->setText("  O  ");


        }

        buttonPushed[number-1] = true;

        if(isGameOver())
            gameOver = true;

         playerOne = true;
         playerTwo = false;

         if(!gameOver)
             displayTurn();

         if(gameOver){
             if(draw){
                ui->label->setText("<b> DRAW!</b>");
             }
            playAgain();
         }

         return;
    }
}

void MainWindow::on_pushButton_clicked()
{

  buttonsPushed(1);
}



void MainWindow::on_pushButton_2_clicked()
{
  buttonsPushed(2);
}



void MainWindow::on_pushButton_3_clicked()
{
   buttonsPushed(3);
}

void MainWindow::on_pushButton_4_clicked()
{
    buttonsPushed(4);
}

void MainWindow::on_pushButton_5_clicked()
{
    buttonsPushed(5);
}

void MainWindow::on_pushButton_6_clicked()
{
    buttonsPushed(6);
}

void MainWindow::on_pushButton_7_clicked()
{
    buttonsPushed(7);
}

void MainWindow::on_pushButton_8_clicked()
{
    buttonsPushed(8);
}

void MainWindow::on_pushButton_9_clicked()
{
    buttonsPushed(9);
}


Last edited on
Topic archived. No new replies allowed.