forked from julioCROS/Rastros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlimeTrail.cpp
214 lines (173 loc) · 6.23 KB
/
SlimeTrail.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
#include "SlimeTrail.h"
#include "ui_SlimeTrail.h"
#include <QDebug>
#include <QMessageBox>
#include <QActionGroup>
#include <QSignalMapper>
bool firstTurn = true;
int curr_id = 0;
SlimeTrail::Player otherPlayer(SlimeTrail::Player player) {
return (player == SlimeTrail::RedPlayer ?
SlimeTrail::BluePlayer : SlimeTrail::RedPlayer);
}
SlimeTrail::SlimeTrail(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::SlimeTrail),
m_player(SlimeTrail::RedPlayer){
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(this, SIGNAL(gameOver(Player)), this, SLOT(showGameOver(Player)));
QObject::connect(this, SIGNAL(gameOver(Player)), this, SLOT(reset()));
QSignalMapper* map = new QSignalMapper(this);
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
QString holeName = QString("hole%1%2").arg(row).arg(col);
Hole* hole = this->findChild<Hole*>(holeName);
Q_ASSERT(hole != nullptr);
Q_ASSERT(hole->row() == row && hole->col() == col);
m_board[row][col] = hole;
int id = row * 8 + col;
map->setMapping(hole, id);
QObject::connect(hole, SIGNAL(clicked(bool)), map, SLOT(map()));
}
}
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QObject::connect(map, SIGNAL(mapped(int)), this, SLOT(play(int)));
#else
QObject::connect(map, SIGNAL(mappedInt(int)), this, SLOT(play(int)));
#endif
// When the turn ends, switch the player.
QObject::connect(this, SIGNAL(turnEnded()), this, SLOT(switchPlayer()));
this->reset();
this->adjustSize();
this->setFixedSize(this->size());
}
SlimeTrail::~SlimeTrail() {
delete ui;
}
int SlimeTrail::testHole(int id, int curr_id){
Hole* hole = NULL;
int testStates = 0;
int blackStates = 0;
int colTests [8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int rowTests [8] = {-1, -1, -1, 0, 0, 1, 1, 1};
// Teste para saber se a peça branca está cercada
// por peças pretas
for(int i = 0; i < 9; i++){
if((curr_id / 8) + rowTests[i] >= 8 || (curr_id % 8) + colTests[i] >= 8
|| (curr_id / 8) + rowTests[i] < 0 || (curr_id % 8) + colTests[i] < 0){
continue;
}else {
testStates++;
hole = m_board[(curr_id / 8) + rowTests[i]][(curr_id % 8) + colTests[i]];
if(hole->state() == Hole::BlackState){
blackStates++;
}
}
}
// Se a peça branca estiver cercada de peças pretas
// então o jogo é finalizado com um EMPATE
if(blackStates == testStates){
return -1;
}
// Teste para validar se o jogador selecionou ou não
// uma posição valida para a peça branca
// 0 -> Posição invalida
// 1 -> Posição valida
if(id != curr_id - 9 && id != curr_id - 8 && id != curr_id - 7
&& id != curr_id - 1 && id != curr_id + 1 && id != curr_id + 7
&& id != curr_id + 8 && id != curr_id + 9){
return 0;
}
// Se a peça branca chegar na posição do jogador azul
// então o jogo é finalizado com VITORIA do jogador azul
if(id == 7){
return 2;
}
// Se a peça branca chegar na posição do jogador vermelho
// então o jogo é finalizado com VITORIA do jogador vermelho
if(id == 56){
return 3;
}
return 1;
}
int SlimeTrail::play(int id) {
if(firstTurn == true){
curr_id = 28;
firstTurn = false;
}
Hole* curr_hole = m_board[curr_id / 8][curr_id % 8];
Hole* hole = m_board[id / 8][id % 8];
if(testHole(id, curr_id) == -1){
emit gameOver(SlimeTrail::DrawPlayer);
return 0;
}
if(testHole(id, curr_id) == 2 || testHole(id, curr_id) == 3){
curr_hole->setState(Hole::BlackState);
hole->setState(Hole::WhiteState);
if(testHole(id, curr_id) == 2){
emit gameOver(SlimeTrail::BluePlayer);
return 0;
} else if(testHole(id, curr_id) == 3){
emit gameOver(SlimeTrail::RedPlayer);
return 0;
}
}
if(hole->state() != Hole::BlackState && testHole(id, curr_id) == 1){
curr_hole->setState(Hole::BlackState);
hole->setState(Hole::WhiteState);
curr_id = id;
} else {
emit turnEnded();
}
emit turnEnded();
return 0;
}
void SlimeTrail::switchPlayer() {
// Switch the player.
m_player = otherPlayer(m_player);
// Finally, update the status bar.
this->updateStatusBar();
}
void SlimeTrail::reset() {
// Reset board.
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
Hole* hole = m_board[row][col];
hole->reset();
//hole->setMarked(true);
hole->setState(Hole::EmptyState);
}
}
// Mark the starting position.
m_board[3][4]->setState(Hole::WhiteState);
firstTurn = true;
// Reset the player.
m_player = SlimeTrail::RedPlayer;
// Finally, update the status bar.
this->updateStatusBar();
}
void SlimeTrail::showAbout() {
QMessageBox::information(this, tr("Sobre"), tr("Rastros feito por: \n\n - Julio Cesar Rocha: [email protected] \n\n - Thalles Augusto Soares Verçosa: [email protected]"));
}
void SlimeTrail::showGameOver(Player player) {
switch (player) {
case SlimeTrail::RedPlayer:
QMessageBox::information(this, tr("Vencedor"), tr("Parabéns, o jogador vermelho venceu."));
break;
case SlimeTrail::BluePlayer:
QMessageBox::information(this, tr("Vencedor"), tr("Parabéns, o jogador azul venceu."));
break;
case SlimeTrail::DrawPlayer:
QMessageBox::information(this, tr("Empate"), tr("Nenhum dos jogadores venceu."));
break;
default:
Q_UNREACHABLE();
}
}
void SlimeTrail::updateStatusBar() {
QString player(m_player == SlimeTrail::RedPlayer ? "Vermelho" : "Azul");
ui->statusbar->showMessage(tr("Vez do Jogador %2").arg(player));
}