-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathposition.cpp
More file actions
88 lines (77 loc) · 1.78 KB
/
position.cpp
File metadata and controls
88 lines (77 loc) · 1.78 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
#include "position.h"
#include <boost/functional/hash.hpp>
Position::Position(const int& x, const int& y) :
x(x), y(y)
{
}
void
Position::with_direction(const Direction& direction)
{
switch (direction)
{
case STAY:
return;
case NORTH:
x--;
return;
case SOUTH:
x++;
return;
case EAST:
y++;
return;
case WEST:
y--;
return;
}
/*
static const Position deltas[5] = {
Position(0,0), // STAY
Position(-1,0), // NORTH
Position(1,0), // SOUTH
Position(0,1), // EAST
Position(0,-1), // WEST
};
*/
}
bool
Position::next_to(const Position& position) const
{
const int& delta_x = x-position.x;
if (delta_x > 1 || delta_x < -1) return false;
const int& delta_y = y-position.y;
if (delta_y > 1 || delta_y < -1) return false;
if (delta_x == 0) return true;
if (delta_y == 0) return true;
return false;
}
Hash
hash_value(const Position& position)
{
Hash seed = 42;
boost::hash_combine(seed, position.x);
boost::hash_combine(seed, position.y);
return seed;
}
std::ostream&
operator<<(std::ostream& os, const Position& position)
{
os << "[" << position.x << "," << position.y << "]";
return os;
}
bool
operator==(const Position& position_aa, const Position& position_bb)
{
return position_aa.x == position_bb.x && position_aa.y == position_bb.y;
}
bool
operator!=(const Position& position_aa, const Position& position_bb)
{
return position_aa.x != position_bb.x || position_aa.y != position_bb.y;
}
bool
operator<(const Position& position_aa, const Position& position_bb)
{
if (position_aa.x != position_bb.x) return position_aa.x < position_bb.x;
return position_aa.y < position_bb.y;
}