-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask3.c++
127 lines (106 loc) · 3.33 KB
/
task3.c++
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
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>
class TicTacToe : public QWidget {
Q_OBJECT
public:
TicTacToe(QWidget *parent = nullptr) : QWidget(parent) {
initializeUI();
}
private slots:
void handleButtonClick(int row, int col);
private:
void initializeUI();
void resetGame();
bool checkForWin();
bool checkForDraw();
QGridLayout *gridLayout;
QVector<QVector<QPushButton*>> buttons;
char currentPlayer;
};
void TicTacToe::initializeUI() {
gridLayout = new QGridLayout(this);
currentPlayer = 'X';
for (int i = 0; i < 3; ++i) {
QVector<QPushButton*> rowButtons;
for (int j = 0; j < 3; ++j) {
QPushButton *button = new QPushButton(this);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
button->setFont(QFont("Arial", 20));
connect(button, &QPushButton::clicked, [this, i, j]() { handleButtonClick(i, j); });
rowButtons.push_back(button);
gridLayout->addWidget(button, i, j);
}
buttons.push_back(rowButtons);
}
resetGame();
setLayout(gridLayout);
}
void TicTacToe::resetGame() {
currentPlayer = 'X';
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
buttons[i][j]->setText("");
}
}
}
void TicTacToe::handleButtonClick(int row, int col) {
if (buttons[row][col]->text().isEmpty()) {
buttons[row][col]->setText(QString(currentPlayer));
if (checkForWin()) {
QMessageBox::information(this, "Game Over", QString("Player ") + currentPlayer + " wins!");
resetGame();
} else if (checkForDraw()) {
QMessageBox::information(this, "Game Over", "It's a draw!");
resetGame();
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
}
bool TicTacToe::checkForWin() {
for (int i = 0; i < 3; ++i) {
if (buttons[i][0]->text() == currentPlayer &&
buttons[i][1]->text() == currentPlayer &&
buttons[i][2]->text() == currentPlayer) {
return true;
}
if (buttons[0][i]->text() == currentPlayer &&
buttons[1][i]->text() == currentPlayer &&
buttons[2][i]->text() == currentPlayer) {
return true;
}
}
if (buttons[0][0]->text() == currentPlayer &&
buttons[1][1]->text() == currentPlayer &&
buttons[2][2]->text() == currentPlayer) {
return true;
}
if (buttons[0][2]->text() == currentPlayer &&
buttons[1][1]->text() == currentPlayer &&
buttons[2][0]->text() == currentPlayer) {
return true;
}
return false;
}
bool TicTacToe::checkForDraw() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (buttons[i][j]->text().isEmpty()) {
return false;
}
}
}
return true;
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
TicTacToe game;
game.setWindowTitle("Tic-Tac-Toe");
game.resize(300, 300);
game.show();
return app.exec();
}
#include "main.moc"