-
Notifications
You must be signed in to change notification settings - Fork 5
/
board.go
183 lines (170 loc) · 1.83 KB
/
board.go
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
package chessimage
import "fmt"
// Tile represents a specific position of a tile on a chess board
type Tile int8
func (s Tile) rank() int {
return int(int(s) / 8)
}
func (s Tile) rankInverted() int {
return 7 - s.rank()
}
func (s Tile) file() int {
return int(s) % 8
}
func (s Tile) fileInverted() int {
return 7 - s.file()
}
func tileFromRankFile(rank int, file int) Tile {
return Tile(file*8 + rank)
}
type position struct {
tile Tile
pieceSymbol string
}
type board []position
//LastMove represents two tiles that indicate a piece was moved
type LastMove struct {
From Tile
To Tile
}
//TileFromAN will attempt to get a tile by its algebraic notation (ie: "e5")
func TileFromAN(an string) (Tile, error) {
tile, ok := tileMap[an]
if !ok {
return NoTile, fmt.Errorf("tile %v not found", an)
}
return tile, nil
}
const (
NoTile Tile = iota - 1
A8
B8
C8
D8
E8
F8
G8
H8
A7
B7
C7
D7
E7
F7
G7
H7
A6
B6
C6
D6
E6
F6
G6
H6
A5
B5
C5
D5
E5
F5
G5
H5
A4
B4
C4
D4
E4
F4
G4
H4
A3
B3
C3
D3
E3
F3
G3
H3
A2
B2
C2
D2
E2
F2
G2
H2
A1
B1
C1
D1
E1
F1
G1
H1
)
var tileMap = map[string]Tile{
"a1": A1,
"a2": A2,
"a3": A3,
"a4": A4,
"a5": A5,
"a6": A6,
"a7": A7,
"a8": A8,
"b1": B1,
"b2": B2,
"b3": B3,
"b4": B4,
"b5": B5,
"b6": B6,
"b7": B7,
"b8": B8,
"c1": C1,
"c2": C2,
"c3": C3,
"c4": C4,
"c5": C5,
"c6": C6,
"c7": C7,
"c8": C8,
"d1": D1,
"d2": D2,
"d3": D3,
"d4": D4,
"d5": D5,
"d6": D6,
"d7": D7,
"d8": D8,
"e1": E1,
"e2": E2,
"e3": E3,
"e4": E4,
"e5": E5,
"e6": E6,
"e7": E7,
"e8": E8,
"f1": F1,
"f2": F2,
"f3": F3,
"f4": F4,
"f5": F5,
"f6": F6,
"f7": F7,
"f8": F8,
"g1": G1,
"g2": G2,
"g3": G3,
"g4": G4,
"g5": G5,
"g6": G6,
"g7": G7,
"g8": G8,
"h1": H1,
"h2": H2,
"h3": H3,
"h4": H4,
"h5": H5,
"h6": H6,
"h7": H7,
"h8": H8,
}