-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinateSystem.cs
More file actions
441 lines (352 loc) · 13.2 KB
/
CoordinateSystem.cs
File metadata and controls
441 lines (352 loc) · 13.2 KB
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
/*
* Copyright (c) 2007 Philipp Garcia (phil@gotraxx.org)
*
* This file is part of GoTraxx (www.gotraxx.org).
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* This license governs use of the accompanying software. If you use the software, you
* accept this license. If you do not accept the license, do not use the software.
*
* Permission is granted to anyone to use this software for any noncommercial purpose,
* and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that
* you wrote the original software.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. If you bring a patent claim against the original author or any contributor over
* patents that you claim are infringed by the software, your patent license from
* such contributor to the software ends automatically.
*
* 4. This software may not be used in whole, nor in part, to enter any competition
* without written permission from the original author.
*
* 5. This notice may not be removed or altered from any source distribution.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace GoTraxx
{
public class CoordinateSystem
{
public static int MAX_BOARD_SIZE = 19;
public static int PASS = -1;
public static int RESIGN = -2;
// public static int NO_MOVE = -3;
protected static CoordinateSystem[] CoordinateSystems = CoordinateSystem.BuildCoordinateMatrix(MAX_BOARD_SIZE);
public int BoardSize;
public int BoardArea;
public List<int>[] Neighbors;
public List<int>[] Corners;
public int[,] NeighborsEx;
public CoordinateSystem(int boardSize)
{
BoardSize = boardSize;
BoardArea = BoardSize * BoardSize;
Neighbors = new List<int>[BoardArea];
Corners = new List<int>[BoardArea];
NeighborsEx = new int[BoardArea, 4];
for (int lIndex = 0; lIndex < BoardArea; lIndex++)
{
Neighbors[lIndex] = GetNeighborsEx(lIndex);
Corners[lIndex] = GetCornersEx(lIndex);
for (int lDirection = 0; lDirection < 4; lDirection++)
NeighborsEx[lIndex, lDirection] = GetNeighborEx(lIndex, lDirection);
}
}
public static CoordinateSystem GetCoordinateSystem(int boardSize)
{
return CoordinateSystems[boardSize];
}
public int At(int pX, int pY)
{
return (pX + (pY * BoardSize));
}
public int At(char pA, int pY)
{
char lA = Char.ToLower(pA);
return At((lA < 'I') ? lA - 'a' : lA - 'a' - 1, pY - 1);
}
// From SGF coordinates
public static int AtFromSGF(string str, int boardSize)
{
string lString = str.Trim().ToLower();
if (lString.Length < 2)
return PASS;
if (lString == "tt") // pass
return PASS;
// for SGF coordinates in standard Go format
if (lString == "pass") // pass
return PASS;
// for SGF coordinates in standard Go format
if (lString == "resign") // pass
return RESIGN;
char lA = lString[0];
if (!Char.IsDigit(lString[1]))
{
char lB = lString[1];
int lY = lB - 'a';
return At(lA - 'a', boardSize - lY - 1, boardSize);
}
else
{
string lB = lString.Substring(1);
return At((lA < 'i') ? lA - 'a' : lA - 'a' - 1, Convert.ToInt32(lB) - 1, boardSize);
}
}
public int At(string str)
{
if ((str.Length == 2) || (str.Length == 3))
{
char lA = Char.ToLower(str[0]);
int lY = Convert.ToInt32(str.Substring(1)) - 1;
return At((lA < 'i') ? lA - 'a' : lA - 'a' - 1, lY);
}
else
if (str.ToLower() == "resign")
return RESIGN;
else
return PASS;
}
public static int At(int x, int y, int boardSize)
{
return (x + (y * boardSize));
}
public static int At(string str, int boardSize)
{
if ((str.Length == 2) || (str.Length == 3))
{
char lA = Char.ToLower(str[0]);
int lY = Convert.ToInt32(str.Substring(1)) - 1;
return At((lA < 'i') ? lA - 'a' : lA - 'a' - 1, lY, boardSize);
}
else
if (str.ToLower() == "resign")
return RESIGN;
else
return PASS;
}
public bool OnBoard(int index)
{
return ((index >= 0) && (index < BoardArea));
}
public static bool OnBoard(int index, int boardSize)
{
return ((index >= 0) && (index < (boardSize * boardSize)));
}
public bool OnEdge(int index)
{
int lColumn = GetColumn(index);
int lRow = GetRow(index);
return ((lColumn == 0) || (lRow == 0) || (lColumn == BoardSize - 1) || (lRow == BoardSize - 1));
}
public bool OnCorner(int index)
{
int lColumn = GetColumn(index);
int lRow = GetRow(index);
return (((lColumn == 0) || (lColumn == BoardSize - 1)) && ((lRow == 0) || (lRow == BoardSize - 1)));
}
static public bool IsPass(int index)
{
return index == PASS;
}
public int GetColumn(int index)
{
return index % BoardSize;
}
public int GetRow(int index)
{
return index / BoardSize;
}
public static int GetColumn(int index, int boardSize)
{
return index % boardSize;
}
public static int GetRow(int index, int boardSize)
{
return index / boardSize;
}
public bool IsNeighbor(int index1, int index2)
{
if ((!OnBoard(index1)) || (!OnBoard(index2)))
return false;
int lColumn1 = GetColumn(index1);
int lRow1 = GetRow(index1);
int lColumn2 = GetColumn(index2);
int lRow2 = GetRow(index2);
if ((lColumn1 == lColumn2 - 1) && (lRow1 == lRow2) && (lColumn2 > 0)) return true;
if ((lColumn1 == lColumn2 + 1) && (lRow1 == lRow2) && (lColumn1 < BoardSize)) return true;
if ((lColumn1 == lColumn2) && (lRow1 == lRow2 - 1) && (lRow2 > 0)) return true;
if ((lColumn1 == lColumn2) && (lRow1 == lRow2 + 1) && (lRow1 < BoardSize)) return true;
return false;
}
protected int GetNeighborEx(int index, int direction)
{
int lColumn = GetColumn(index);
int lRow = GetRow(index);
switch (direction)
{
case 0: lColumn++; break; /* North */
case 1: lRow++; break; /* East */
case 2: lColumn--; break; /* South */
case 3: lRow--; break; /* West */
}
if ((lColumn < 0) || (lRow < 0) || (lColumn >= BoardSize) || (lRow >= BoardSize))
return PASS;
else
return At(lColumn, lRow);
}
protected static int GetNeighborEx(int index, int direction, int boardSize)
{
int lColumn = GetColumn(index, boardSize);
int lRow = GetRow(index, boardSize);
switch (direction)
{
case 0: lColumn++; break; /* North */
case 1: lRow++; break; /* East */
case 2: lColumn--; break; /* South */
case 3: lRow--; break; /* West */
}
if ((lColumn < 0) || (lRow < 0) || (lColumn >= boardSize) || (lRow >= boardSize))
return PASS;
else
return At(lColumn, lRow, boardSize);
}
protected int GetCornerEx(int index, int direction)
{
int lColumn = GetColumn(index);
int lRow = GetRow(index);
switch (direction)
{
case 0: lColumn++; lRow--; break; /* North-West */
case 1: lColumn++; lRow++; break; /* North-East */
case 2: lColumn--; lRow++; break; /* South-East */
case 3: lColumn--; lRow--; break; /* South-West */
}
if ((lColumn < 0) || (lRow < 0) || (lColumn >= BoardSize) || (lRow >= BoardSize))
return PASS;
else
return At(lColumn, lRow);
}
public static int TurnClockWise(int direction)
{
return (direction < 3) ? ++direction : 0;
}
public static int TurnCounterClockWise(int direction)
{
return (direction > 0) ? --direction : 3;
}
public static int TurnAround(int direction)
{
return TurnClockWise(TurnClockWise(direction));
}
public List<int> GetNeighbors(int index)
{
return Neighbors[index];
}
public int GetNeighbor(int index, int direction)
{
return NeighborsEx[index, direction];
}
public List<int> GetCorners(int index)
{
return Corners[index];
}
protected List<int> GetNeighborsEx(int index)
{
List<int> lNeighbors = new List<int>(4);
for (int lDirection = 0; lDirection < 4; lDirection++)
{
int lIndex = GetNeighborEx(index, lDirection);
if (lIndex != PASS)
lNeighbors.Add(lIndex);
}
return lNeighbors;
}
protected static List<int> GetNeighborsEx(int index, int boardSize)
{
List<int> lNeighbors = new List<int>(4);
for (int lDirection = 0; lDirection < 4; lDirection++)
{
int lIndex = GetNeighborEx(index, lDirection, boardSize);
if (lIndex != PASS)
lNeighbors.Add(lIndex);
}
return lNeighbors;
}
protected List<int> GetCornersEx(int index)
{
List<int> lCorners = new List<int>(4);
for (int lDirection = 0; lDirection < 4; lDirection++)
{
int lIndex = GetCornerEx(index, lDirection);
if (lIndex != PASS)
lCorners.Add(lIndex);
}
return lCorners;
}
public static String ToString(int pX, int pY)
{
if (pX < 0)
return "PASS";
int lColumn = pX;
if (lColumn >= 'I' - 'A')
lColumn++;
string lCol = Convert.ToString(Convert.ToChar(lColumn + 'A'));
return lCol + Convert.ToString(pY + 1);
}
public String ToString(int index)
{
if (index == PASS)
return "PASS";
if (index == RESIGN)
return "RESIGN";
int lColumn = GetColumn(index);
if (lColumn >= 'I' - 'A')
lColumn++;
string lCol = Convert.ToString(Convert.ToChar(lColumn + 'A'));
int lRow = GetRow(index) + 1;
return lCol + Convert.ToString(lRow);
}
public static String ToString2(int index, int boardsize)
{
if (index == PASS)
return "PASS";
if (index == RESIGN)
return "RESIGN";
int lColumn = GetColumn(index, boardsize);
if (lColumn >= 'I' - 'A')
lColumn++;
string lCol = Convert.ToString(Convert.ToChar(lColumn + 'A'));
int lRow = GetRow(index, boardsize) + 1;
return lCol + Convert.ToString(lRow);
}
public String ToSGFString(int index)
{
if (index == PASS)
return "";
if (index == RESIGN)
return "";
int lColumn = GetColumn(index);
int lRow = BoardSize - GetRow(index) - 1;
return Convert.ToString(Convert.ToChar(lColumn + 'a')) + Convert.ToString(Convert.ToChar(lRow + 'a'));
}
private static CoordinateSystem[] BuildCoordinateMatrix(int pUpToSize)
{
CoordinateSystem[] lCoordinateSystem = new CoordinateSystem[pUpToSize+1];
for (int i = 1; i <= pUpToSize; i++)
lCoordinateSystem[i] = new CoordinateSystem(i);
return lCoordinateSystem;
}
}
}