-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.cpp
More file actions
106 lines (96 loc) · 2.85 KB
/
Knight.cpp
File metadata and controls
106 lines (96 loc) · 2.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "Knight.h"
//------------------------------------------------------------------------------
const std::string Knight::UTF_SYMBOL = "\u2658";
//------------------------------------------------------------------------------
void Knight::getAccessibleFields(
Position initial_position,
Field board[BOARD_DIMENSIONS][BOARD_DIMENSIONS],
bool access_map[BOARD_DIMENSIONS][BOARD_DIMENSIONS])
{
int src_row = initial_position.row;
int src_col = initial_position.col;
int dest_row;
int dest_col;
bool player_is_white = board[src_row][src_col].getPiece()->isWhite();
// 8 possible positions (clockwise)
// move up-right
dest_row = src_row - LONG_HOP_DISTANCE;
dest_col = src_col + SHORT_HOP_DISTANCE;
if(dest_row >= 0 && dest_col <= BOARD_DIMENSIONS)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move right-up
dest_row = src_row - SHORT_HOP_DISTANCE;
dest_col = src_col + LONG_HOP_DISTANCE;
if(dest_row >= 0 && dest_col <= BOARD_DIMENSIONS)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move right-down
dest_row = src_row + SHORT_HOP_DISTANCE;
dest_col = src_col + LONG_HOP_DISTANCE;
if(dest_row <= BOARD_DIMENSIONS && dest_col <= BOARD_DIMENSIONS)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move down-right
dest_row = src_row + LONG_HOP_DISTANCE;
dest_col = src_col + SHORT_HOP_DISTANCE;
if(dest_row <= BOARD_DIMENSIONS && dest_col <= BOARD_DIMENSIONS)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move down-left
dest_row = src_row + LONG_HOP_DISTANCE;
dest_col = src_col - SHORT_HOP_DISTANCE;
if(dest_row <= BOARD_DIMENSIONS && dest_col >= 0)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move left-down
dest_row = src_row + SHORT_HOP_DISTANCE;
dest_col = src_col - LONG_HOP_DISTANCE;
if(dest_row <= BOARD_DIMENSIONS && dest_col >= 0)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move left-up
dest_row = src_row - SHORT_HOP_DISTANCE;
dest_col = src_col - LONG_HOP_DISTANCE;
if(dest_row >= 0 && dest_col >= 0)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
// move up-left
dest_row = src_row - LONG_HOP_DISTANCE;
dest_col = src_col - SHORT_HOP_DISTANCE;
if(dest_row >= 0 && dest_col >= 0)
{
if(board[dest_row][dest_col].isAccessible(player_is_white))
{
access_map[dest_row][dest_col] = true;
}
}
}