-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircum.go
372 lines (351 loc) · 7.35 KB
/
circum.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package fm3
import (
"sort"
"strconv"
"strings"
)
type Piece struct {
Hither bool
Name Name // class name or empty
}
type Circum struct {
Board [10][10]*Piece // col, row
Hand map[bool]map[Name]int // hither -> class name -> num
Hither bool
LastIsUchifu bool
theHash int64
}
// String return human readable board.
// TODO(oka): Match the fm's output http://www.abz.jp/~k7ro/report/ki4slct.html
func (c *Circum) String() string {
res := ""
res += "後手:"+handToStr(c.Hand[false])+"\n"
var rows []string
edge := "+---------------------------+"
rows = append(rows, edge)
for j := 1; j <= 9; j++ {
var row []string
for i := 9; i >= 1; i-- {
p := c.Board[i][j]
if p == nil {
row = append(row, " "+NONE.letter())
} else if p.Hither {
row = append(row, " "+p.Name.letter())
} else {
row = append(row, "v"+p.Name.letter())
}
}
rows = append(rows, "|" + strings.Join(row, "") + "|")
}
rows = append(rows, edge)
res += strings.Join(rows, "\n")+"\n"
res += "持駒:"+handToStr(c.Hand[true])+"\n"
return res
}
func handToStr(hand map[Name]int) string {
var names []int
for n, _ := range hand {
names = append(names, int(n))
}
sort.Ints(names)
// fmt.Println("names", names, hand)
res := ""
for _, ni := range names {
n := Name(ni)
i := hand[n]
if i < 0 {
panic("i < 0")
}
if i == 1 {
res += n.letter()+" "
} else if i > 1 {
res += n.letter()+strconv.Itoa(i)+" "
}
}
return res
}
func (c *Circum) clone() *Circum {
d := NewCircum()
for i := 1; i <= 9; i++ {
for j := 1; j <= 9; j++ {
d.Board[i][j] = c.Board[i][j]
}
}
for name, num := range c.Hand[true] {
d.Hand[true][name] = num
}
for name, num := range c.Hand[false] {
d.Hand[false][name] = num
}
d.Hither = c.Hither
d.LastIsUchifu = c.LastIsUchifu
return d
}
func (c *Circum) hash() int64 {
if c.theHash != 0 {
return c.theHash
}
const mul = int64(1e9 + 7)
res := int64(0)
if c.Hither {
res += 1
}
if c.LastIsUchifu {
res += 2
}
for i := 1; i <= 9; i++ {
for j := 1; j <= 9; j++ {
res *= mul
if p := c.Board[i][j]; p != nil {
res += int64(p.Name)*2
if p.Hither {
res+= 1
}
}
}
}
for n, i := range c.Hand[true] {
res *= mul
res +=int64(n)*2
res += 1
res *= mul
res += int64(i)
}
for n, i := range c.Hand[false] {
res *= mul
res += int64(n)*2
res *= mul
res += int64(i)
}
c.theHash = res
return res
}
func NewCircum() *Circum {
c := &Circum{
Hand: make(map[bool]map[Name]int),
}
c.Hand[true] = make(map[Name]int)
c.Hand[false] = make(map[Name]int)
return c
}
// Mated returns if I am mated.
func (c *Circum) Mated() bool {
return c.Checked() && len(c.Nexts()) == 0
}
// Checked returns if I am checked.
func (c *Circum) Checked() bool {
me := c.Hither
you := !me
// When it is your turn
c.Hither = you
defer func() {
c.Hither = me
}()
// fmt.Println("c",c)
for _, nc := range c.maybeNexts() {
// You captured my king
if nc.Hand[you][OU] > 0 {
return true
}
}
return false
}
// Illegal returns if this circum is illegal. Illegal circums are
// disregard of check, immovable pieces or uchifuzume.
func (c *Circum) Illegal() bool {
me := c.Hither
you := !me
// disregard of check
c.Hither = you
if c.Checked() {
c.Hither = me
return true
}
c.Hither = me
// If I am yonder, must be being checked.
if !me && !c.Checked() {
return true
}
// you have immovable pieces
if you { // hither
for col := 1; col <= 9; col++ {
for row := 1; row <= 2; row++ {
if p := c.Board[col][row]; p != nil && p.Hither == you {
if (p.Name == FU || p.Name == KY) && row == 1 {
return true
} else if p.Name == KE {
return true
}
}
}
}
} else {// yonder
for col := 1; col <= 9; col++ {
for row := 8; row <= 9; row++ {
if p := c.Board[col][row]; p != nil && p.Hither == you {
if (p.Name == FU || p.Name == KY) && row == 9 {
return true
} else if p.Name == KE {
return true
}
}
}
}
}
// you made nifu
for col := 1; col <= 9; col++ {
found := false
for row := 1; row <= 9; row++ {
if p := c.Board[col][row]; p != nil && p.Hither == you && p.Name == FU {
if found {
return true
}
found = true
}
}
}
// uchifuzume
if c.LastIsUchifu && c.Mated() {
return true
}
return false
}
// Nexts returns all next legal circums.
func (c *Circum) Nexts() []*Circum {
var res []*Circum
for _, nc := range c.maybeNexts() {
if !nc.Illegal() {
res = append(res, nc)
}
}
return res
}
var promotableRow map[bool]map[int]bool = map[bool]map[int]bool {
true: map[int]bool {
1: true, 2: true, 3: true,
},
false: map[int]bool {
7: true, 8: true, 9: true,
},
}
// maybeNexts doesn't consider illegality. May capture king and take it in hand.
func (c *Circum) maybeNexts() []*Circum {
var res []*Circum
for i := 1; i <= 9; i++ {
for j := 1; j <= 9; j++ {
p := c.Board[i][j];
if (p != nil && c.Hither == p.Hither) {
cl := p.Name.class()
// leap
for _, l := range cl.leap {
if !c.Hither {
l = V {
c: -l.c,
r: -l.r,
}
}
ni := i + l.c
nj := j + l.r
if (ni < 1 || 9 < ni || nj < 1 || 9 < nj) {
continue
}
q := c.Board[ni][nj]
if q == nil || q.Hither != c.Hither {
res = append(res, move(c, newV(i, j), newV(ni, nj), false))
if cl.promote != NONE {
rs := promotableRow[c.Hither]
if rs[j] || rs[nj] {
res = append(res, move(c, newV(i, j), newV(ni, nj), true))
}
}
}
}
// ride
for _, r := range cl.ride {
if !c.Hither {
r = V {
c: -r.c,
r: -r.r,
}
}
for ni, nj := i + r.c, j + r.r; 1 <= ni && ni <= 9 && 1 <= nj && nj <= 9; ni, nj = ni+r.c, nj+r.r {
q := c.Board[ni][nj]
if q == nil {
res = append(res, move(c, newV(i, j), newV(ni, nj), false))
if cl.promote != NONE {
rs := promotableRow[c.Hither]
if rs[j] || rs[nj] {
res = append(res, move(c, newV(i, j), newV(ni, nj), true))
}
}
} else {
if q.Hither != c.Hither {
res = append(res, move(c, newV(i, j), newV(ni, nj), false))
if cl.promote != NONE {
rs := promotableRow[c.Hither]
if rs[j] || rs[nj] {
res = append(res, move(c, newV(i, j), newV(ni, nj), true))
}
}
}
break
}
}
}
}
}
}
for name, num := range c.Hand[c.Hither] {
if num < 0 {
panic("num < 0")
}
if num == 0 {
continue
}
for i := 1; i <= 9; i++ {
for j := 1; j <= 9; j++ {
if c.Board[i][j] == nil {
d := c.clone()
d.Hither = !d.Hither
d.Hand[c.Hither][name]--
d.Board[i][j] = &Piece {
Hither: c.Hither,
Name: name,
}
d.LastIsUchifu = name == FU
res = append(res, d)
}
}
}
}
return res
}
func move(c *Circum, from, to V, promote bool) *Circum {
p := c.Board[from.c][from.r]
q := c.Board[to.c][to.r]
me := c.Hither
you := !me
d := c.clone()
d.Hither = you
d.LastIsUchifu = false
if q != nil {
if q.Hither == me {
panic("Can't capture my piece.")
}
if unp := q.Name.class().unpromote; unp != NONE {
d.Hand[me][unp]++
} else {
d.Hand[me][q.Name]++
}
}
if promote {
d.Board[to.c][to.r] = &Piece {
Hither: me,
Name: p.Name.class().promote,
}
} else {
d.Board[to.c][to.r] = p
}
d.Board[from.c][from.r] = nil
return d
}