-
Notifications
You must be signed in to change notification settings - Fork 0
/
rook.cc
59 lines (48 loc) · 1.45 KB
/
rook.cc
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
#include <iostream>
#include <sstream>
#include "rook.h"
Rook::Rook(char colour, int row, int col, std::vector<std::vector<Piece *>> &board): Piece('r', colour, row, col, board) {}
bool Rook::canMove(int endRow, int endCol) const {
// ensure not remaining in same position
if (row == endRow && col == endCol) {
return false;
}
// ensure new position doesn't contain an allied piece
if (board[endRow][endCol]->getColour() == getColour()) {
return false;
}
// moving vertically
if (col == endCol) {
// set distances + directions
int rowDistance = endRow - row;
int rowDirection = 1;
if (rowDistance < 0) {
rowDirection = -1;
}
// ensure clear path
for (int i = 1; i < abs(rowDistance); ++i) {
if (board[row + i*rowDirection][col]->getType() != 'e') {
return false;
}
}
return true;
}
// moving horizontally
if (row == endRow) {
// set distances + directions
int colDistance = endCol - col;
int colDirection = 1;
if (colDistance < 0) {
colDirection = -1;
}
// ensure clear path
for (int i = 1; i < abs(colDistance); ++i) {
if (board[row][col + i*colDirection]->getType() != 'e') {
return false;
}
}
return true;
}
// else rook move isn't valid
return false;
}