/
square.go
243 lines (228 loc) · 5.58 KB
/
square.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
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
//
// FrankyGo - UCI chess engine in GO for learning purposes
//
// MIT License
//
// Copyright (c) 2018-2020 Frank Kopp
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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.
//
package types
import (
"fmt"
"github.com/frankkopp/FrankyGo/internal/assert"
)
// Square represent exactly on square on a chess board.
type Square uint8
//noinspection GoUnusedConst
const (
SqA1 Square = iota // 0
SqB1 Square = iota // 1
SqC1 Square = iota
SqD1 Square = iota
SqE1 Square = iota
SqF1 Square = iota
SqG1 Square = iota
SqH1 Square = iota
SqA2 Square = iota
SqB2 Square = iota
SqC2 Square = iota
SqD2 Square = iota
SqE2 Square = iota
SqF2 Square = iota
SqG2 Square = iota
SqH2 Square = iota
SqA3 Square = iota
SqB3 Square = iota
SqC3 Square = iota
SqD3 Square = iota
SqE3 Square = iota
SqF3 Square = iota
SqG3 Square = iota
SqH3 Square = iota
SqA4 Square = iota
SqB4 Square = iota
SqC4 Square = iota
SqD4 Square = iota
SqE4 Square = iota
SqF4 Square = iota
SqG4 Square = iota
SqH4 Square = iota
SqA5 Square = iota
SqB5 Square = iota
SqC5 Square = iota
SqD5 Square = iota
SqE5 Square = iota
SqF5 Square = iota
SqG5 Square = iota
SqH5 Square = iota
SqA6 Square = iota
SqB6 Square = iota
SqC6 Square = iota
SqD6 Square = iota
SqE6 Square = iota
SqF6 Square = iota
SqG6 Square = iota
SqH6 Square = iota
SqA7 Square = iota
SqB7 Square = iota
SqC7 Square = iota
SqD7 Square = iota
SqE7 Square = iota
SqF7 Square = iota
SqG7 Square = iota
SqH7 Square = iota
SqA8 Square = iota
SqB8 Square = iota
SqC8 Square = iota
SqD8 Square = iota
SqE8 Square = iota
SqF8 Square = iota
SqG8 Square = iota
SqH8 Square = iota // 63
SqNone Square = iota // 64
)
// IsValid checks a value of type square if it represents a valid
// square on a chess board (e.q. sq < 64).
func (sq Square) IsValid() bool {
return sq < SqNone
}
// FileOf returns the file of the square
func (sq Square) FileOf() File {
return File(sq & 7)
}
// RankOf returns the rank of the square
func (sq Square) RankOf() Rank {
return Rank(sq >> 3)
}
// MakeSquare returns a square based on the string given or SqNone if
// no valid square could be read from the string
func MakeSquare(s string) Square {
if assert.DEBUG {
assert.Assert(len(s) == 2, "square string is not 2 characters long")
}
file := File(s[0] - 'a')
rank := Rank(s[1] - '1')
if !file.IsValid() || !rank.IsValid() {
return SqNone
}
return SquareOf(file, rank)
}
// String returns a string of the file letter and rank number (e.g. e5)
// if the sq is not a valid square returns "-"
func (sq Square) String() string {
if !sq.IsValid() {
return "-"
}
return sq.FileOf().String() + sq.RankOf().String()
}
// SquareOf returns a square from file and rank
// Returns SqNone for invalid files or ranks
func SquareOf(f File, r Rank) Square {
if !f.IsValid() || !r.IsValid() {
return SqNone
}
return Square((int(r) << 3) + int(f))
}
// To returns the square on the chess board in the given direction
func (sq Square) To(d Direction) Square {
// Precomputed
// order: North, East, South, West, Northeast, Southeast, Southwest, Northwest
switch d {
case North:
return sqTo[sq][0]
case East:
return sqTo[sq][1]
case South:
return sqTo[sq][2]
case West:
return sqTo[sq][3]
case Northeast:
return sqTo[sq][4]
case Southeast:
return sqTo[sq][5]
case Southwest:
return sqTo[sq][6]
case Northwest:
return sqTo[sq][7]
default:
panic(fmt.Sprintf("Invalid direction %d", d))
}
}
var sqTo [SqLength][8]Square
func init() {
for sq := SqA1; sq < SqNone; sq++ {
for i, dir := range Directions {
sqTo[sq][i] = sq.toPreCompute(dir)
}
}
}
func (sq Square) toPreCompute(d Direction) Square {
// overflow To south or north are easily detected <0 ot >63
// east and west need check
switch d {
case North:
sq += Square(d)
case East:
if sq.FileOf() < FileH {
sq += Square(d)
} else {
return SqNone
}
case South:
sq += Square(d)
case West:
if sq.FileOf() > FileA {
sq += Square(d)
} else {
return SqNone
}
case Northeast:
if sq.FileOf() < FileH {
sq += Square(d)
} else {
return SqNone
}
case Southeast:
if sq.FileOf() < FileH {
sq += Square(d)
} else {
return SqNone
}
case Southwest:
if sq.FileOf() > FileA {
sq += Square(d)
} else {
return SqNone
}
case Northwest:
if sq.FileOf() > FileA {
sq += Square(d)
} else {
return SqNone
}
default:
panic(fmt.Sprintf("Invalid direction %d", d))
}
if sq.IsValid() {
return sq
} else {
return SqNone
}
}