forked from paviad/GoSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point.cs
89 lines (81 loc) · 2.77 KB
/
Point.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Go
{
/// <summary>
/// Represents a pair of board coordinates (x and y).
/// </summary>
public struct Point
{
/// <summary>
/// The X value of the coordinate.
/// </summary>
public int x;
/// <summary>
/// The Y value of the coordinate.
/// </summary>
public int y;
/// <summary>
/// Constructs a Point object from the specified coordinates.
/// </summary>
/// <param name="xx">The X coordinate.</param>
/// <param name="yy">The Y coordinate.</param>
public Point(int xx, int yy)
{
x = xx;
y = yy;
}
/// <summary>
/// Returns a string representation of the Point in the format of (x,y).
/// </summary>
/// <returns>Returns a string representation of the Point in the format of (x,y).</returns>
public override string ToString()
{
return "(" + x + "," + y + ")";
}
/// <summary>
/// Returns a hash code based on the x and y values of the object.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return (x << 5) + y;
}
/// <summary>
/// Converts a point to SGF move format (e.g. 2,3 to "cd").
/// </summary>
/// <param name="x">The X coordinate.</param>
/// <param name="y">The Y coordinate.</param>
/// <returns>The point in SGF format.</returns>
public static string ConvertToSGF(int x, int y)
{
byte[] b = new byte[2] { (byte)(x + 97), (byte)(y + 97) };
return ASCIIEncoding.ASCII.GetString(b);
}
/// <summary>
/// Converts a point to SGF move format (e.g. 2,3 to "cd").
/// </summary>
/// <param name="pnt">The coordinates.</param>
/// <returns>The point in SGF format.</returns>
public static string ConvertToSGF(Point pnt)
{
if (pnt.Equals(Game.PassMove)) return "";
return ConvertToSGF(pnt.x, pnt.y);
}
/// <summary>
/// Converts an SGF format point to a Point object.
/// </summary>
/// <param name="sgf">The point in SGF format.</param>
/// <returns>The Point object representing the position.</returns>
public static Point ConvertFromSGF(string sgf)
{
if (sgf == "") return Game.PassMove;
var bb = ASCIIEncoding.ASCII.GetBytes (sgf);
int x = bb[0] >= 'a' ? bb[0] - 'a' : bb[0] - 'A' + 26;
int y = bb[1] >= 'a' ? bb[1] - 'a' : bb[1] - 'A' + 26;
return new Point (x, y);
}
}
}