-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrook.cpp
54 lines (48 loc) · 1.18 KB
/
rook.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
//
// Created by Admin on 25/03/2023.
//
#include "rook.h"
bool rook::is_obstruct(const board& game_board, position target) const
{
if (target.x == pos.x) {
int y = pos.y;
while (abs(y - target.y) > 1) {
if (y < target.y) {
y++;
} else {
y--;
}
if (game_board.find({pos.x, y}).has_value()) {
return true;
}
}
} else {
int x = pos.x;
while (abs(x - target.x) > 1) {
if (x < target.x) {
x++;
} else {
x--;
}
if (game_board.find({x, pos.y}).has_value()) {
return true;
}
}
}
return false;
}
bool rook::is_valid_move(const board& game_board, position target) const
{
//Rook can move only straight
return !is_obstruct(game_board, target) && (target.x == pos.x || target.y == pos.y);
}
bool rook::is_valid_capture(const board& game_board, position target) const
{
return is_valid_move(game_board, target);
}
rook *rook::clone() const {
return new rook(*this);
}
void rook::move_cleanup() {
has_moved = true;
}