-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.cpp
49 lines (44 loc) · 1.68 KB
/
King.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
//
// Created by tjensen on 6/5/2020.
//
#include "King.h"
King::King() {
name = " K ";
}
/**
* overloaded CheckIfCaptured for the King piece.
* @param board a reference to the board object
* @param y the King's y-coordinate
* @param x the King's x-coordinate
* @param done a boolean to indicate if the game is complete
*/
void King::CheckIfCaptured(vector<vector <Pieces*>> &board, short y, short x, bool &done) {
//first priority, check if the king has escaped to an edge of the board
if ((y == 0) || (y == 6) || (x == 0) || (x == 6)) {
cout << "The King has escaped!" << endl;
done = true;
return;
}
try {
//first check if the King is NOT on the Throne, where he needs 4 Attackers to be captured.
if ((y != 3) && (x != 3)) {
//ordinary capture checking
if (((board.at(y + 1).at(x)->GetName() == " A ") && (board.at(y - 1).at(x)->GetName() == " A ")) ||
((board.at(y).at(x + 1)->GetName() == " A ") && (board.at(y).at(x - 1)->GetName() == " A "))) {
cout << "The King has been captured." << endl;
done = true;
return;
}
}
//else, check if he's been captured on his Throne
else if ((board.at(y + 1).at(x)->GetName() == " A ") && (board.at(y - 1).at(x)->GetName() == " A ") &&
(board.at(y).at(x + 1)->GetName() == " A ") && (board.at(y).at(x - 1)->GetName() == " A ")) {
cout << "The King has been captured." << endl;
done = true;
return;
}
}
catch (...) {
//DEBUG: cout << "nothing here." << endl;
}
}