-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
executable file
·456 lines (389 loc) · 11.1 KB
/
Game.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
* Game Class
* libMorris
*
* @author Tibor Buzási <develop@tiborbuzasi.com>
*
* Copyright © 2020 Tibor Buzási. All rights reserved.
* For licensing information see LICENSE in the project root folder.
*/
#include "Game.hpp"
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <fstream>
// TODO: REWORK LOGGING
// #include "../eMorrisGUI/_Source/engine/UtilityFunctions.hpp"
// TODO: REMOVE
std::array<unsigned char, NUM_OF_FIELD_PLACES>* Game::GetField()
{
return &field;
}
Game::Game()
{
// Initialize
for (unsigned char index = 0; index < NUM_OF_PLAYERS; index++)
{
deck[index] = NUM_OF_PIECES;
numOfPieces[index] = 0;
}
// Select starting player
srand(time(nullptr));
currentPlayer = rand() % NUM_OF_PLAYERS;
// Setting game state
state = GameState::Place;
}
GameState Game::GetGameState()
{
return state;
}
inline unsigned char Game::GetCurrentPlayer()
{
return currentPlayer + 1;
}
unsigned char Game::GetDeck()
{
return deck[currentPlayer];
}
unsigned char Game::GetNumberOfPieces()
{
return numOfPieces[currentPlayer];
}
unsigned char Game::GetNumberOfMills()
{
return numOfMills;
}
void Game::CheckState()
{
switch (state)
{
case GameState::Init:
break;
case GameState::Place:
case GameState::Move:
NextPlayer();
// Set game state to end if the opponent has not enough pieces
if (numOfPieces[currentPlayer] < 3 && deck[currentPlayer] == 0)
{
state = GameState::End;
NextPlayer();
break;
}
NextPlayer();
// Set game state to remove if the current player has a mill
if (CheckForMills())
{
state = GameState::Remove;
break;
}
NextPlayer();
// Set game state to move if the opponent's deck is empty
if (deck[currentPlayer] == 0)
{
// Set game state to end if the opponent can't move
if (!CheckHasMove())
{
state = GameState::End;
NextPlayer();
break;
}
state = GameState::Move;
break;
}
break;
case GameState::Remove:
NextPlayer();
// Set game state to end if the opponent has not enough pieces
if (numOfPieces[currentPlayer] < 3 && deck[currentPlayer] == 0)
{
state = GameState::End;
NextPlayer();
break;
}
NextPlayer();
// Stay in this state if the current player still has mills
if (--numOfMills > 0)
{
break;
}
NextPlayer();
// Set game state to place if the opponent's deck is not empty
if (deck[currentPlayer] > 0)
{
CheckForMills();
state = GameState::Place;
break;
}
// Set game state to end if the opponent can't move
if (!CheckHasMove())
{
state = GameState::End;
NextPlayer();
break;
}
// Set game state to move if the piece was removed
state = GameState::Move;
break;
case GameState::End:
break;
}
}
bool Game::Place(unsigned char point)
{
if (state != GameState::Place || !CheckPlace(point))
{
return false;
}
// Set field point to current player's index
field[point] = GetCurrentPlayer();
deck[currentPlayer]--;
numOfPieces[currentPlayer]++;
return true;
}
bool Game::Move(unsigned char fromPoint, unsigned char toPoint)
{
// Check first part of the move
if (toPoint == 255)
{
if (state != GameState::Move || !CheckRemove(fromPoint, true))
{
return false;
}
}
// Check both parts of the move
else
{
if (state != GameState::Move || !CheckMove(fromPoint, toPoint))
{
return false;
}
// Set field point "to" to current player and field point "from" to empty
field[toPoint] = field[fromPoint];
field[fromPoint] = EMPTY_PLACE;
}
return true;
}
bool Game::Remove(unsigned char point)
{
if (state != GameState::Remove || !CheckRemove(point))
{
return false;
}
// Set field point to empty
field[point] = EMPTY_PLACE;
NextPlayer();
numOfPieces[currentPlayer]--;
NextPlayer();
return true;
}
void Game::NextPlayer()
{
// Set the next player as the current player
currentPlayer++;
if (currentPlayer >= NUM_OF_PLAYERS)
{
currentPlayer = 0;
}
}
bool Game::CheckHasMove()
{
if (numOfPieces[currentPlayer] <= 3)
{
return true;
}
for (unsigned char place = 0; place < NUM_OF_FIELD_PLACES; ++place)
{
if (field[place] != GetCurrentPlayer())
{
continue;
}
std::vector<unsigned char>::const_iterator api;
for (api = adjacentPlaces[place].cbegin(); api != adjacentPlaces[place].cend(); ++api)
{
if (field[*api] == EMPTY_PLACE)
{
return true;
}
}
}
return false;
}
bool Game::CheckPlace(unsigned char point)
{
// Check if point is empty
if (field[point] != 0)
{
return false;
}
return true;
}
bool Game::CheckMove(unsigned char fromPoint, unsigned char toPoint)
{
// Check if from point is the same as to point
if (fromPoint == toPoint)
{
return false;
}
// Check if from point has the current player's piece
if (field[fromPoint] != GetCurrentPlayer())
{
return false;
}
// Check if to point is empty
if (field[toPoint] != EMPTY_PLACE)
{
return false;
}
// Check move if the player can not jump
if (numOfPieces[currentPlayer] > 3)
{
// Check if player moves to the adjacent point (and does not jump to next square starting point)
if ((toPoint != fromPoint - 1 && toPoint != fromPoint + 1) || (fromPoint % NUM_OF_SQUARE_PLACES == 0
&& toPoint == fromPoint - 1) || (fromPoint % NUM_OF_SQUARE_PLACES == NUM_OF_SQUARE_PLACES - 1
&& toPoint == fromPoint + 1))
{
// Check if player moves where the square is closing (abs(from-to)==7)
if ((fromPoint % NUM_OF_SQUARE_PLACES != 0 || toPoint != fromPoint + NUM_OF_SQUARE_PLACES - 1)
&& (fromPoint % NUM_OF_SQUARE_PLACES != NUM_OF_SQUARE_PLACES - 1 || toPoint != fromPoint - NUM_OF_SQUARE_PLACES + 1))
{
// Check if player moves to another square
if (fromPoint % 2 != 1 || (toPoint != fromPoint - NUM_OF_SQUARE_PLACES && toPoint != fromPoint + NUM_OF_SQUARE_PLACES))
{
return false;
}
}
}
}
return true;
}
bool Game::CheckRemove(unsigned char place, bool currentPlayerCheck)
{
// Check if place is empty
if (field[place] == 0)
{
return false;
}
// Check if place has the current player's piece
else if (currentPlayerCheck && field[place] != GetCurrentPlayer())
{
return false;
}
// Check if place has the opponent's piece
else if (!currentPlayerCheck)
{
if (field[place] == GetCurrentPlayer())
{
return false;
}
// Check if piece is included in a mill and has other pieces that are not
std::array<bool, NUM_OF_FIELD_PLACES> isMills = { false };
NextPlayer(); // TODO: replace with more elegant solution
CheckForMills(&isMills);
if (isMills[place])
{
unsigned char count = 0;
for (unsigned char index = 0; index < NUM_OF_FIELD_PLACES; ++index)
{
if (isMills[index])
{
++count;
}
}
if (count != numOfPieces[currentPlayer])
{
NextPlayer(); // TODO: replace with more elegant solution
return false;
}
}
NextPlayer(); // TODO: replace with more elegant solution
}
return true;
}
bool Game::CheckForMills(std::array<bool, NUM_OF_FIELD_PLACES>* isMills)
{
std::vector<unsigned char> mills;
// Loop through the squares
for (unsigned char square = 0; square < NUM_OF_SQUARES; ++square)
{
// Loop through the sides of a square
for (unsigned char place = square * NUM_OF_SQUARE_PLACES;
place < (square + 1) * NUM_OF_SQUARE_PLACES; place += NUM_OF_PLACES_TO_SHIFT)
{
if (CheckMill(place, isMills))
{
mills.push_back(place);
}
}
}
// Loop through the connections of the squares
for (unsigned char place = 1; place < NUM_OF_SQUARE_PLACES; place += NUM_OF_PLACES_TO_SHIFT)
{
if (CheckMill(place, isMills))
{
mills.push_back(place);
}
}
// Don't update
if (isMills != nullptr)
{
return false;
}
// Looking for new mills
numOfMills = 0;
for (std::vector<unsigned char>::iterator it = mills.begin(); it != mills.end();
++it)
{
std::vector<unsigned char>::iterator itFind = std::find(
this->mills[currentPlayer].begin(), this->mills[currentPlayer].end(), *it);
if (itFind == this->mills[currentPlayer].end())
{
numOfMills++;
}
}
// Replace stored mills with the current ones
this->mills[currentPlayer].clear();
this->mills[currentPlayer].reserve(mills.size());
for (std::vector<unsigned char>::iterator it = mills.begin(); it != mills.end(); ++it)
{
this->mills[currentPlayer].push_back(*it);
}
// Return true if the current player has a mill
if (numOfMills > 0)
{
return true;
}
return false;
}
bool Game::CheckMill(unsigned char place, std::array<bool, NUM_OF_FIELD_PLACES>* isMills)
{
std::array<unsigned char, NUM_OF_PLACES_TO_SHIFT + 1> places = GetMillPlaces(place);
if (field[places[0]] == GetCurrentPlayer() && field[places[1]] == field[places[0]]
&& field[places[2]] == field[places[0]])
{
if (isMills != nullptr)
{
isMills->at(places[0]) = true;
isMills->at(places[1]) = true;
isMills->at(places[2]) = true;
}
return true;
}
return false;
}
std::array<unsigned char, NUM_OF_PLACES_TO_SHIFT + 1> Game::GetMillPlaces(unsigned char place)
{
std::array<unsigned char, NUM_OF_PLACES_TO_SHIFT + 1> places;
// Get mill places on the connection of the squares
if (place % 2 == 1 && place < NUM_OF_SQUARE_PLACES)
{
places = { place, place + NUM_OF_SQUARE_PLACES, place + NUM_OF_SQUARE_PLACES * 2 };
}
// Get mill places on the side of a square
if (place % 2 == 0 && place < NUM_OF_FIELD_PLACES)
{
unsigned char placePlusTwo = (place + 2) % NUM_OF_SQUARE_PLACES == 0 ? (place / NUM_OF_SQUARE_PLACES) *
NUM_OF_SQUARE_PLACES : place + 2;
places = { place, place + 1, placePlusTwo };
}
return places;
}