-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtGo.cpp
273 lines (247 loc) · 7.3 KB
/
HtGo.cpp
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
#include "HtGo.h"
#include <cstdio>
#include <stdexcept>
#include <iostream>
HexTorusGoMap::HexTorusGoMap(int radius) :
radius ( radius ),
coreMap ( radius )
{
int i, j, r;
for(r=1;r<=radius;r++) {
for(i=0;i<6;i++) {
for(j=0;j<r;j++) {
int x, y;
cartesianiseHexCoordinate( i, j, r, x, y );
coreMap.get(x,y).coreX = x;
coreMap.get(x,y).coreY = y;
}
}
}
coreMap.get(0,0).coreX = 0;
coreMap.get(0,0).coreY = 0;
}
void HexTorusGoMap::debugLabelCore(void) {
char buffer[1024];
int i, j, r, n = 0;
coreMap.get(0,0).positionalLabel = "0";
for(r=1;r<radius;r++) {
for(i=0;i<6;i++) {
for(j=0;j<r;j++) {
int x, y;
cartesianiseHexCoordinate( i, j, r, x, y );
sprintf(buffer, "%d", ++n );
coreMap.get(x,y).positionalLabel = std::string( buffer );
}
}
}
n = 0;
r = radius;
for(i=0;i<6;i++) {
for(j=0;j<r;j++) {
int x, y;
cartesianiseHexCoordinate( i, j, r, x, y );
using namespace std;
sprintf(buffer, "R%d", ++n );
coreMap.get(x,y).positionalLabel = std::string( buffer );
}
}
coreMap.getDefault().positionalLabel = "ERR";
}
HtGoTile& HexTorusGoMap::getIJR(int i,int j,int r) {
int x, y;
cartesianiseHexCoordinate( i, j, r, x, y );
return get( x, y );
}
HtGoTile& HexTorusGoMap::get(int x, int y) {
int guard = 100;
int i, j, r;
polariseHexCoordinate( x, y, i, j, r );
if( r < radius ||
( (r == radius) &&
( (i == 0 && j != 0) ||
(i == 1) ||
(i == 2) ) ) ) {
return coreMap.get( x, y );
}
if( r == radius ) switch( i ) {
case 0:
case 5:
return get( -x, y );
case 3:
return getIJR( 0, radius - j, r );
case 4:
return getIJR( 1, radius - j, r );
default:
throw std::logic_error( "illegal late-return state on rim of core hex" );
}
using namespace std;
y %= 6 * radius;
x %= 6 * radius;
x -= 6 * radius;
y -= 6 * radius;
do {
polariseHexCoordinate( x, y, i, j, r );
if( r <= radius ) {
return get(x,y);
}
if( y > 0 ) {
if( x > 0 ) {
x -= 3 * radius;
y -= 3 * radius;
} else {
x += 3 * radius;
y -= 3 * radius;
}
} else {
if( x > 0 ) {
x -= 3 * radius;
y += 3 * radius;
} else {
x += 3 * radius;
y += 3 * radius;
}
}
if( --guard <= 0 ) {
throw std::logic_error( "illegal state - probably looping infinitely");
}
} while(true);
}
void PointSet::add(const HtGoTile& tile) {
ips.insert( std::pair<int,int>( tile.coreX, tile.coreY ) );
}
void PointSet::remove(const HtGoTile& tile) {
InternalPointSet::iterator i = ips.find( std::pair<int,int>( tile.coreX, tile.coreY ) );
if( i != ips.end() ) {
ips.erase( i );
}
}
bool PointSet::has(const HtGoTile& tile) const {
InternalPointSet::const_iterator i = ips.find( std::pair<int,int>( tile.coreX, tile.coreY ) );
return i != ips.end();
}
HtGoTile& HexTorusGoMap::getNeighbourOf(int x,int y,int i) {
const static int dx[] = { 3, 0, -3, -3, 0, 3 },
dy[] = { 1, 2, 1, -1, -2, -1 };
return get(x + dx[i%6], y + dy[i%6]);
}
HtGoTile& HexTorusGoMap::get(std::pair<int,int> xy) {
return get( xy.first, xy.second );
}
HtGoTile& HexTorusGoMap::getNeighbourOf(const HtGoTile& tile, int i) {
return getNeighbourOf(tile.coreX, tile.coreY, i );
}
PointSet HexTorusGoMap::libertiesOf(const PointSet& group) {
PointSet rv;
for(PointSet::const_iterator i = group.begin(); i != group.end(); i++) {
const HtGoTile& tile = get( *i );
for(int i=0;i<6;i++) {
HtGoTile& nb = getNeighbourOf( tile, i );
if( nb.state == HtGoTile::BLANK ) {
rv.add( nb );
}
}
}
return rv;
}
PointSet HexTorusGoMap::groupOf(int x, int y) {
using namespace std;
HtGoTile& tile = get(x,y);
return groupOf( tile );
}
PointSet HexTorusGoMap::groupOf(const HtGoTile& tile) {
using namespace std;
PointSet rv, open;
open.add( tile );
rv.add( tile );
while( !open.empty() ) {
std::pair<int,int> coords = open.pop();
HtGoTile& tile = get( coords );
for(int i=0;i<6;i++) {
HtGoTile& nb = getNeighbourOf( tile, i );
if( nb.state == tile.state ) {
if( !rv.has( nb ) ) {
open.add( nb );
rv.add( nb );
}
}
}
}
return rv;
}
std::pair<int,int> PointSet::pop(void) {
InternalPointSet::iterator i = ips.begin();
if( i == ips.end() ) {
throw std::runtime_error( "popping from empty set" );
}
std::pair<int,int> rv = *i;
ips.erase( i );
return rv;
}
int HexTorusGoMap::removeGroup(const PointSet& xys) {
int rv = xys.size();
for(PointSet::const_iterator i = xys.begin(); i != xys.end(); i++) {
get( i->first, i->second ).state = HtGoTile::BLANK;
}
return rv;
}
int HexTorusGoMap::put(int x,int y, HtGoTile::TileState st) {
using namespace std;
int enemyCaptures = 0, selfCaptures = 0;
int singleCaptureX, singleCaptureY;
get(x,y).state = st;
for(int i=0;i<6;i++) {
HtGoTile& tile = getNeighbourOf( x, y, i );
if( tile.state == st ) continue;
if( tile.state == HtGoTile::BLANK ) continue;
PointSet group = groupOf( tile );
PointSet libs = libertiesOf( group );
if( libs.empty() ) {
singleCaptureX = tile.coreX;
singleCaptureY = tile.coreY;
enemyCaptures += removeGroup( group );
}
}
PointSet selfGroup = groupOf( x, y );
if( libertiesOf( selfGroup ).empty() ) {
selfCaptures += removeGroup( selfGroup );
}
if( enemyCaptures == 1 && selfCaptures == 0 ) {
koActive = true;
koCoreX = singleCaptureX;
koCoreY = singleCaptureY;
} else {
koActive = false;
}
return enemyCaptures;
}
HexTorusGoMap::HexTorusGoMap(const HexTorusGoMap& that) :
radius ( that.radius ),
coreMap ( that.coreMap ),
koActive ( that.koActive ),
koCoreX ( that.koCoreX ),
koCoreY ( that.koCoreY )
{
}
const HexTorusGoMap& HexTorusGoMap::operator=(const HexTorusGoMap& that) {
if( this != &that ) {
radius = that.radius;
coreMap = that.coreMap;
koActive = that.koActive;
koCoreX = that.koCoreX;
koCoreY = that.koCoreY;
}
return *this;
}
bool HexTorusGoMap::putWouldBeLegal(int x,int y, HtGoTile::TileState st) {
// TODO constify hax
if( st == HtGoTile::BLANK ) return false;
HtGoTile& tile = get(x,y);
if( tile.state != HtGoTile::BLANK ) return false;
if( koActive && koCoreX == tile.coreX && koCoreY == tile.coreY ) {
return false;
}
HexTorusGoMap copy = *this;
copy.put( x, y, st );
if( copy.get(x,y).state != st ) return false; // suicide is illegal
return true;
}