-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knight.cpp
44 lines (31 loc) · 1.02 KB
/
Knight.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
// jammon17@georgefox.edu
// Assignment 9
// 2019-04-24
#include <iostream>
#include "Knight.h"
#include "Board.h"
bool Knight::canMoveTo(Square& location) {
bool validMove = false;
Square* moveSquare = &location;
Board* board = Board::getInstance();
// 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 (abs(thisRank - moveRank) == 2 && abs(thisFile - moveFile) == 1) {
validMove = true;
}
if (abs(thisFile - moveFile) == 2 && abs(thisRank - moveRank) == 1) {
validMove = true;
}
}
return validMove;
}
void Knight::display() {
cout << getColor() + "N" << flush;
}