-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.cpp
More file actions
39 lines (33 loc) · 1.05 KB
/
Knight.cpp
File metadata and controls
39 lines (33 loc) · 1.05 KB
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
#include "Knight.h"
#include <vector>
#include <iostream>
#include "Position.h"
#include "Chessboard.h"
// offset matrix for possible knight moves
const int row_change[] = { -2, -2, -1, -1, +1, +1, +2, +2 };
// offset matrix for possible knight moves
const int col_change[] = { -1, +1, -2, +2, -2, +2, +1, -1 };
Knight::Knight(bool white) : Chessman('n', white)
{
}
void Knight::calculate_possible_moves(const int row, const int col, const Chessboard& cb) const
{
valid_moves->clear();
capture_moves->clear();
int change_array_size = sizeof(row_change) / sizeof(int);
int size = cb.get_size();
for (int i = 0; i < size; i++) {
int new_row = row + row_change[i % change_array_size];
int new_col = col + col_change[i % change_array_size];
if (cb.is_out_of_bounds(new_row, new_col))
continue;
// free square
if (cb.can_pass_over(new_row, new_col)) {
valid_moves->push_back(Position(new_row, new_col));
}
// enemy on square
else if (cb.can_capture_on(new_row, new_col, is_white())) {
capture_moves->push_back(Position(new_row, new_col));
}
}
}