-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySecondChessman.cpp
More file actions
56 lines (47 loc) · 1.75 KB
/
MySecondChessman.cpp
File metadata and controls
56 lines (47 loc) · 1.75 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "MySecondChessman.h"
#include <vector>
#include <iostream>
#include "Position.h"
#include "Chessboard.h"
// offset matrix for possible king moves
const int row_change[] = { -1, 0, 1, -1, 1, -1, 0, +1 };
// offset matrix for possible king moves
const int col_change[] = { -1, -1, -1, +0, +0, +1, +1, +1 };
const int find_valid_move_cap = 5;
MySecondChessman::MySecondChessman(bool white) : Chessman('z', white)
{
}
void MySecondChessman::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);
// this chessman can capture in every direction
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;
auto man = cb(new_row, new_col);
if (man == nullptr)
continue;
// enemy on square
if (man->is_white() != is_white()) {
capture_moves->push_back(Position(new_row, new_col));
}
}
// this chessman can move only in one direction if it is not a capture move
// the movement pattern changes every turn
int counter = 0;
do
{
int possible_random_movement_index = rand() % change_array_size;
int new_row = row + row_change[possible_random_movement_index];
int new_col = col + col_change[possible_random_movement_index];
if (!cb.is_out_of_bounds(new_row, new_col) && cb.can_land_on(new_row, new_col, is_white())) {
valid_moves->push_back(Position(new_row, new_col));
}
// we try to find a move in case this valid move would be on a friendly figures position
} while (valid_moves->size() == 0 && counter++ < find_valid_move_cap);
}