-
Notifications
You must be signed in to change notification settings - Fork 5
/
game.cpp
540 lines (449 loc) · 14.5 KB
/
game.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* Implementation of game class, the AChecker
*/
#include "headers/game.h"
Game::Game()
:updated(true), moveCount(0)
{
}
// Copy constructor, can be used in AI. Note that this will not update the
// areas. You shouldn't be copying this and then immediately calling
// connectable() a whole bunch of times. Call those immediate connectable calls
// on the object you copied from. After copying this and adding to it do you
// want to update the areas and then check connectable() again.
Game::Game(const Game& g)
:updated(false), nodes(g.nodes.size()), lines(g.lines.size())
{
copy(g);
}
Game& Game::operator=(const Game& g)
{
cleanup();
updated = false;
nodes = vector<Node*>(g.nodes.size());
lines = vector<Line*>(g.lines.size());
copy(g);
return *this;
}
void Game::copy(const Game& g)
{
// newLines[oldAddress] = newAddress
map<Line*, Line*> newLines;
map<Node*, Node*> newNodes;
// Copy all lines
for (int i = 0; i < g.lines.size(); ++i)
{
Line* line = new Line(*g.lines[i]);
lines[i] = line;
newLines[g.lines[i]] = line;
}
// Recreate nodes
for (int i = 0; i < g.nodes.size(); ++i)
{
Node* node = new Node(g.nodes[i]->loci);
nodes[i] = node;
newNodes[g.nodes[i]] = node;
}
// Recreate node connections
for (int i = 0; i < g.nodes.size(); ++i)
{
Node* node = nodes[i];
for (int j = 0; j < 3; ++j)
{
if (g.nodes[i]->connections[j].exists())
{
node->addConnection(Connection(
newLines[g.nodes[i]->connections[j].line],
newNodes[g.nodes[i]->connections[j].dest]));
}
}
}
}
void Game::updateAreas()
{
Connection* nodeConns;
// We have once again updated the area
updated = true;
clearAreas();
// When we unique the areasets, we need a catch-all one
areasets.push_back(&defaultAreaset);
// Find all Circuits/areas
for(int i=0;i<nodes.size();i++)
nodes[i]->walk(areas);
//create and apply area sets to each node
for(int i=0;i<nodes.size();i++)
{
if(nodes[i]->dead())
continue;
Areaset* tempSets[2];
tempSets[0] = new Areaset();
tempSets[1] = new Areaset();
nodeConns = nodes[i]->getConnAddr();
Coord original = nodes[i]->getLoci();
/*
* this if statement is only true if on a border, since conn 1
* would have to be non null, and dead eliminated dead node
*/
if(nodeConns[1].exists())
{
if(nodes[i]->vertical())
{
for(int j=0;j<areas.size();j++)
{
// If its above added to the above area set
if (isInArea(*areas[j],Coord(original.x-1, original.y)))
tempSets[0]->push_back(areas[j]);
// If it is below then added to the below area set
if (isInArea(*areas[j],Coord(original.x+1, original.y)))
tempSets[1]->push_back(areas[j]);
}
}
else
{
for(int j=0;j<areas.size();j++)
{
// If its left added to the left area set
if (isInArea(*areas[j],Coord(original.x, original.y-1)))
tempSets[0]->push_back(areas[j]);
// If it is right then added to the right area set
if (isInArea(*areas[j],Coord(original.x, original.y+1)))
tempSets[1]->push_back(areas[j]);
}
}
}
else // A node with either one or no connections
{
for(int j=0;j<areas.size();j++)
{
if(isInArea(*areas[j],original))
{
// In "both directions" we're in this area. If we don't do
// this for both [0] and [1], we're always in the default
// area and can connect to points outside of this area if
// one of their areas is the default area.
tempSets[0]->push_back(areas[j]);
tempSets[1]->push_back(areas[j]);
}
}
}
/*
* This sorts the area sets, does NOT add if duplicate
* And then applies them to the node
*/
sort(tempSets[0]->begin(),tempSets[0]->end());
sort(tempSets[1]->begin(),tempSets[1]->end());
vector<Areaset*>::iterator itA=find_if(areasets.begin(),areasets.end(),
PointerFind<Areaset>(*tempSets[0]));
if(itA==areasets.end())
{
areasets.push_back(tempSets[0]);
}
else
{
delete tempSets[0];
tempSets[0]=*itA;
}
vector<Areaset*>::iterator itB=find_if(areasets.begin(),areasets.end(),
PointerFind<Areaset>(*tempSets[1]));
if(itB==areasets.end())
{
areasets.push_back(tempSets[1]);
}
else
{
delete tempSets[1];
tempSets[1]=*itB;
}
nodes[i]->setAreasets(tempSets);
}
}
/*
* Look at horizontal lines and see if drawing a vertical line from the current
* point to that line would cross it. If we cross an odd number of lines within
* an area in the one direction (here, up), we're in the area since we only
* allow 90 degree angles.
*/
bool Game::isInArea(const Area& target, Coord position) const //Blame Luke for any problems here
{
int tSize = target.size(), lCount = 0, lSize = 0;
for(int i=0;i<tSize;i++)
{
lSize = target[i]->line->size();
// We are starting from 1 since a line is two between two coordinates.
// We'll use the index and index-1 for those two.
for(int j=1;j<lSize;j++)
{
/*
* If this was change to y instead of x then it would find
* horizontal lines
*/
const Line& lRef = *(target[i]->line);
// A Vertical line to the left of this point.
if (lRef[j-1].x == lRef[j].x && lRef[j].x < position.x)
{
int minY,maxY;
minY=min(lRef[j].y,lRef[j-1].y);
maxY=max(lRef[j].y,lRef[j-1].y);
// Only one of these can have a equal sign. If a node is on the
// start/end of a line. We only want to detect one of these since
// otherwise it'll cross the other (odd/even) lines the wrong
// number of times.
if (position.y <= maxY && position.y > minY)
++lCount;
}
}
}
// If it is in the area then lCount, the number of lines between it and the
// origin, will be odd, and thus return true.
return lCount%2;
}
bool Game::connectable(const Node& nodea, const Node& nodeb) const
{
if (!updated)
throw AreasOutdated();
// A self loop is possible if we have two free connections
if (&nodea == &nodeb)
return !nodea.connections[1].exists() && !nodea.connections[2].exists();
// If either is dead, we can't connect
if (nodea.dead() || nodeb.dead())
return false;
// Otherwise, see if there's a shared areaset
return nodea.areasets[0] == nodeb.areasets[0] ||
nodea.areasets[0] == nodeb.areasets[1] ||
nodea.areasets[1] == nodeb.areasets[0] ||
nodea.areasets[1] == nodeb.areasets[1];
}
Node& Game::insertNode(Coord coord, Connection con1, Connection con2)
{
// We've changed something, must update after this
updated = false;
Node* node = new Node(coord, con1, con2);
nodes.push_back(node);
return *node;
}
Line& Game::insertLine(const Line& line)
{
// We've changed something, must update after this
updated = false;
Line* keep = new Line(line);
lines.push_back(keep);
return *keep;
}
void Game::clearAreas()
{
for (int i = 0; i < areas.size(); ++i)
delete areas[i];
// ALWAYS skip the first one, which is static, the defaultAreaset.
for (int i = 1; i < areasets.size(); ++i)
delete areasets[i];
// Just to make sure we never try accessing those again
areas.clear();
areasets.clear();
}
void Game::cleanup()
{
clearAreas();
for (int i = 0; i < nodes.size(); ++i)
delete nodes[i];
for (int i = 0; i < lines.size(); ++i)
delete lines[i];
nodes.clear();
lines.clear();
}
Game::~Game()
{
cleanup();
}
ostream& operator<<(ostream& os, const Game& g)
{
// Areas
for (int i = 0; i < g.areas.size(); i++)
os << "Area " << g.areas[i] << ": " << *g.areas[i] << endl;
// Areasets
for (int i = 0; i < g.areasets.size(); i++)
{
const Areaset& areaset = *(g.areasets[i]);
os << "Areaset " << g.areasets[i] << endl;
for (int j = 0; j < areaset.size(); j++)
if (areaset[j])
os << " Area " << areaset[j] << ": " << *areaset[j] << endl;
}
// Nodes
for (int i = 0; i < g.nodes.size(); i++)
{
os << "Node " << g.nodes[i] << " @ " << g.nodes[i]->loci << " { U:"
<< g.nodes[i]->openUp() << ", R:" << g.nodes[i]->openRight() << ", D:"
<< g.nodes[i]->openDown() << ", L:" << g.nodes[i]->openLeft() << " } "
<< "Areasets { " << g.nodes[i]->areasets[0] << ", "
<< g.nodes[i]->areasets[1] << " } " << endl;
for (int j = 0; j < 3; j++)
os << " Connection[" << j << "] " << g.nodes[i]->connections[j] << endl;
}
// Lines
for (int i = 0; i < g.lines.size(); i++)
os << "Line " << g.lines[i] << ": " << *g.lines[i] << endl;
return os;
}
void Game::doMove(const Line& line, Coord middle, bool extraChecks)
{
updated = false;
// Determine the end nodes
Node* a = NULL;
Node* b = NULL;
for (int i = 0; i < nodes.size(); i++)
{
if (nodes[i]->getLoci() == line.front())
{
if (a)
throw InvalidNode();
else
a = nodes[i];
}
if (nodes[i]->getLoci() == line.back())
{
if (b)
throw InvalidNode();
else
b = nodes[i];
}
}
// Couldn't find nodes
if (!a || !b)
throw InvalidNode();
if (line.size() == 0)
throw InvalidLine(line);
// Now that line crossings work, we don't really need to do this. In games
// with 10 nodes or so, it starts taking a while. We'll specify when
// creating the game whether we want extra checks (like this) to be
// performed.
if (extraChecks)
{
updateAreas();
if (!connectable(*a, *b))
throw NotConnectable();
}
// Split the line using the middle coordinate
int count = 0; // Add to first line when 0, second when 1
Line AC_line;
Line CB_line;
// First point since we start at the second point, looking at the first
// line segment
AC_line.push_back(line.front());
// When between two points, split there
for (int i = 1; i < line.size(); i++)
{
if ((middle.y == line[i].y && line[i].y == line[i-1].y && // Horizontal
((middle.x > line[i].x && middle.x < line[i-1].x) ||
(middle.x < line[i].x && middle.x > line[i-1].x))) ||
(middle.x == line[i].x && line[i].x == line[i-1].x && // Vertical
((middle.y > line[i].y && middle.y < line[i-1].y) ||
(middle.y < line[i].y && middle.y > line[i-1].y))))
{
++count;
AC_line.push_back(middle);
CB_line.push_back(middle);
CB_line.push_back(line[i]);
}
else
{
if (count == 0)
AC_line.push_back(line[i]);
else
CB_line.push_back(line[i]);
}
}
// We should have found a place to put the middle point
if (count != 1)
throw InvalidMiddle(count, middle);
// Note that if the middle point is on a corner, it will throw above because
// we check that x or y is less than one and greater than the other, meaning
// that it can't be equal to either.
Line& AC = insertLine(AC_line);
Line& CB = insertLine(CB_line);
try
{
// Insert middle node C between nodes A and B
Node& c = insertNode(middle,
Connection(&AC, a),
Connection(&CB, b));
try
{
// Likewise add the connection to the start and end nodes
a->addConnection(Connection(&AC, &c));
b->addConnection(Connection(&CB, &c));
++moveCount;
}
catch (...)
{
// Get rid of the node we just added
deleteLastNode();
throw;
}
}
// Since it ran into problems, make sure we get rid of anything we added
catch (...)
{
// We definitely inserted lines, so get rid of them.
delete &AC;
delete &CB;
lines.pop_back();
lines.pop_back();
throw;
}
updateAreas();
}
int Game::moves() const
{
return moveCount;
}
bool Game::gameEnded() const
{
// Loop through all nodes and return true if none of them can be connected
for (int i = 0; i < nodes.size(); i++)
{
for (int j = i; j < nodes.size(); j++)
{
if (connectable(*nodes[i], *nodes[j]))
{
return false;
}
}
}
return true;
}
void Game::deleteLastNode()
{
Node& node = *nodes.back();
for (int i = 0; i < 3; i++)
{
if (node.connections[i].exists())
{
Node& otherNode = *node.connections[i].dest;
// Delete any reciprocal connections pointing to this node
for (int j = 0; j < 3; j++)
{
if (otherNode.connections[j].dest == &node)
{
otherNode.connections[j] = Connection();
otherNode.updateOpen();
break;
}
}
// Don't delete the lines since we have better access to it elsewhere
}
}
delete nodes.back();
nodes.pop_back();
}
Node* Game::findNode(Coord point) const
{
for (int i = 0; i < nodes.size(); i++)
if (nodes[i]->getLoci() == point)
return nodes[i];
return NULL;
}
ostream& operator<<(ostream& os, const InvalidMiddle& o)
{
os << "Found " << o.count << " positions for invalid middle " << o.middle << ".";
return os;
}