-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.h
103 lines (89 loc) · 2.07 KB
/
board.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
#pragma once
#include <vector>
enum class TileColour {EMPTY, WHITE, BLACK};
/**
* Return given a tile colour, return its opposite.
* The opposite of EMPTY is EMPTY.
*/
inline TileColour oppositeColour(TileColour c)
{
TileColour opposite;
switch(c)
{
case TileColour::BLACK:
opposite = TileColour::WHITE;
break;
case TileColour :: WHITE:
opposite = TileColour::BLACK;
break;
default:
opposite = TileColour::EMPTY;
break;
}
return opposite;
}
/**
* Utility constexpr to convert the enum class value to its integer value. Useful for debugging purposes.
*/
template <typename Enumeration>
constexpr auto enum_as_integer(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
{
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}
/**
* The Hex game board.
* Contains the board dimensions and the TileColour (EMPTY / WHITE / BLACK) of
* every tile in the board.
*/
class Board
{
private:
/**
* The number of tiles on the side of the board
*/
int side_;
/**
* Total number of tiles: side*side
*/
int ntiles_;
/**
* Board represented as a flattened 2d vector of TileColours
*/
std::vector<TileColour> board_;
public:
/**
* Constructor
* @param s Board side length
*/
Board(int s): side_(s), ntiles_(s*s), board_(s*s, TileColour::EMPTY) {}
/**
* @return number of tiles on side of board
*/
int side() const
{
return side_;
}
/**
* @return number of tiles in board.
*/
int ntiles() const
{
return ntiles_;
}
/**
* Overload subscript operator to get/set individual tiles
*/
TileColour& operator[](int idx)
{
return board_[idx];
}
TileColour& operator() (unsigned row, unsigned col)
{
int idx = (row-1) * side_ + (col-1);
return board_[idx];
}
const TileColour& operator[](int idx) const
{
return board_[idx];
}
};