Skip to content

Commit fb90a70

Browse files
committed
Add detailed board information; add captured move
1 parent c450b45 commit fb90a70

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

engine/board.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,20 @@ func (b *Board) String() string {
250250
files := " a b c d e f g h"
251251
str := fmt.Sprintf("%s\n", files)
252252

253+
lastMoveSquare := Invalid
254+
255+
if len(b.history) > 0 {
256+
lastMoveSquare = b.history[len(b.history)-1].move.To
257+
}
258+
253259
for rank := int8(7); rank >= 0; rank-- {
254260
var s = fmt.Sprintf("%d ", rank+1)
255261
for file := int8(0); file < 8; file++ {
256-
s += fmt.Sprintf("%s ", symbol(b.data[square(rank, file)]))
262+
moved := " "
263+
if square(rank, file) == int8(lastMoveSquare) {
264+
moved = "*"
265+
}
266+
s += fmt.Sprintf("%s%s ", symbol(b.data[square(rank, file)]), moved)
257267
}
258268
if rank == 4 {
259269
color := "white"
@@ -262,6 +272,31 @@ func (b *Board) String() string {
262272
}
263273
s += fmt.Sprintf("\t(%d) %s's move", b.fullMoves, color)
264274
}
275+
if rank == 3 {
276+
c := ""
277+
if b.whiteCastle&castleShort != 0 {
278+
c += "K"
279+
}
280+
if b.whiteCastle&castleLong != 0 {
281+
c += "Q"
282+
}
283+
if b.blackCastle&castleShort != 0 {
284+
c += "k"
285+
}
286+
if b.blackCastle&castleLong != 0 {
287+
c += "q"
288+
}
289+
if len(c) == 0 {
290+
c = "-"
291+
}
292+
s += fmt.Sprintf("\tCasteling: %s", c)
293+
}
294+
if rank == 2 {
295+
gen := NewGenerator(b)
296+
if gen.CheckSimple() {
297+
s += fmt.Sprintf("\tCheck!")
298+
}
299+
}
265300
str += fmt.Sprintf("%s\n", s)
266301
}
267302
lastMove := ""

engine/generator.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package engine
22

3+
import "fmt"
4+
35
var (
46
nextRank int8 = 16
57
nextFile int8 = 1
@@ -55,6 +57,7 @@ type Generator struct {
5557
board *Board
5658
legalEnding []bool
5759
legalDelta []int8
60+
lastMoveSquare Square
5861
moves []Move
5962
kingSquare int8
6063
kingUnderCheck bool
@@ -186,9 +189,14 @@ func (g *Generator) reset() {
186189

187190
g.legalEnding = make([]bool, boardSize)
188191
g.legalDelta = make([]int8, boardSize)
192+
193+
if len(g.board.history) > 0 {
194+
g.lastMoveSquare = g.board.history[len(g.board.history)-1].move.To
195+
}
189196
}
190197

191198
func (g *Generator) sortMoves() {
199+
lastCapture := make([]Move, 0, len(g.moves))
192200
sorted := make([]Move, 0, len(g.moves))
193201
captured := make([]Move, 0, len(g.moves))
194202
promotions := make([]Move, 0, len(g.moves))
@@ -197,6 +205,8 @@ func (g *Generator) sortMoves() {
197205

198206
for _, move := range g.moves {
199207
switch {
208+
case move.To == g.lastMoveSquare:
209+
lastCapture = append(lastCapture, move)
200210
case move.Content != Empty:
201211
captured = append(captured, move)
202212
case move.Special == movePromotion:
@@ -208,7 +218,8 @@ func (g *Generator) sortMoves() {
208218
}
209219
}
210220

211-
// TODO 1. last moved piece capture
221+
// 1. last moved piece capture
222+
sorted = append(sorted, lastCapture...)
212223

213224
// 2. capture moves
214225
sorted = append(sorted, captured...)
@@ -223,6 +234,7 @@ func (g *Generator) sortMoves() {
223234
sorted = append(sorted, ordinary...)
224235

225236
if len(sorted) != len(g.moves) {
237+
fmt.Printf("len(g.moves): %d, len(sorted): %d\n", len(g.moves), len(sorted))
226238
panic("Sorted moves are missing moves!")
227239
}
228240

0 commit comments

Comments
 (0)