-
Notifications
You must be signed in to change notification settings - Fork 1
/
square.go
210 lines (182 loc) · 3.83 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
package pawn
import "fmt"
type Square struct {
Position
Piece
}
func (s Square) String() string {
return fmt.Sprintf("[%s %s]", s.Piece.FAN(), s.Position.AN())
}
func (s Square) possiblePaths() []Path {
switch s.Material {
// TODO Handle capture case? Or only from perspective of the board?
case Pawn:
switch s.Rank {
case 2:
switch s.Color {
case White:
up1, _ := s.Jump(Up)
up2, _ := s.Jump(Up, Up)
return []Path{Path{up1}, Path{up2}}
case Black:
down, _ := s.Jump(Down)
return []Path{Path{down}}
}
case 7:
switch s.Color {
case Black:
down1, _ := s.Jump(Down)
down2, _ := s.Jump(Down, Down)
return []Path{Path{down1}, Path{down2}}
case White:
up, _ := s.Jump(Up)
return []Path{Path{up}}
}
default:
switch s.Color {
case White:
if up, err := s.Jump(Up); err == nil {
return []Path{Path{up}}
} else {
return []Path{}
}
case Black:
if down, err := s.Jump(Down); err == nil {
return []Path{Path{down}}
} else {
return []Path{}
}
}
}
case Knight:
possibleJumps := [][]Direction{
[]Direction{Up, Up, Right},
[]Direction{Right, Right, Up},
[]Direction{Right, Right, Down},
[]Direction{Down, Down, Right},
[]Direction{Down, Down, Left},
[]Direction{Left, Left, Down},
[]Direction{Left, Left, Up},
[]Direction{Up, Up, Left},
}
paths := []Path{}
for _, jumps := range possibleJumps {
if jump, err := s.Jump(jumps...); err == nil {
paths = append(paths, Path{jump})
}
}
return paths
case Rook:
return paths(
s.Path(Up),
s.Path(Right),
s.Path(Down),
s.Path(Left),
)
case Bishop:
return paths(
s.Path(UpRightDiagonal),
s.Path(DownRightDiagonal),
s.Path(DownLeftDiagonal),
s.Path(UpLeftDiagonal),
)
case Queen:
return paths(
s.Path(Up),
s.Path(UpRightDiagonal),
s.Path(Right),
s.Path(DownRightDiagonal),
s.Path(Down),
s.Path(DownLeftDiagonal),
s.Path(Left),
s.Path(UpLeftDiagonal),
)
case King:
return paths(
s.OneMove(Up),
s.OneMove(UpRightDiagonal),
s.OneMove(Right),
s.OneMove(DownRightDiagonal),
s.OneMove(Down),
s.OneMove(DownLeftDiagonal),
s.OneMove(Left),
s.OneMove(UpLeftDiagonal),
)
}
return []Path{}
}
func (s Square) pathsToTake() []Path {
switch s.Material {
case Pawn:
paths := []Path{}
switch s.Color {
case White:
if upRight, err := s.Jump(Up, Right); err == nil {
paths = append(paths, Path{upRight})
}
if upLeft, err := s.Jump(Up, Left); err == nil {
paths = append(paths, Path{upLeft})
}
case Black:
if downLeft, err := s.Jump(Down, Right); err == nil {
paths = append(paths, Path{downLeft})
}
if downRight, err := s.Jump(Down, Left); err == nil {
paths = append(paths, Path{downRight})
}
}
return paths
default:
return s.possiblePaths()
}
}
// Given a set of Path it simply strips out any that
// are empty
func paths(p ...Path) []Path {
paths := []Path{}
for _, path := range p {
if len(path) > 0 {
paths = append(paths, path)
}
}
return paths
}
var materialByFile = map[File]Material{
A: Rook,
B: Knight,
C: Bishop,
D: Queen,
E: King,
F: Bishop,
G: Knight,
H: Rook,
}
func NewSquare(position Position) *Square {
var color Color
emptyColor := color
var material Material
emptyMaterial := material
switch position.Rank {
case 7, 8:
color = Black
case 1, 2:
color = White
}
switch position.Rank {
case 2, 7:
material = Pawn
case 1, 8:
material = materialByFile[position.File]
}
if color != emptyColor && material != emptyMaterial {
return &Square{Position: position, Piece: Piece{color, material}}
}
return &Square{Position: position}
}
func AllSquares() []*Square {
var squares = []*Square{}
for _, position := range allPositions {
squares = append(squares, NewSquare(position))
}
return squares
}