forked from paviad/GoSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
542 lines (508 loc) · 18.5 KB
/
Board.cs
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
541
542
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Go
{
/// <summary>
/// Encapsulates a board position, without any game context. This object also
/// supports scoring mode by setting the IsScoring property to true.
/// </summary>
public class Board
{
private Content[,] content;
private Group[,] groupCache2;
private List<Group> groupCache = null;
private bool _IsScoring = false;
/// <summary>
/// Gets the horizontal size of the board.
/// </summary>
public int SizeX { get; private set; }
/// <summary>
/// Gets the vertical size of the board.
/// </summary>
public int SizeY { get; private set; }
/// <summary>
/// Gets or sets a flag indicating whether this board is in scoring mode.
/// If this property is changed from false to true, the scoring cache is cleared,
/// and all dead groups are reinstated. To reset the scoring process, set this
/// property to false and then to true again, or alternatively call ResetScoring.
/// </summary>
public bool IsScoring
{
get
{
return _IsScoring;
}
set
{
if (_IsScoring != value)
{
_IsScoring = value;
ClearGroupCache();
if (value) CalcTerritory();
}
}
}
/// <summary>
/// Gets a Dictionary<Content,int> containing the score for each side. The score
/// includes dead groups but does not include captured stones (no game context).
/// If SetDeadGroup is called, this property must be retrieved again to get
/// the updated score.
/// </summary>
public Dictionary<Content, int> Territory
{
get
{
Dictionary<Content, int> rc = new Dictionary<Content, int>();
int w = 0, b = 0;
foreach (var p in groupCache.Where(x => x.Content == Content.Empty))
{
if (p.Neighbours.All(x => GetContentAt(x) != Content.Black))
{
w += p.Points.Count();
p.Territory = Content.White;
}
else if (p.Neighbours.All(x => GetContentAt(x) != Content.White))
{
b += p.Points.Count();
p.Territory = Content.Black;
}
else p.Territory = Content.Empty;
}
foreach (var p in groupCache.Where(x => x.IsDead))
{
if (p.Content == Content.Black)
w += p.Points.Count() * 2;
else if (p.Content == Content.White)
b += p.Points.Count() * 2;
}
rc[Content.Black] = b;
rc[Content.White] = w;
return rc;
}
}
/// <summary>
/// Constructs a board object of specified horizontal and vertical size.
/// </summary>
/// <param name="sx">The horizontal size of the board.</param>
/// <param name="sy">The vertical size of the board.</param>
public Board(int sx, int sy)
{
content = new Content[sx, sy];
SizeX = sx;
SizeY = sy;
}
/// <summary>
/// Constructs a board object from an existing board object, copying its size and content.
/// </summary>
/// <param name="fromBoard">The source board object.</param>
public Board(Board fromBoard)
{
SizeX = fromBoard.SizeX;
SizeY = fromBoard.SizeY;
content = new Content[SizeX, SizeY];
Array.Copy (fromBoard.content, content, content.Length);
}
/// <summary>
/// Construct a board object from a parameter array. Each parameter may be
/// 0 for empty, 1 for black or 2 for white, and the number of parameters must
/// be a square of a natural number. The board size will be a square whose side
/// length is the square root of the number of parameters.
/// </summary>
/// <param name="c">The board content (0-empty, 1-black, 2-white).</param>
public Board(params int[] c)
{
if (c.Length == 0)
throw new InvalidOperationException("Must provide some arguments.");
double d = Math.Sqrt(c.Length);
int id = (int)d;
if (id != d)
throw new InvalidOperationException("Argument count must be a square of a natural number.");
SizeX = SizeY = id;
content = new Content[id, id];
int y = 0, x = 0;
for (int i = 0; i < c.Length; i++)
{
content[x, y] = (Content)c[i];
x++;
if (x == SizeX)
{
x = 0;
y++;
}
}
}
/// <summary>
/// Gets or sets the board content at the specified point. Changing the board
/// content using this property is not considered a game move, but rather a
/// setup move.
/// </summary>
/// <param name="x">The X coordinate of the position.</param>
/// <param name="y">The Y coordinate of the position.</param>
/// <returns></returns>
public Content this[int x, int y]
{
get
{
return GetContentAt(x, y);
}
set
{
SetContentAt(x, y, value);
}
}
/// <summary>
/// Gets or sets the board content at the specified point. Changing the board
/// content using this property is not considered a game move, but rather a
/// setup move.
/// </summary>
/// <param name="n">The coordinates of the position.</param>
/// <returns></returns>
public Content this[Point n]
{
get
{
return GetContentAt(n.x, n.y);
}
set
{
SetContentAt(n.x, n.y, value);
}
}
/// <summary>
/// Gets the board content at the specified point.
/// </summary>
/// <param name="n">The coordinates of the position.</param>
/// <returns></returns>
public Content GetContentAt(Point n)
{
return GetContentAt(n.x, n.y);
}
/// <summary>
/// Gets the board content at the specified point.
/// </summary>
/// <param name="x">The X coordinate of the position.</param>
/// <param name="y">The Y coordinate of the position.</param>
/// <returns></returns>
public Content GetContentAt(int x, int y)
{
if (IsScoring && content[x, y] != Content.Empty && groupCache2[x, y] != null && groupCache2[x, y].IsDead)
return Content.Empty;
return content[x, y];
}
/// <summary>
/// Sets the board content at the specified point, this is not considered a
/// game move, but rather a setup move.
/// </summary>
/// <param name="p">The coordinates of the position.</param>
/// <param name="content">The new content at the position.</param>
public void SetContentAt(Point p, Content content)
{
SetContentAt(p.x, p.y, content);
}
/// <summary>
/// Sets the board content at the specified point, this is not considered a
/// game move, but rather a setup move.
/// </summary>
/// <param name="x">The X coordinate of the position.</param>
/// <param name="y">The Y coordinate of the position.</param>
/// <param name="c">The new content at the position.</param>
public void SetContentAt(int x, int y, Content c)
{
if (x < 0 || x >= SizeX)
{
throw new ArgumentOutOfRangeException("x", "Invalid x coordinate.");
}
if (y < 0 || y >= SizeY)
{
throw new ArgumentOutOfRangeException("y", "Invalid y coordinate.");
}
content[x, y] = c;
ClearGroupCache();
}
/// <summary>
/// Gets the group including the board content at the specified position.
/// </summary>
/// <param name="n">The coordinates of the position.</param>
/// <returns>A group object containing a list of points.</returns>
public Group GetGroupAt(Point n)
{
return GetGroupAt(n.x, n.y);
}
/// <summary>
/// Gets the group including the board content at the specified position.
/// </summary>
/// <param name="x">The X coordinate of the position.</param>
/// <param name="y">The Y coordinate of the position.</param>
/// <returns>A group object containing a list of points.</returns>
public Group GetGroupAt(int x, int y)
{
if (groupCache == null)
{
groupCache = new List<Group>();
groupCache2 = new Group[SizeX, SizeY];
}
Group group = groupCache.SingleOrDefault(z => z.Points.Contains(new Point(x, y)));
if (group == null)
{
group = new Group(content[x, y]);
RecursiveAddPoint(group, x, y);
groupCache.Add(group);
}
return group;
}
private void RecursiveAddPoint(Group group, int x, int y)
{
if (GetContentAt(x, y) == group.Content)
{
if (group.ContainsPoint(x, y)) return;
group.AddPoint(x, y);
groupCache2[x, y] = group;
if (x > 0) RecursiveAddPoint(group, x - 1, y);
if (x < SizeX - 1) RecursiveAddPoint(group, x + 1, y);
if (y > 0) RecursiveAddPoint(group, x, y - 1);
if (y < SizeY - 1) RecursiveAddPoint(group, x, y + 1);
}
else
{
group.AddNeighbour(x, y);
}
}
/// <summary>
/// Gets the liberty count of the specified group.
/// </summary>
/// <param name="group">The group object.</param>
/// <returns>The number of liberties of the specified group.</returns>
public int GetLiberties(Group group)
{
int libs = 0;
foreach (var n in group.Neighbours)
{
if (GetContentAt(n) == Content.Empty) libs++;
}
return libs;
}
/// <summary>
/// Gets the liberty count of the group containing the board content at
/// the specified point.
/// </summary>
/// <param name="x">The X coordinate of the position.</param>
/// <param name="y">The Y coordinate of the position.</param>
/// <returns>The number of liberties.</returns>
public int GetLiberties(int x, int y)
{
return GetLiberties(GetGroupAt(x, y));
}
/// <summary>
/// Gets the liberty count of the group containing the board content at
/// the specified point.
/// </summary>
/// <param name="n">The coordinates of the position.</param>
/// <returns>The number of liberties.</returns>
public int GetLiberties(Point n)
{
return GetLiberties(n.x, n.y);
}
private void CalcTerritory()
{
bool pass = true;
while (pass)
{
pass = false;
for (int i = 0; i < SizeX; i++)
{
for (int j = 0; j < SizeY; j++)
{
if (groupCache2[i, j] == null)
{
GetGroupAt(i, j);
pass = true;
}
}
}
}
}
/// <summary>
/// Marks a group as dead for the purposes of scoring. This method has no effect if
/// the board is not in scoring mode (see the IsScoring property).
/// </summary>
/// <param name="n">The coordinates of the position of a stone in the group.</param>
public void SetDeadGroup(Point n)
{
SetDeadGroup(n.x, n.y);
}
/// <summary>
/// Marks a group as dead for the purposes of scoring. This method has no effect if
/// the board is not in scoring mode (see the IsScoring property).
/// </summary>
/// <param name="x">The X coordinate of a position belonging to the group.</param>
/// <param name="y">The Y coordinate of a position belonging to the group.</param>
public void SetDeadGroup(int x, int y)
{
Group g = GetGroupAt(x, y);
if (g.Content == Content.Empty) return;
g.IsDead = !g.IsDead;
}
/// <summary>
/// Resets the scoring process, unmarking dead groups.
/// </summary>
public void ResetScoring()
{
if (!IsScoring) return;
ClearGroupCache();
CalcTerritory();
}
internal List<Group> GetCapturedGroups(int x, int y)
{
Group group = GetGroupAt(x, y);
List<Group> captures = new List<Group>();
var stoneNeighbours = GetStoneNeighbours(x, y);
foreach (var n in stoneNeighbours)
{
if (GetContentAt(n) != Content.Empty)
{
Group ngroup = GetGroupAt(n);
if (ngroup.ContainsPoint(x, y)) continue; // Don't consider self group
if (GetLiberties(ngroup) == 0)
{
if (!captures.Any(g => g.Points.Intersect(ngroup.Points).Any()))
captures.Add(ngroup);
}
}
}
return captures;
}
private List<Point> GetStoneNeighbours(int x, int y)
{
List<Point> rc = new List<Point>();
if (x > 0) rc.Add(new Point(x - 1, y));
if (x < SizeX - 1) rc.Add(new Point(x + 1, y));
if (y > 0) rc.Add(new Point(x, y - 1));
if (y < SizeY - 1) rc.Add(new Point(x, y + 1));
return rc;
}
internal int Capture(IEnumerable<Group> captures)
{
int rc = 0;
foreach (var g in captures)
{
rc += Capture(g);
}
return rc;
}
internal int Capture(Group g)
{
foreach (var p in g.Points)
SetContentAt(p, Content.Empty);
return g.Points.Count();
}
private void ClearGroupCache()
{
groupCache = null;
}
internal int GetContentHashCode()
{
int hc = 0, tmp;
foreach (var i in content)
{
tmp = hc >> 30;
hc <<= 2;
hc ^= (int)i ^ tmp;
}
return hc;
}
/// <summary>
/// Returns a multi-line string representation of the board with the scoring
/// state. Each spot is composed of two characters. The first is one of [.XO]
/// representing an empty, black or white board content respectively. The second
/// is one of [.xoD] representing unowned, black or white territory, or D for a
/// dead group.
/// </summary>
/// <returns>Returns the multi-line string representation of the board.</returns>
public override string ToString()
{
string rc = "";
for (int i = 0; i < SizeY; i++)
{
for (int j = 0; j < SizeX; j++)
{
if (content[j, i] == Content.Empty) rc += ".";
else if (content[j, i] == Content.Black) rc += "X";
else rc += "O";
if (IsScoring)
{
Group g = groupCache2[j, i];
if (g.IsDead) rc += "D";
else if (g.Territory == Content.Empty) rc += ".";
else if (g.Territory == Content.Black) rc += "x";
else if (g.Territory == Content.White) rc += "o";
}
rc += " ";
}
rc += "\n";
}
return rc;
}
/// <summary>
/// Gets a hash code of this board. Hash code includes board content.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return GetContentHashCode();
}
/// <summary>
/// Represents a position and a content at that position.
/// </summary>
public struct PositionContent
{
/// <summary>
/// The position point.
/// </summary>
public Point Position;
/// <summary>
/// The content at the position.
/// </summary>
public Content Content;
}
/// <summary>
/// Returns an enumerable representing all occupied board spots.
/// </summary>
public IEnumerable<PositionContent> AllStones
{
get
{
for (int i = 0; i < SizeX; i++)
{
for (int j = 0; j < SizeY; j++)
{
if (content[i, j] != Content.Empty)
yield return new PositionContent
{
Content = content[i, j],
Position = new Point(i, j)
};
}
}
}
}
/// <summary>
/// Returns an enumerable representing all empty board spots.
/// </summary>
public IEnumerable<Point> EmptySpaces
{
get
{
for (int i = 0; i < SizeX; i++)
{
for (int j = 0; j < SizeY; j++)
{
if (content[i, j] == Content.Empty)
yield return new Point(i, j);
}
}
}
}
}
}