-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamelogic.cpp
271 lines (232 loc) · 7.34 KB
/
gamelogic.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
#include "gamelogic.h"
#include <random>
void GameLogic::init()
{
switch (level)
{
case GameLevel::EASY:
boardSize = 3;
break;
case GameLevel::MODERATE:
boardSize = 4;
break;
case GameLevel::HARD:
boardSize = 5;
break;
default:
boardSize = 3;
break;
}
//create an empty board
for (size_t i = 0; i < boardSize; i++)
{
for (size_t j = 0; j < boardSize; j++)
{
board[i][j] = State::EMPTY;
}
}
remainingPiecesBlack = remainingPiecesWhite = boardSize;
playerMoved = false;
//randomly determine the first player
std::random_device rd;
std::mt19937 gen(rd());
std::bernoulli_distribution distribution(0.5);
bool drawFirstPlayer = distribution(gen);
currentPlayer = drawFirstPlayer ? GameLogic::Player::BLACK : GameLogic::Player::WHITE;
pieceSelected = false;
}
bool GameLogic::PlayerMoved() const
{
return playerMoved;
}
int GameLogic::RemainingPiecesBlack() const { return remainingPiecesBlack; }
int GameLogic::RemainingPiecesWhite() const { return remainingPiecesWhite; }
GameLogic::Player GameLogic::CurrentPlayer() const { return currentPlayer; }
bool GameLogic::isGameOver()
{
//search for game over
//check rows and columns
for (size_t i = 0; i < boardSize; i++)
{
State firstOfRow = board[i][0];
State firstOfColumn = board[0][i];
size_t rowCounter = 0;
size_t columnCounter = 0;
for (size_t j = 0; j < boardSize; j++)
{
if (board[i][j] == firstOfRow && board[i][j] != State::EMPTY)
{
rowCounter++;
}
if (board[j][i] == firstOfColumn && board[j][i] != State::EMPTY)
{
columnCounter++;
}
}
if (rowCounter == boardSize || columnCounter == boardSize)
{
return true;
}
}
//check diagonals
State firstOfDiagonal1 = board[0][0];
State firstOfDiagonal2 = board[0][boardSize - 1];
size_t diagonal1Counter = 0;
size_t diagonal2Counter = 0;
for (size_t i = 0; i < boardSize; i++)
{
if (board[i][i] == firstOfDiagonal1 && board[i][i] != State::EMPTY)
{
diagonal1Counter++;
}
if (board[i][boardSize - 1 - i] == firstOfDiagonal2 && board[i][boardSize - 1 - i] != State::EMPTY)
{
diagonal2Counter++;
}
if (diagonal1Counter == boardSize || diagonal2Counter == boardSize)
{
return true;
}
}
return false;
}
size_t GameLogic::Size()
{
return boardSize;
}
void GameLogic::setLevel(GameLogic::GameLevel selected)
{
level = selected;
}
void GameLogic::togglePlayer()
{
currentPlayer = (currentPlayer == Player::BLACK) ? Player::WHITE : Player::BLACK;
playerMoved = true;
}
bool GameLogic::selectPiece(Point &p)
{
if (!allPiecesPlaced())
{
return true;
}
//convert coordinates to board indexes
int gridX, gridY;
gridX = (p.x - BOARD_OFFSET) / ((SCREEN_WIDTH - BOARD_OFFSET) / boardSize);
gridY = (p.y - BOARD_OFFSET) / ((SCREEN_WIDTH - BOARD_OFFSET) / boardSize);
//check if point is in range
if (gridX >= 0 && gridX < boardSize && gridY >= 0 && gridY < boardSize)
{
//check if it is a valid click
//find center of clickable area
int centerX, centerY, r;
r = ((SCREEN_WIDTH - BOARD_OFFSET) / boardSize - BOARD_OFFSET) / 2;
centerX = BOARD_OFFSET + gridX * (SCREEN_WIDTH - BOARD_OFFSET) / boardSize + r;
centerY = BOARD_OFFSET + gridY * (SCREEN_WIDTH - BOARD_OFFSET) / boardSize + r;
//check if click is inside the circle
if (r * r > (pow(centerX - p.x, 2) + pow(centerY - p.y, 2)))
{
if (board[gridY][gridX] != State::EMPTY)
{
pieceSelected = true;
if (currentPlayer == Player::BLACK && board[gridY][gridX] == State::BLACK)
{
source.x = gridX;
source.y = gridY;
return true;
}
else if (currentPlayer == Player::WHITE && board[gridY][gridX] == State::WHITE)
{
source.x = gridX;
source.y = gridY;
return true;
}
}
}
pieceSelected = false;
}
source.x = source.y = -1;
return false;
}
bool GameLogic::selectTarget(Point &p)
{
//convert coordinates to board indexes
int gridX, gridY;
gridX = (p.x - BOARD_OFFSET) / ((SCREEN_WIDTH - BOARD_OFFSET) / boardSize);
gridY = (p.y - BOARD_OFFSET) / ((SCREEN_WIDTH - BOARD_OFFSET) / boardSize);
//check if point is in range
if (gridX >= 0 && gridX < boardSize && gridY >= 0 && gridY < boardSize)
{
//check if it is a valid click
//find center of clickable area
int centerX, centerY, r;
r = ((SCREEN_WIDTH - BOARD_OFFSET) / boardSize - BOARD_OFFSET) / 2;
centerX = BOARD_OFFSET + gridX * (SCREEN_WIDTH - BOARD_OFFSET) / boardSize + r;
centerY = BOARD_OFFSET + gridY * (SCREEN_WIDTH - BOARD_OFFSET) / boardSize + r;
//check if click is inside the circle
if (r * r > (pow(centerX - p.x, 2) + pow(centerY - p.y, 2)))
{
if (!allPiecesPlaced())
{
//check if player attempts to make a shortcut to game end.
//during placement, the final move is blocked
State currentState = board[gridY][gridX];
if (currentPlayer == Player::BLACK && board[gridY][gridX] == State::EMPTY)
{
board[gridY][gridX] = State::BLACK;
remainingPiecesBlack--;
}
else
{
board[gridY][gridX] = State::WHITE;
remainingPiecesWhite--;
}
if (isGameOver())
{
//undo the move
printf("gameover\n");
board[gridY][gridX] = currentState;
if (currentPlayer == Player::BLACK)
{
remainingPiecesBlack++;
}
else
{
remainingPiecesWhite++;
}
}
else
{
playerMoved = true;
togglePlayer();
}
return true;
}
else
{
//calculate manhattan distance between source and target
int diff = abs(gridX - source.x) + abs(gridY - source.y);
if (diff == 1)
{
if (board[gridY][gridX] == State::EMPTY)
{
target.x = gridX;
target.y = gridY;
pieceSelected = false;
return true;
}
}
}
}
}
pieceSelected = false;
target.x = target.y = -1;
return false;
}
bool GameLogic::allPiecesPlaced()
{
return !(remainingPiecesBlack + remainingPiecesWhite);
}
bool GameLogic::PieceSelected() const
{
return pieceSelected;
}