-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cpp
450 lines (363 loc) · 9.6 KB
/
GameManager.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
#include "GameManager.h"
#include "conio.h"
#include <iostream>
Piece* GameManager::tracker[64];
Piece* GameManager::pieces_black[16];
Piece* GameManager::pieces_white[16];
Piece* GameManager::threats[3];
int GameManager::result;
int GameManager::turn;
bool GameManager::check;
bool GameManager::selected;
bool GameManager::quit;
bool GameManager::checkmate;
int GameManager::selected_block;
int GameManager::threat_count;
int GameManager::LOS[7];
void GameManager::RefreshScreen()
{
//Draw the board first
Renderer::DrawBoard();
//Draw Pieces
for (int i = 0; i < 16; i++)
{
// Draw(pixels_piece, x_position, y_position)
if(pieces_black[i]->GetAcive())
Renderer::Draw(pieces_black[i]->GetPixels(), pieces_black[i]->GetScreenPos().x, pieces_black[i]->GetScreenPos().y);
if (pieces_white[i]->GetAcive())
Renderer::Draw(pieces_white[i]->GetPixels(), pieces_white[i]->GetScreenPos().x, pieces_white[i]->GetScreenPos().y);
}
}
void ClearInputStream()
{
//Remove all characters from the std in stream
while (_getch()) {}
}
void GameManager::Init()
{
turn = WHITE;
check = false;
checkmate = false;
selected = false;
quit = false;
threat_count = 0;
Renderer::SetSelect_Active(false);
//Initialize black pieces
pieces_black[0] = new King(3, BLACK, BMP_BK);
pieces_black[1] = new Queen(4, BLACK, BMP_BQ);
pieces_black[2] = new Bishop(2, BLACK, BMP_BB);
pieces_black[3] = new Bishop(5, BLACK, BMP_BB);
pieces_black[4] = new Knight(1, BLACK, BMP_BH);
pieces_black[5] = new Knight(6, BLACK, BMP_BH);
pieces_black[6] = new Rook(0, BLACK, BMP_BR);
pieces_black[7] = new Rook(7, BLACK, BMP_BR);
pieces_black[8] = new Pawn(8, BLACK, BMP_BP);
pieces_black[9] = new Pawn(9, BLACK, BMP_BP);
pieces_black[10] = new Pawn(10, BLACK, BMP_BP);
pieces_black[11] = new Pawn(11, BLACK, BMP_BP);
pieces_black[12] = new Pawn(12, BLACK, BMP_BP);
pieces_black[13] = new Pawn(13, BLACK, BMP_BP);
pieces_black[14] = new Pawn(14, BLACK, BMP_BP);
pieces_black[15] = new Pawn(15, BLACK, BMP_BP);
//Initialize white pieces
pieces_white[0] = new King(59, WHITE, BMP_WK);
pieces_white[1] = new Queen(60, WHITE, BMP_WQ);
pieces_white[2] = new Bishop(58, WHITE, BMP_WB);
pieces_white[3] = new Bishop(61, WHITE, BMP_WB);
pieces_white[4] = new Knight(57, WHITE, BMP_WH);
pieces_white[5] = new Knight(62, WHITE, BMP_WH);
pieces_white[6] = new Rook(56, WHITE, BMP_WR);
pieces_white[7] = new Rook(63, WHITE, BMP_WR);
pieces_white[8] = new Pawn(48, WHITE, BMP_WP);
pieces_white[9] = new Pawn(49, WHITE, BMP_WP);
pieces_white[10] = new Pawn(50, WHITE, BMP_WP);
pieces_white[11] = new Pawn(51, WHITE, BMP_WP);
pieces_white[12] = new Pawn(52, WHITE, BMP_WP);
pieces_white[13] = new Pawn(53, WHITE, BMP_WP);
pieces_white[14] = new Pawn(54, WHITE, BMP_WP);
pieces_white[15] = new Pawn(55, WHITE, BMP_WP);
//Update tracker with initial positions
UpdateTracker();
//Init movesets
for (int i = 0; i < 16; i++)
{
pieces_white[i]->RecalculateMS();
pieces_black[i]->RecalculateMS();
}
//Initialize marker
Renderer::SetMarker(32);
}
void GameManager::EndGame()
{
for (int i = 0; i < 16; i++)
{
delete pieces_white[i];
delete pieces_black[i];
}
}
void GameManager::AttemptMove()
{
int move_block = Renderer::GetMarker();
Piece* move_piece = tracker[selected_block];
//Assume the move is legal
bool illegal = false;
//Check if in moveset
if (!move_piece->CanMove(move_block))
return;
//Temporarily allow move to check legality
Piece* temp = tracker[move_block];
tracker[move_block] = move_piece;
tracker[selected_block] = 0;
//Kill code... Checks if move block is empty, kills piece if not empty
if (temp != 0)
temp->SetAcive(false);
Piece** opponent = turn ? pieces_black : pieces_white;
//Check if the move caused any piece from the waiting side
//to threaten the king of the playing side
illegal = CheckThreats(!turn);
if (illegal)
{
std::cout << "Illegal move" << std::endl;
//Reactivate the piece if it was there
if (temp != 0)
temp->SetAcive(true);
tracker[selected_block] = move_piece;
tracker[move_block] = temp;
//Refresh movesets
CheckThreats(!turn);
return;
}
//Perfrom the move if no voilations
move_piece->Move(move_block);
//Update the tracker with the new position
UpdateTracker();
DeselectBlock();
//Update only the two blocks that are effected by the move
Renderer::ReDrawBlocks(move_block, selected_block, move_piece->GetPixels(), 0);
//Check if the move by the playing side threatened
//the king of the waiting side
if (CheckThreats(turn))
{
std::cout << !turn << " in check " << std::endl;
std::cout << threat_count << " threat(s) detected " << std::endl;
if (Checkmate())
checkmate = true;
}
//Change the turn (waiting side is now the playing side
turn = !turn;
}
bool GameManager::Checkmate()
{
Piece* threatened_king = turn ? pieces_black[0] : pieces_white[0];
int x = threatened_king->GetBoardPos().x;
int y = threatened_king->GetBoardPos().y;
int o_block = Utils::IndexToBlock(x, y);
Piece* temp = NULL;
//Assume checkmate
bool mate = true;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int c_block = Utils::IndexToBlock(x + i, y + j);
//Try moving king to the surrounding blocks
if (threatened_king->CanMove(c_block))
{
threatened_king->Move(c_block, true);
if (tracker[c_block])
{
tracker[c_block]->SetAcive(false);
temp = tracker[c_block];
}
UpdateTracker();
//If any move results in the check being lifted mate = false
mate = mate && CheckThreats(turn);
//Move the king back to the orignal block
threatened_king->Move(o_block, true);
tracker[c_block] = temp;
if(temp)
tracker[c_block]->SetAcive(true);
CheckThreats(turn);
}
}
}
UpdateTracker();
CheckThreats(turn);
if (mate && threat_count == 1)
{
Compute_LOS(threats[0]->GetBlock(), o_block);
if (LOS_Breakable())
mate = false;
}
return mate;
}
bool GameManager::CheckThreats(bool color)
{
Piece** piece_set = color ? pieces_white : pieces_black;
threat_count = 0;
check = false;
for (int i = 0; i < 16; i++)
{
if (!piece_set[i]->GetAcive())
continue;
if (piece_set[i]->RecalculateMS())
{
check = true;
threats[threat_count] = piece_set[i];
std::cout << piece_set[i]->GetType() << " is a threat" << std::endl;
threat_count++;
}
}
return check;
}
bool GameManager::LOS_Breakable()
{
Piece** piece_set = !turn ? pieces_white : pieces_black;
int k = 0;
bool breakable = false;
while (LOS[k] != -1)
{
for (int i = 0; i < 16; i++)
{
breakable = breakable || (piece_set[i]->GetAcive() && piece_set[i]->GetType() != KING && piece_set[i]->CanMove(LOS[k]));
if ((piece_set[i]->GetAcive() && piece_set[i]->CanMove(LOS[k])))
std::cout << piece_set[i]->GetColor() << " " << piece_set[i]->GetType() << " can move to " << LOS[k] << std::endl;
}
k++;
}
std::cout << ((breakable) ? "LOS is breakable" : "LOS not breakable") << std::endl;
return breakable;
}
void GameManager::Compute_LOS(int attacker_pos, int king_pos)
{
int vert_offset = 8;
int hor_offset = 1;
int rdiag_offset = 9;
int ldiag_offset = 7;
int offset;
int diff_pos = king_pos - attacker_pos;
int counter = 0;
for (int i = 0; i < 7; i++)
LOS[i] = -1;
if (tracker[attacker_pos]->GetType() == KNIGHT || tracker[attacker_pos]->GetType() == PAWN)
{
LOS[0] = attacker_pos;
return;
}
if (diff_pos % vert_offset == 0)
offset = vert_offset;
else if (diff_pos % rdiag_offset == 0)
offset = rdiag_offset;
else if (diff_pos % ldiag_offset == 0)
offset = ldiag_offset;
else if (diff_pos % hor_offset == 0)
offset = hor_offset;
if (diff_pos < 0)
offset *= -1;
for (int i = attacker_pos; i != king_pos; i = i + offset)
{
LOS[counter] = i;
counter++;
}
return;
}
Piece ** GameManager::GetTracker()
{
return tracker;
}
void GameManager::HandleInput()
{
//Take char input
char input = _getch();
switch (input)
{
//Update marker position accordingly
case 'w':
case 'a':
case 's':
case 'd':
Renderer::UpdateMarker(input);
break;
//Refresh screen
case 'r':
RefreshScreen();
break;
//Quit game
case 'q':
quit = true;
result = QUIT;
break;
//Select block
case ' ':
if (selected)
{
AttemptMove();
break;
}
selected_block = Renderer::GetMarker();
if(!tracker[selected_block])
break;
if (tracker[selected_block]->GetColor() != turn)
break;
SelectBlock();
break;
case 'e':
DeselectBlock();
return;
}
}
void GameManager::SelectBlock()
{
Renderer::MarkBlock(Renderer::GetMarker(), SELECTED_MARKER_COLOR);
Renderer::SetSelected(selected_block);
Renderer::SetSelect_Active(true);
selected = true;
}
void GameManager::DeselectBlock()
{
Renderer::EraseMarker(selected_block);
Renderer::SetSelected(-1);
Renderer::SetSelect_Active(false);
selected = false;
}
void GameManager::StartMatch()
{
system("pause");
system("cls");
RefreshScreen();
while (!checkmate && !quit)
{
//Check for input without waiting for enter
if (_kbhit() != 0)
{
HandleInput();
ClearInputStream();
}
}
}
void GameManager::UpdateTracker()
{
//Clear the tracker
for (int i = 0; i < 64; i++)
tracker[i] = 0;
for (int i = 0; i < 16; i++)
{
//Update the tracker by assigning piece at appropriate index
if(pieces_white[i]->GetAcive())
tracker[pieces_white[i]->GetBlock()] = pieces_white[i];
if(pieces_black[i]->GetAcive())
tracker[pieces_black[i]->GetBlock()] = pieces_black[i];
}
}
void GameManager::DisplayResult()
{
if (quit)
std::cout << "You quit the game" << std::endl;
else if (checkmate)
{
if (!turn)
std::cout << "CHECKMATE: WHITE WINS!!!!!!" << std::endl;
else
std::cout << "CHECKMATE: BLACK WINS!!!!!!" << std::endl;
}
}