-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhexBoard.h
90 lines (70 loc) · 2.66 KB
/
hexBoard.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
#ifndef HEXBOARD_H
#define HEXBOARD_H
#include <cstdlib>
#include <vector>
using namespace std;
enum Space {P_EMPTY, P_BLACK, P_WHITE};
class hexBoard {
public:
// Constructor adds 4 extra nodes to act as board edge pseudonodes.
hexBoard(int thisSideLength): sideLength(thisSideLength){
spaces = new vector<Space>(sideLength * sideLength, P_EMPTY);
};
// Copy constructor
hexBoard(hexBoard const &oldBoard): sideLength(oldBoard.sideLength){
spaces = new vector<Space>(*(oldBoard.spaces));
}
virtual ~hexBoard() {
delete spaces;
}
// Board manipulating functions
inline int getSize() const {return spaces->size();}
int getIndex(unsigned int x, unsigned int y) const;
pair<int, int> getCoords(unsigned int index) const;
Space getSpace(unsigned int index) const;
Space getSpace(unsigned int x, unsigned int y) const;
void setSpace(unsigned int index, Space sp);
void setSpace(unsigned int x, unsigned int y, Space sp);
bool isValidMove(unsigned int x, unsigned int y, bool piRule = false) const;
bool isValidMove(unsigned int index, bool piRule = false) const;
bool isValidSpace(unsigned int index) const;
bool isValidSpace(unsigned int x, unsigned int y) const;
const unsigned int sideLength;
private:
vector<Space>* spaces;
};
inline bool hexBoard::isValidSpace(unsigned int index) const {
return index < spaces->size();
}
inline bool hexBoard::isValidSpace(unsigned int x, unsigned int y) const {
return x < sideLength && y < sideLength;
}
inline bool hexBoard::isValidMove(unsigned int x, unsigned int y, bool piRule) const {
return isValidSpace(x, y) && (getSpace(x, y) == P_EMPTY || piRule);
}
inline bool hexBoard::isValidMove(unsigned int index, bool piRule) const {
return isValidSpace(index) && (getSpace(index) == P_EMPTY || piRule);
}
inline int hexBoard::getIndex(unsigned int x, unsigned int y) const {
return sideLength*y + x;
}
inline pair<int, int> hexBoard::getCoords(unsigned int index) const {
int x = index % sideLength;
int y = index / sideLength;
return pair<int, int>(x, y);
}
inline Space hexBoard::getSpace(unsigned int index) const {
// assert(isValidSpace(index));
return (*spaces)[index];
}
inline Space hexBoard::getSpace(unsigned int x, unsigned int y) const {
return getSpace(getIndex(x, y));
}
inline void hexBoard::setSpace(unsigned int index, Space sp) {
// assert(isValidSpace(index));
(*spaces)[index] = sp;
}
inline void hexBoard::setSpace(unsigned int x, unsigned int y, Space sp) {
setSpace(getIndex(x, y), sp);
}
#endif /* HEXBOARD_H */