Skip to content

Commit bc724bd

Browse files
committed
Add formatBoard; remove String representation for Board
1 parent 2823589 commit bc724bd

File tree

4 files changed

+81
-91
lines changed

4 files changed

+81
-91
lines changed

engine/board.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -323,80 +323,3 @@ func (b *Board) generateHash() int64 {
323323

324324
return key
325325
}
326-
327-
func (b *Board) String() string {
328-
files := " a b c d e f g h"
329-
str := fmt.Sprintf("%s\n", files)
330-
331-
lastMoveSquare := Invalid
332-
333-
if len(b.history) > 0 {
334-
lastMoveSquare = b.history[len(b.history)-1].move.To
335-
}
336-
337-
for rank := int8(7); rank >= 0; rank-- {
338-
var s = fmt.Sprintf("%d ", rank+1)
339-
for file := int8(0); file < 8; file++ {
340-
moved := " "
341-
if square(rank, file) == int8(lastMoveSquare) {
342-
moved = "*"
343-
}
344-
s += fmt.Sprintf("%s%s ", symbol(b.data[square(rank, file)]), moved)
345-
}
346-
if rank == 4 {
347-
color := "white"
348-
if b.sideToMove == Black {
349-
color = "black"
350-
}
351-
s += fmt.Sprintf("\t(%d) %s's move", b.fullMoves, color)
352-
}
353-
if rank == 3 {
354-
c := ""
355-
if b.whiteCastle&castleShort != 0 {
356-
c += "K"
357-
}
358-
if b.whiteCastle&castleLong != 0 {
359-
c += "Q"
360-
}
361-
if b.blackCastle&castleShort != 0 {
362-
c += "k"
363-
}
364-
if b.blackCastle&castleLong != 0 {
365-
c += "q"
366-
}
367-
if len(c) == 0 {
368-
c = "-"
369-
}
370-
s += fmt.Sprintf("\tCasteling: %s", c)
371-
}
372-
if rank == 2 {
373-
gen := NewGenerator(b)
374-
if gen.CheckSimple() {
375-
s += fmt.Sprintf("\tCheck!")
376-
}
377-
}
378-
str += fmt.Sprintf("%s\n", s)
379-
}
380-
lastMove := ""
381-
if len(b.history) > 0 {
382-
lastMove = b.history[len(b.history)-1].move.String()
383-
}
384-
str += fmt.Sprintf("%s\t%s\t", files, lastMove)
385-
386-
switch b.status {
387-
case statusCheck:
388-
str += "Check!"
389-
case statusDraw:
390-
str += "Draw!"
391-
case statusWhiteMates:
392-
str += "Mate! White wins."
393-
case statusBlackMates:
394-
str += "Mate! Black wins."
395-
case statusStaleMate:
396-
str += "Stale mate!"
397-
}
398-
399-
str += "\n"
400-
401-
return str
402-
}

engine/game.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ func (g *Game) Run() {
5656
g.board = NewBoard(in[4:])
5757

5858
} else if in == "print" || in == "p" {
59-
fmt.Printf("%s\n", g.board.String())
59+
fmt.Printf("%s\n", formatBoard(g.board))
6060

6161
} else if in == "search" || in == "s" {
6262
Search(g.board)
6363

6464
} else if in == "do" || in == "d" {
6565
g.board.MakeMove(Search(g.board))
66-
fmt.Printf("%s\n", g.board.String())
66+
fmt.Printf("%s\n", formatBoard(g.board))
6767

6868
} else if in == "eval" || in == "e" {
6969
fmt.Printf("Score: %d\n", Evaluate(g.board))
7070

7171
} else if in == "auto" || in == "a" {
7272
for g.board.status == statusNormal {
7373
g.board.MakeMove(Search(g.board))
74-
fmt.Printf("%s\n", g.board.String())
74+
fmt.Printf("%s\n", formatBoard(g.board))
7575
}
7676

7777
} else if m, err := createMove(in); err == nil {

engine/search.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package engine
22

3-
import (
4-
"fmt"
5-
"time"
6-
)
3+
import "time"
74

85
var (
96
searchVerbose = true
@@ -98,7 +95,6 @@ func (pv *pvSearch) alphaBeta(depth, alpha, beta int) int {
9895

9996
// TODO: index out of range
10097
if len(pv.pathLength) <= pv.board.ply {
101-
fmt.Printf("len(pv.PathLength): %d, pv.board.ply: %d\n", len(pv.pathLength), pv.board.ply)
10298
return 0
10399
}
104100
pv.pathLength[pv.board.ply] = pv.board.ply
@@ -184,12 +180,6 @@ func (pv *pvSearch) quiescence(alpha, beta int) int {
184180
}
185181
}
186182

187-
if pv.board.ply >= len(pv.pathLength) {
188-
fmt.Printf("%s\n\nalpha: %d, beta: %d\n", pv.board.String(), alpha, beta)
189-
190-
return 0
191-
}
192-
193183
pv.pathLength[pv.board.ply] = pv.board.ply
194184

195185
eval := Evaluate(pv.board)

engine/util.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,83 @@ func formatNodesCount(nodes int64) string {
121121
return fmt.Sprintf("%.2fM", float64(nodes)/1000000)
122122
}
123123

124+
func formatBoard(b *Board) string {
125+
files := " a b c d e f g h"
126+
str := fmt.Sprintf("%s\n", files)
127+
128+
lastMoveSquare := Invalid
129+
130+
if len(b.history) > 0 {
131+
lastMoveSquare = b.history[len(b.history)-1].move.To
132+
}
133+
134+
for rank := int8(7); rank >= 0; rank-- {
135+
var s = fmt.Sprintf("%d ", rank+1)
136+
for file := int8(0); file < 8; file++ {
137+
moved := " "
138+
if square(rank, file) == int8(lastMoveSquare) {
139+
moved = "*"
140+
}
141+
s += fmt.Sprintf("%s%s ", symbol(b.data[square(rank, file)]), moved)
142+
}
143+
if rank == 4 {
144+
color := "white"
145+
if b.sideToMove == Black {
146+
color = "black"
147+
}
148+
s += fmt.Sprintf("\t(%d) %s's move", b.fullMoves, color)
149+
}
150+
if rank == 3 {
151+
c := ""
152+
if b.whiteCastle&castleShort != 0 {
153+
c += "K"
154+
}
155+
if b.whiteCastle&castleLong != 0 {
156+
c += "Q"
157+
}
158+
if b.blackCastle&castleShort != 0 {
159+
c += "k"
160+
}
161+
if b.blackCastle&castleLong != 0 {
162+
c += "q"
163+
}
164+
if len(c) == 0 {
165+
c = "-"
166+
}
167+
s += fmt.Sprintf("\tCasteling: %s", c)
168+
}
169+
if rank == 2 {
170+
gen := NewGenerator(b)
171+
if gen.CheckSimple() {
172+
s += fmt.Sprintf("\tCheck!")
173+
}
174+
}
175+
str += fmt.Sprintf("%s\n", s)
176+
}
177+
lastMove := ""
178+
if len(b.history) > 0 {
179+
lastMove = b.history[len(b.history)-1].move.String()
180+
}
181+
str += fmt.Sprintf("%s\t%s\t", files, lastMove)
182+
183+
switch b.status {
184+
case statusCheck:
185+
str += "Check!"
186+
case statusDraw:
187+
str += "Draw!"
188+
case statusWhiteMates:
189+
str += "Mate! White wins."
190+
case statusBlackMates:
191+
str += "Mate! Black wins."
192+
case statusStaleMate:
193+
str += "Stale mate!"
194+
}
195+
196+
str += "\n"
197+
198+
return str
199+
}
200+
124201
func abs(v int8) int8 {
125202
if v >= 0 {
126203
return v

0 commit comments

Comments
 (0)