-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.h
160 lines (133 loc) · 3.31 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
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
/* Written by Todd Doucet. */
#pragma once
#include <cstring>
#include <cstdint>
#include <cassert>
enum color_t { white = 0, black = 1};
inline color_t opponentOf(color_t color)
{
return (color_t) ! (int) color;
}
inline int opponentPoint(int x)
{
return 25 - x;
}
class board
{
public:
int d1() const { return die_1; }
int d2() const { return die_2; }
int diceInCup() const { return d1() < 1; }
void setDice(int _d1, int _d2){ die_1 = _d1; die_2 = _d2;}
void pickupDice()
{
on_roll = opponentOf(on_roll);
setDice(0, 0);
}
void setRoller(color_t r) { on_roll = r; }
color_t onRoll() const { return on_roll;}
color_t notOnRoll() const { return opponentOf(on_roll);}
inline int checkersOnPoint(color_t color, int point) const
{
return thePoints[color][point];
}
inline int myCheckersOnMyPoint(int point) const
{
return thePoints[ onRoll() ][point];
}
inline int myCheckersOnHisPoint(int point) const
{
return thePoints[ onRoll() ][ opponentPoint(point) ];
}
inline int hisCheckersOnMyPoint(int point) const
{
return thePoints[ notOnRoll() ][ opponentPoint(point) ];
}
inline int hisCheckersOnHisPoint(int point) const
{
return thePoints[ notOnRoll() ][ point ];
}
int moveChecker(color_t color, int from, int to)
{
if ( (to == 0) || (to == 25) )
{
_moveChecker(color, from, to);
return 0;
}
int op = opponentPoint(to);
int hit = 0;
while(checkersOnPoint(opponentOf(color), op))
{
putOnBar(notOnRoll(), op);
hit++;
}
_moveChecker(color, from, to);
return hit;
}
int moveMyChecker(int from, int to)
{
return moveChecker(onRoll(), from, to);
}
void putOnBar(color_t color, int pt)
{
moveChecker(color, pt, 25);
}
void removeFromBar(color_t color, int pt)
{
moveChecker(color, 25, pt);
}
int checkersOnBar(color_t color) const
{
return checkersOnPoint(color, 25);
}
int highestChecker(color_t color) const
{
int i;
for (i = 25; i; i--)
if (checkersOnPoint(color, i))
break;
return i;
}
int pipCount(color_t color) const
{
return pip[color];
}
bool operator==(const board& rhs) const
{
return memcmp(this, &rhs, sizeof(board)) == 0;
}
void clearBoard()
{
for (int i = 0; i <= 25; i++)
{
thePoints[0][i] = 0;
thePoints[1][i] = 0;
}
pip[0] = pip[1] = 0;
setRoller(white);
setDice(0, 0);
thePoints[0][0] = 15;
thePoints[1][0] = 15;
}
board() { clearBoard(); }
static const char *colorname(color_t color)
{
assert (color == white || color == black);
if (color == white)
return "White";
else if (color == black)
return "Black";
return "";
}
private:
int8_t thePoints[2][26];
int32_t pip[2];
int32_t die_1, die_2;
color_t on_roll;
void _moveChecker(color_t color, int from, int to)
{
thePoints[color][from]--;
thePoints[color][to]++;
pip[color] += (to - from);
}
};