-
Notifications
You must be signed in to change notification settings - Fork 1
/
position_struct.h
122 lines (79 loc) · 2.64 KB
/
position_struct.h
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef POSITION_CLASS_DECLARATION
#define POSITION_CLASS_DECLARATION
// Board diminsion
#define BOARD_SIZE 8
#define BOARD_FIRST_X 0
#define BOARD_LAST_X 7
#define BOARD_FIRST_Y 0
#define BOARD_LAST_Y 7
typedef int PositionRelativeDirection;
#define RIGHT_RELATIVE_DIRECTION 0
#define UPPER_RIGHT_RELATIVE_DIRECTION 1
#define UP_RELATIVE_DIRECTION 2
#define UPPER_LEFT_RELATIVE_DIRECTION 3
#define LEFT_RELATIVE_DIRECTION 4
#define DOWN_LEFT_RELATIVE_DIRECTION 5
#define DOWN_RELATIVE_DIRECTION 6
#define DOWN_RIGHT_RELATIVE_DIRECTION 7
#define INVALID_RELATIVE_DIRECTION -1
#define BOARD_DIRECTIONS_NUM 8
const int X_STEP_IN_DIRECTION [BOARD_DIRECTIONS_NUM] = {0, -1, -1, -1, 0, 1, 1, 1};
const int Y_STEP_IN_DIRECTION [BOARD_DIRECTIONS_NUM] = {1, 1, 0, -1, -1, -1, 0, 1};
class Position {
public:
int x, y;
Position(int x = 0, int y = 0) : x(x), y(y) {
}
bool operator== (Position p) {
return (p.x == x && p.y == y);
}
bool operator!= (Position p) {
return !this->operator==(p);
}
Position& operator++ () {
if (y+1 == BOARD_SIZE) {
x ++;
y = 0;
} else {
y++;
}
return *this;
}
bool isOutOfBoard() const {
return (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE);
}
PositionRelativeDirection getDirectionFrom(Position pivotPosition) const {
if (x == pivotPosition.x && y < pivotPosition.y) {
return RIGHT_RELATIVE_DIRECTION;
}
if (x > pivotPosition.x && y < pivotPosition.y) {
return UPPER_RIGHT_RELATIVE_DIRECTION;
}
if (x > pivotPosition.x && y == pivotPosition.y) {
return UP_RELATIVE_DIRECTION;
}
if (x > pivotPosition.x && y > pivotPosition.y) {
return UPPER_LEFT_RELATIVE_DIRECTION;
}
if (x == pivotPosition.x && y > pivotPosition.y) {
return LEFT_RELATIVE_DIRECTION;
}
if (x < pivotPosition.x && y > pivotPosition.y) {
return DOWN_LEFT_RELATIVE_DIRECTION;
}
if (x < pivotPosition.x && y == pivotPosition.y) {
return DOWN_RELATIVE_DIRECTION;
}
if (x < pivotPosition.x && y < pivotPosition.y) {
return DOWN_RIGHT_RELATIVE_DIRECTION;
}
return INVALID_RELATIVE_DIRECTION;
}
void moveOneStepInDirection(PositionRelativeDirection direction) {
if (direction != INVALID_RELATIVE_DIRECTION) {
x += X_STEP_IN_DIRECTION[direction];
y += Y_STEP_IN_DIRECTION[direction];
}
}
};
#endif // POSITION_CLASS_DECLARARION