-
Notifications
You must be signed in to change notification settings - Fork 0
/
NineHoles.cpp
309 lines (258 loc) · 8.67 KB
/
NineHoles.cpp
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
#include "NineHoles.h"
#include "ui_NineHoles.h"
#include <iostream>
#include <QDebug>
#include <QSignalMapper>
#include <QMessageBox>
NineHoles::Player state2player(Hole::State state) {
switch (state) {
case Hole::RedState:
return NineHoles::RedPlayer;
case Hole::BlueState:
return NineHoles::BluePlayer;
default:
Q_UNREACHABLE();
}
}
Hole::State player2state(NineHoles::Player player) {
return player == NineHoles::RedPlayer ? Hole::RedState : Hole::BlueState;
}
NineHoles::NineHoles(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::NineHoles),
m_player(NineHoles::RedPlayer),
m_phase(NineHoles::DropPhase),
m_dropCount(0),
m_selected(nullptr) {
ui->setupUi(this);
QObject::connect(ui->actionNew, SIGNAL(triggered(bool)), this, SLOT(reset()));
QObject::connect(ui->actionQuit, SIGNAL(triggered(bool)), qApp, SLOT(quit()));
QObject::connect(ui->actionAbout, SIGNAL(triggered(bool)), this, SLOT(showAbout()));
QObject::connect(ui->actionAuthor, SIGNAL(triggered(bool)), this, SLOT(showAuthor()));
QObject::connect(this, SIGNAL(gameOver(Player)), this, SLOT(showGameOver(Player)));
QObject::connect(this, SIGNAL(gameOver(Player)), this, SLOT(reset()));
QSignalMapper* map = new QSignalMapper(this);
for (int id = 0; id < 9; id++) {
int r = id / 3;
int c = id % 3;
Hole* hole = this->findChild<Hole*>(QString("hole%1%2").arg(r).arg(c));
Q_ASSERT(hole != nullptr);
m_holes[id] = hole;
map->setMapping(hole, id);
QObject::connect(hole, SIGNAL(clicked(bool)), map, SLOT(map()));
}
QObject::connect(map, SIGNAL(mappedInt(int)), this, SLOT(play(int)));
this->updateStatusBar();
this->adjustSize();
this->setFixedSize(this->size());
}
NineHoles::~NineHoles() {
delete ui;
}
Hole* NineHoles::holeAt(int row, int col) const {
if (row >= 0 && row < 3 &&
col >= 0 && col < 3) {
int index = row * 3 + col;
return m_holes[index];
} else {
return 0;
}
}
void NineHoles::play(int id) {
Hole* hole = m_holes[id];
switch (m_phase) {
case NineHoles::DropPhase:
drop(hole);
break;
case NineHoles::MovePhase:
move(hole);
break;
default:
Q_UNREACHABLE();
}
}
void NineHoles::drop(Hole* hole) {
if (hole->state() == Hole::EmptyState) {
hole->setState(player2state(m_player));
if (isGameOver(hole))//nao quero que ninguem ganhe na faze de drop
//emit gameOver(m_player);
this->reset();
else {
++m_dropCount;
if (m_dropCount == 6){
m_phase = NineHoles::MovePhase;
}
this->switchPlayer();
}
}
}
void NineHoles::move(Hole* hole) {
QPair<Hole*,Hole*>* movement = nullptr;
if (hole->state() == Hole::SelectableState) {
Q_ASSERT(m_selected != 0);
movement = new QPair<Hole*,Hole*>(m_selected, hole);
} else {
if (hole->state() == player2state(m_player)) {
QList<Hole*> selectable = this->findSelectable(hole);
if (selectable.count() == 1) {
movement = new QPair<Hole*,Hole*>(hole, selectable.at(0));
} else if (selectable.count() > 1) {
this->clearSelectable();
foreach (Hole* tmp, selectable)
tmp->setState(Hole::SelectableState);
m_selected = hole;
}
}
}
if (movement != nullptr) {
this->clearSelectable();
m_selected = 0;
Q_ASSERT(movement->first->state() == player2state(m_player));
Q_ASSERT(movement->second->state() == Hole::EmptyState);
movement->first->setState(Hole::EmptyState);
movement->second->setState(player2state(m_player));
if (isGameOver(movement->second))
emit gameOver(m_player);
else
this->switchPlayer();
delete movement;
}
}
void NineHoles::switchPlayer() {
m_player = m_player == NineHoles::RedPlayer ?
NineHoles::BluePlayer : NineHoles::RedPlayer;
this->updateStatusBar();
}
bool isSelectable(Hole* hole) {
return hole != nullptr &&
(hole->state() == Hole::EmptyState ||
hole->state() == Hole::SelectableState);
}
void NineHoles::clearSelectable() {
for (int id = 0; id < 9; id++) {
Hole* hole = m_holes[id];
if (hole->state() == Hole::SelectableState)
hole->setState(Hole::EmptyState);
}
}
QList<Hole*> NineHoles::findSelectable(Hole* hole) {
QList<Hole*> list;
Hole* ne = this->holeAt(hole->row() + 1, hole->col()-1);
if (isSelectable(ne))
list << ne;
Hole* nw = this->holeAt(hole->row() - 1, hole->col()-1);
if (isSelectable(nw))
list << nw;
Hole* se = this->holeAt(hole->row() + 1, hole->col()+1);
if (isSelectable(se))
list << se;
Hole* sw = this->holeAt(hole->row() - 1, hole->col()+1);
if (isSelectable(sw))
list << sw;
Hole* left = this->holeAt(hole->row() - 1, hole->col());
if (isSelectable(left))
list << left;
Hole* up = this->holeAt(hole->row(), hole->col() - 1);
if (isSelectable(up))
list << up;
Hole* right = this->holeAt(hole->row() + 1, hole->col());
if (isSelectable(right))
list << right;
Hole* bottom = this->holeAt(hole->row(), hole->col() + 1);
if (isSelectable(bottom))
list << bottom;
return list;
}
bool NineHoles::checkRow(Player player, int col) {
Hole::State state = player2state(player);
for (int r = 0; r < 3; r++) {
Hole* tmp = this->holeAt(r, col);
Q_ASSERT(tmp != 0);
switch (tmp->state()) {
case Hole::RedState:
case Hole::BlueState:
if (state != tmp->state())
return false;
break;
default:
return false;
}
}
return true;
}
bool NineHoles::checkCol(Player player, int row) {
Hole::State state = player2state(player);
for (int c = 0; c < 3; c++) {
Hole* tmp = this->holeAt(row, c);
Q_ASSERT(tmp != 0);
switch (tmp->state()) {
case Hole::RedState:
case Hole::BlueState:
if (state != tmp->state())
return false;
break;
default:
return false;
}
}
return true;
}
bool NineHoles::checkDiagonals(Player player){
//Hole::State state = player2state(player);
Hole* tmp1;
Hole* tmp2;
bool d1=true, d2=true;
for(int d = 0; d < 3; d++){
tmp1 = this->holeAt(d, d);
tmp2 = this->holeAt(d, 2-d);
Q_ASSERT(tmp1 != 0);
Q_ASSERT(tmp2 != 0);
if(tmp1->state()!=player2state(player)){
d1 = false;
}
if(tmp2->state()!=player2state(player)){
d2 = false;
}
}
return d1 || d2;
}
bool NineHoles::isGameOver(Hole* hole) {
NineHoles::Player player = state2player(hole->state());
return this->checkRow(player, hole->col()) || this->checkCol(player, hole->row()) || this->checkDiagonals(player);
}
void NineHoles::reset() {
for (int id = 0; id < 9; id++) {
Hole* hole = m_holes[id];
hole->reset();
}
m_player = NineHoles::RedPlayer;
m_phase = NineHoles::DropPhase;
m_dropCount = 0;
m_selected = nullptr;
this->updateStatusBar();
}
void NineHoles::showAbout() {
QMessageBox::information(this, tr("Regras do Jogo"), tr(
"1. Jogadores alternadamente distribuem suas peças sobre o tabuleiro.\n2. Após peças postas move-se na horizontal e vertical cada jogador, em sua vez, sua peça.\n3. Ganha quem formar uma linha vertical ou horizontal primeiro."
));
}
void NineHoles::showAuthor(){
QMessageBox::information(this, tr("Autor"), tr("Marco Túlio Siqueira da Mata 20193007156\nmarcotulio.sm956@gmail.com "));
}
void NineHoles::showGameOver(Player player) {
switch (player) {
case NineHoles::RedPlayer:
QMessageBox::information(this, tr("Vencedor"), tr("Parabéns, o jogador vermelho venceu."));
break;
case NineHoles::BluePlayer:
QMessageBox::information(this, tr("Vencedor"), tr("Parabéns, o jogador azul venceu."));
break;
default:
Q_UNREACHABLE();
}
}
void NineHoles::updateStatusBar() {
QString player(m_player == NineHoles::RedPlayer ? "vermelho" : "azul");
QString phase(m_phase == NineHoles::DropPhase ? "colocar" : "mover");
ui->statusbar->showMessage(tr("Fase de %1: vez do jogador %2").arg(phase).arg(player));
}