-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.cpp
67 lines (51 loc) · 1.62 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// jammon17@georgefox.edu
// Assignment 9
// 2019-04-24
#include <iostream>
#include "King.h"
#include "Square.h"
bool King::canMoveTo(Square& location) {
bool validMove = false;
Square* moveSquare = &location;
// get if the square is occupied
bool isOccupied = moveSquare->isOccupied();
// get the destination squares file and rank
int moveFile = moveSquare->getFile();
int moveRank = moveSquare->getRank();
// get the current locations file and rank
int thisFile = getLocation()->getFile();
int thisRank = getLocation()->getRank();
if (!isOccupied || moveSquare->getOccupant()->getColor() != getColor())
{
if (moveRank == thisRank + 1 || moveRank == thisRank - 1) {
if (thisFile == moveFile) {
validMove = true;
}
}
if (moveFile == thisFile + 1 || moveFile == thisFile - 1) {
if (thisRank == moveRank ) {
validMove = true;
}
}
// up and to the left
if (moveRank == thisRank + 1 && moveFile == thisFile - 1) {
validMove = true;
}
// up and to the right
if (moveRank == thisRank + 1 && moveFile == thisFile + 1) {
validMove = true;
}
// down and to the left
if (moveRank == thisRank - 1 && moveFile == thisFile - 1) {
validMove = true;
}
// down and to the right
if (moveRank == thisRank - 1 && moveFile == thisFile + 1) {
validMove = true;
}
}
return validMove;
}
void King::display() {
cout << getColor() + "K" << flush;
}