-
Notifications
You must be signed in to change notification settings - Fork 0
/
Position.h
366 lines (313 loc) · 12.3 KB
/
Position.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#pragma once
#include "Common.h"
#include <ostream>
#include <tuple>
#include <sstream>
#include <cmath>
class Position
{
public:
Position() noexcept : x_(0), y_(0) {}
Position(const int x_value, const int y_value) : x_(x_value), y_(y_value) {}
Position(const std::string& position) : Position(x(position.substr(0, 1)), y(position.substr(1, 1))) {}
~Position() = default;
virtual Position& operator=(const Position&) = default;
virtual const int x() const { return x_; }
virtual const int y() const { return y_; }
virtual const int x(const int x_value) { x_ = x_value; return x_; }
virtual const int y(const int y_value) { y_ = y_value; return y_; }
virtual const int x(const std::string&);
virtual const int y(const std::string&);
Position distance(const Position& position) const;
int length() const { return std::lround(std::hypot(x_, y_)); }
friend std::ostream& operator<<(std::ostream& out, const Position& position)
{
out << "(" << position.x_ << ", " << position.y_ << ")";
return out;
}
friend bool operator<(const Position& lhs, const Position& rhs)
{
return std::tie(lhs.x_, lhs.y_) < std::tie(rhs.x_, rhs.y_); // keep the same order
}
friend bool operator==(const Position& lhs, const Position& rhs) {
return std::tie(lhs.x_, lhs.y_) == std::tie(rhs.x_, rhs.y_);
}
virtual Position& operator+=(const Position& rhs)
{
x_ += rhs.x_;
y_ += rhs.y_;
return *this;
}
friend Position operator+(Position lhs, const Position& rhs)
{
lhs += rhs;
return lhs;
}
virtual Position& operator/=(const int d)
{
x_ /= d;
y_ /= d;
return *this;
}
friend Position operator/(Position lhs, const int rhs)
{
lhs /= rhs;
return lhs;
}
private:
int x_;
int y_;
};
inline bool operator> (const Position& lhs, const Position& rhs) { return rhs < lhs; }
inline bool operator<=(const Position& lhs, const Position& rhs) { return !(lhs > rhs); }
inline bool operator>=(const Position& lhs, const Position& rhs) { return !(lhs < rhs); }
inline bool operator!=(const Position& lhs, const Position& rhs) { return !(lhs == rhs); }
class BoardPosition : public Position
{
public:
BoardPosition() noexcept : Position{ 1, 1 } {}
BoardPosition(const int x_value, const int y_value) { x(x_value); y(y_value); }
BoardPosition(const std::string& position) : Position{ position.substr(0, 2) } { x(x()); y(y()); }
BoardPosition(const Position& p) { x(p.x()); y(p.y()); }
~BoardPosition() = default;
virtual BoardPosition& operator=(const BoardPosition&) = default;
const int x() const override { return Position::x(); }
const int y() const override { return Position::y(); }
const int xMin() const { return 1; }
const int yMin() const { return 1; }
const int xMax() const { return 9; }
const int yMax() const { return 9; }
const int x(const int x) override;
const int y(const int y) override;
const int x(const std::string&) override;
const int y(const std::string&) override;
void position(const int x_value, const int y_value) { x(x_value); y(y_value); }
Position position() const { return (Position)*this; }
virtual void restore(const std::string&);
friend std::ostream& operator<<(std::ostream& out, const BoardPosition& position)
{
out << char('a' + position.x() - 1);
out << position.y();
return out;
}
friend bool operator<(const BoardPosition& lhs, const BoardPosition& rhs)
{
return (Position)lhs < (Position)rhs;
}
friend bool operator==(const BoardPosition& lhs, const BoardPosition& rhs) {
return (Position)lhs == (Position)rhs;
}
BoardPosition& operator+=(const Position& rhs)
{
x(x() + rhs.x());
y(y() + rhs.y());
return *this;
}
friend BoardPosition operator+(BoardPosition lhs, const Position& rhs)
{
lhs += rhs;
return lhs;
}
BoardPosition& operator/=(const int d)
{
x(x() / d);
y(y() / d);
return *this;
}
friend BoardPosition operator/(BoardPosition lhs, const int rhs)
{
lhs /= rhs;
return lhs;
}
private:
virtual bool validX(const int x);
virtual bool validY(const int y);
};
inline bool operator> (const BoardPosition& lhs, const BoardPosition& rhs) { return rhs < lhs; }
inline bool operator<=(const BoardPosition& lhs, const BoardPosition& rhs) { return !(lhs > rhs); }
inline bool operator>=(const BoardPosition& lhs, const BoardPosition& rhs) { return !(lhs < rhs); }
inline bool operator!=(const BoardPosition& lhs, const BoardPosition& rhs) { return !(lhs == rhs); }
class PawnPosition : public BoardPosition
{
public:
PawnPosition() = default;
PawnPosition(const BoardPosition& position, const PlayerName& playerName, const Color& color = Color::none, const PawnStyle& style = PawnStyle::none) : BoardPosition{ position }, playerName_{ playerName }, color_{ color }, pawn_style_{ style } {}
PawnPosition(const unsigned int x, const unsigned int y, const PlayerName& playerName, const Color& color = Color::none, const PawnStyle& style = PawnStyle::none) : BoardPosition{ x, y }, playerName_{ playerName }, color_{ color }, pawn_style_{ style } {}
PawnPosition(const std::string & playerPosition, const PlayerName& playerName = "", const Color& color = Color::none, const PawnStyle& style = PawnStyle::none) : BoardPosition(playerPosition.substr(0, 2)), playerName_(playerName), color_{ color }, pawn_style_{ style } {}
~PawnPosition() = default;
PawnPosition& operator=(const PawnPosition&);
void playerName(const PlayerName& playerName) { playerName_ = playerName; }
const PlayerName& playerName() const { return playerName_; }
void color(const Color& color) { color_ = color; }
const Color& color() const { return color_; }
void pawnStyle(PawnStyle style) { pawn_style_ = style; }
const PawnStyle& pawnStyle() const { return pawn_style_; }
void restore(const std::string&) override;
void restore(const std::string&, const PlayerName&);
friend std::ostream& operator<<(std::ostream& out, const PawnPosition& position)
{
out << (BoardPosition)position;
if (!position.playerName_.empty())
out << " (" << position.playerName_ << ") ";
return out;
}
private:
PlayerName playerName_;
Color color_;
PawnStyle pawn_style_;
};
class WallPosition : public BoardPosition
{
public:
enum class Direction { horizontal, vertical };
WallPosition() noexcept : direction_{ Direction::horizontal } {};
WallPosition(const int x_value, const int y_value) : direction_{ Direction::horizontal } { x(x_value); y(y_value); }
WallPosition(const int x, const int y, const Direction& direction) : BoardPosition(x, y), direction_(direction) {}
WallPosition(const int x, const int y, const std::string& text) : BoardPosition(x, y), direction_(direction(text)) {}
WallPosition(const std::string & wallPosition) : BoardPosition(wallPosition.substr(0, 2)), direction_(direction(wallPosition.substr(2, 1))) {}
WallPosition(const BoardPosition&, const BoardPosition&, const bool);
~WallPosition() = default;
WallPosition& operator=(const WallPosition&) = default;
const int x() const { return BoardPosition::x(); }
const int y() const { return BoardPosition::y(); }
const int x(const int x);
const int y(const int y);
const int x(const std::string & x);
const int y(const std::string & y);
void direction(const Direction& direction) { direction_ = direction; }
const Direction direction(const std::string&);
const Direction direction() const { return direction_; }
const Direction oppositeDirection() const { return direction_ == Direction::horizontal ? Direction::vertical : Direction::horizontal; }
WallPosition shiftedWallPosition(Position) const;
void restore(const std::string&) override;
friend std::ostream& operator<<(std::ostream& out, const WallPosition& position)
{
out << (BoardPosition)position;
if (position.direction_ == WallPosition::Direction::horizontal)
out << "h";
if (position.direction_ == WallPosition::Direction::vertical)
out << "v";
return out;
}
friend bool operator==(const WallPosition& lhs, const WallPosition& rhs) {
return ((Position)lhs == (Position)rhs) && (lhs.direction_ == rhs.direction_);
}
WallPosition& operator+=(WallPosition rhs)
{
(Position)rhs += (Position)*this;
if (validX(rhs.x()) && validY(rhs.y()))
{
x(rhs.x());
y(rhs.y());
}
else
{
std::ostringstream oss;
oss << "Position " << rhs << " is out of board.";
throw std::out_of_range(oss.str());
}
return *this;
}
friend WallPosition operator+(WallPosition lhs, const WallPosition& rhs)
{
lhs += rhs;
return lhs;
}
WallPosition& operator+=(Position rhs)
{
rhs += (Position)*this;
if (validX(rhs.x()) && validY(rhs.y()))
{
x(rhs.x());
y(rhs.y());
}
else
{
std::ostringstream oss;
oss << "Position " << rhs << " is out of board.";
throw std::out_of_range(oss.str());
}
return *this;
}
friend WallPosition operator+(WallPosition lhs, const Position& rhs)
{
lhs += rhs;
return lhs;
}
private:
Direction direction_;
bool validX(const int x);
bool validY(const int y);
};
inline bool operator!=(const WallPosition& lhs, const WallPosition& rhs) { return !(lhs == rhs); }
class Move
{
public:
enum class Type { pawn, wall, illegal_pawn, illegal_wall, surrend, none };
friend std::ostream& operator<<(std::ostream& out, const Type& type)
{
switch (type)
{
case Move::Type::pawn:
case Move::Type::wall:
break;
case Move::Type::illegal_pawn:
case Move::Type::illegal_wall:
out << "(illegal)";
break;
case Move::Type::surrend:
out << "(resigns)";
break;
}
return out;
}
Move() noexcept : type_{ Type::none }, player_{}, wall_{} {}
Move(const PawnPosition &player) : type_{ Type::pawn }, player_{ player }, wall_{} {}
Move(const WallPosition &wall) : type_{ Type::wall }, player_{}, wall_{ wall } {}
Move(const std::string &text) :
type_{ type(text) },
player_{ type_ == Type::pawn || type_ == Type::illegal_pawn ? PawnPosition(text) : PawnPosition() },
wall_{ type_ == Type::wall || type_ == Type::illegal_wall ? WallPosition(text) : WallPosition() }
{}
void restore(const std::string &move);
const Type& type(const std::string &text);
void setIllegal();
const Type& type() const { return type_; }
void playerName(const PlayerName& playerName) { player_.playerName(playerName); }
const PawnPosition& pawn() const { return player_; }
const WallPosition& wall() const { return wall_; }
friend std::ostream& operator<<(std::ostream& out, const Move& move)
{
switch (move.type())
{
case Move::Type::pawn:
case Move::Type::illegal_pawn:
out << (BoardPosition)move.player_ << move.type_; // casted to mask player name
break;
case Move::Type::wall:
case Move::Type::illegal_wall:
out << move.wall_ << move.type_;
break;
case Move::Type::surrend:
out << move.type_;
break;
}
return out;
}
friend bool operator==(const Move& lhs, const Move& rhs) {
auto pawnType = ((lhs.type_ == Move::Type::pawn) || (lhs.type_ == Move::Type::illegal_pawn));
auto pawnEqual = (lhs.player_ == rhs.player_);
auto wallType = ((lhs.type_ == Move::Type::wall) || (lhs.type_ == Move::Type::illegal_wall));
auto wallEqual = (lhs.wall_ == rhs.wall_);
return (lhs.type_ == rhs.type_ && (
(pawnType && pawnEqual)
|| (wallType && pawnEqual)
|| (lhs.type_ == Move::Type::surrend)
|| (lhs.type_ == Move::Type::none)
));
}
private:
Type type_;
PawnPosition player_;
WallPosition wall_;
};