-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimensions.cpp
162 lines (125 loc) · 3.49 KB
/
dimensions.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "dimensions.hpp"
DIRECTION operator++(DIRECTION& direction) {
return direction = (DIRECTION)(std::underlying_type<DIRECTION>::type(direction) + 1);
}
DIRECTION operator*(DIRECTION direction) {
return direction;
}
DIRECTION begin(DIRECTION direction) {
return DIRECTION::First;
}
DIRECTION end(DIRECTION direction) {
DIRECTION d = DIRECTION::Last;
return ++d;
}
// This function is done with the row and position as refers to the board; there is no
// translation to what would be used to reference the index of the board array. So if
// we're at row 1, column 1, that's the upper-left corner and so if we want the NW values,
// that would be 0, 0, which would be off the board (because when we translate to the board
// array, that would be -1 which is not a valid array position).
tuple<bool, ROW, COLUMN> getOffsetPosition(ROW row, COLUMN column, DIRECTION direction) {
int offsetRow = to_integral(row);
int offsetCol = to_integral(column);
bool isValidPosition = true;
switch (direction) {
case DIRECTION::NW:
offsetRow -= 1;
offsetCol -= 1;
break;
case DIRECTION::N:
offsetRow -= 1;
break;
case DIRECTION::NE:
offsetRow -= 1;
offsetCol += 1;
break;
case DIRECTION::W:
offsetCol -= 1;
break;
case DIRECTION::E:
offsetCol += 1;
break;
case DIRECTION::SW:
offsetRow += 1;
offsetCol -= 1;
break;
case DIRECTION::S:
offsetRow += 1;
break;
case DIRECTION::SE:
offsetRow += 1;
offsetCol += 1;
break;
}
if (offsetRow <= 0 || offsetCol <= 0) {
isValidPosition = false;
}
else if (offsetRow > BOARD_DIMENSION || offsetCol > BOARD_DIMENSION) {
isValidPosition = false;
}
return make_tuple(isValidPosition, static_cast<ROW>(offsetRow), static_cast<COLUMN>(offsetCol));
}
string getDirectionDescription(DIRECTION direction) {
string desc;
switch (direction) {
case DIRECTION::NW:
desc = "NW";
break;
case DIRECTION::N:
desc = "N";
break;
case DIRECTION::NE:
desc = "NE";
break;
case DIRECTION::W:
desc = "W";
break;
case DIRECTION::E:
desc = "E";
break;
case DIRECTION::SW:
desc = "SW";
break;
case DIRECTION::SE:
desc = "SE";
break;
case DIRECTION::S:
desc = "S";
break;
}
return desc;
}
string getBoardPositionDescription(ROW row, COLUMN column) {
string pos;
switch (column) {
case COLUMN::C_A:
pos = "A";
break;
case COLUMN::C_B:
pos = "B";
break;
case COLUMN::C_C:
pos = "C";
break;
case COLUMN::C_D:
pos = "D";
break;
case COLUMN::C_E:
pos = "E";
break;
case COLUMN::C_F:
pos = "F";
break;
case COLUMN::C_G:
pos = "G";
break;
case COLUMN::C_H:
pos = "H";
break;
case COLUMN::C_INVALID:
pos = "INVALID";
break;
}
pos.append(std::to_string(to_integral(row)));
return pos;
}