@@ -12,29 +12,6 @@ type Point struct {
12
12
y int64
13
13
}
14
14
15
- // pointToSgfMap is a translation reference between int64 Point
16
- // and string SGF-Point (rune) values
17
- var pointToSgfMap = map [int64 ]rune {
18
- 0 : 'a' , 1 : 'b' , 2 : 'c' , 3 : 'd' , 4 : 'e' , 5 : 'f' , 6 : 'g' ,
19
- 7 : 'h' , 8 : 'i' , 9 : 'j' , 10 : 'k' , 11 : 'l' , 12 : 'm' , 13 : 'n' ,
20
- 14 : 'o' , 15 : 'p' , 16 : 'q' , 17 : 'r' , 18 : 's' , 19 : 't' , 20 : 'u' ,
21
- 21 : 'v' , 22 : 'w' , 23 : 'x' , 24 : 'y' , 25 : 'z' , 26 : 'A' , 27 : 'B' ,
22
- 28 : 'C' , 29 : 'D' , 30 : 'E' , 31 : 'F' , 32 : 'G' , 33 : 'H' , 34 : 'I' ,
23
- 35 : 'J' , 36 : 'K' , 37 : 'L' , 38 : 'M' , 39 : 'N' , 40 : 'O' , 41 : 'P' ,
24
- 42 : 'Q' , 43 : 'R' , 44 : 'S' , 45 : 'T' , 46 : 'U' , 47 : 'V' , 48 : 'W' ,
25
- 49 : 'X' , 50 : 'Y' , 51 : 'Z' ,
26
- }
27
-
28
- // sgfToPointMap is a translation reference between string SGF-Point
29
- // (rune) values and int64 Point values
30
- var sgfToPointMap = func (m map [int64 ]rune ) map [rune ]int64 {
31
- out := make (map [rune ]int64 )
32
- for key , val := range m {
33
- out [val ] = key
34
- }
35
- return out
36
- }(pointToSgfMap )
37
-
38
15
// New creates a new immutable Point.
39
16
func New (x , y int64 ) * Point {
40
17
return & Point {
@@ -52,29 +29,34 @@ func (pt *Point) Y() int64 { return pt.y }
52
29
// ToSGF converts a pointer-type (immutable) *Point
53
30
// to an SGF Point (two letter string).
54
31
func (pt * Point ) ToSGF () (string , error ) {
55
- if pt .X () < 0 || pt .X () > 51 || pt .Y () < 0 || pt .Y () > 51 {
56
- return "" , fmt .Errorf ("error converting point to SGF-point; points must be between 0 and 51 inclusive. found %v" , pt .String ())
57
- }
58
- return string (pointToSgfMap [pt .X ()]) + string (pointToSgfMap [pt .Y ()]), nil
32
+ return toSGF (pt )
33
+ }
34
+
35
+ // Equals returns whether this point is equal to another point.
36
+ func (pt * Point ) Equals (other * Point ) bool {
37
+ return pt .X () == other .X () && pt .Y () == other .Y ()
59
38
}
60
39
61
40
// String converts to string representation of a Point.
62
- func (pt Point ) String () string {
41
+ func (pt * Point ) String () string {
63
42
return fmt .Sprintf ("{%d,%d}" , pt .x , pt .y )
64
43
}
65
44
66
- // NewFromSGF converts an SGF point (two letter string) to a Point.
67
- func NewFromSGF (sgfPt string ) (* Point , error ) {
68
- if sgfPt == "" || len (sgfPt ) != 2 {
69
- return nil , fmt .Errorf ("sgf point must be non-empty and two letter char, but was %s" , sgfPt )
70
- }
71
- intX , ok := sgfToPointMap [rune (sgfPt [0 ])]
72
- if ! ok {
73
- return nil , fmt .Errorf ("could not convert coordinate for x-value of sgf point %s; only a-zA-Z (minus i/I) are allowed" , sgfPt )
74
- }
75
- intY , ok := sgfToPointMap [rune (sgfPt [1 ])]
76
- if ! ok {
77
- return nil , fmt .Errorf ("could not convert coordinate for y-value of sgf point %s; only a-zA-Z (minus i/I) are allowed" , sgfPt )
78
- }
79
- return New (intX , intY ), nil
45
+ // Key is a convenience helper to convert this point to a key-struct.
46
+ func (pt * Point ) Key () Key {
47
+ return Key {X : pt .X (), Y : pt .Y ()}
48
+ }
49
+
50
+ // Key is a point-struct that is used for keys in maps. As such, it's intended
51
+ // to be used like the following:
52
+ //
53
+ // Key{X:12, Y:15}
54
+ type Key struct {
55
+ X int64
56
+ Y int64
57
+ }
58
+
59
+ // Point converts a point-Key back to a point.
60
+ func (k Key ) Point () * Point {
61
+ return New (k .X , k .Y )
80
62
}
0 commit comments