Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion board.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ func (b *Board) Transpose() *Board {
// 2 P P P P P P P P
// 1 R N B Q K B N R
func (b *Board) Draw() string {
return b.drawForWhite(false)
}

// Draw2 returns visual representation of the board useful for debugging.
// It is similar to Draw() except allows the caller to specify perspective
// and dark mode options
func (b *Board) Draw2(perspective Color, darkMode bool) string {
if perspective == Black {
return b.drawForBlack(darkMode)
} // else

return b.drawForWhite(darkMode)
}

// drawForWhite returns visual representation of the board from white's perspective
func (b *Board) drawForWhite(darkMode bool) string {
s := "\n A B C D E F G H\n"
for r := 7; r >= 0; r-- {
s += Rank(r).String()
Expand All @@ -177,7 +193,34 @@ func (b *Board) Draw() string {
if p == NoPiece {
s += "-"
} else {
s += p.String()
if darkMode {
s += p.DarkString()
} else {
s += p.String()
}
}
s += " "
}
s += "\n"
}
return s
}

// drawForBlack returns visual representation of the board from black's perspective
func (b *Board) drawForBlack(darkMode bool) string {
s := "\n H G F E D C B A\n"
for r := 0; r <= 7; r++ {
s += Rank(r).String()
for f := numOfSquaresInRow - 1; f >= 0; f-- {
p := b.Piece(NewSquare(File(f), Rank(r)))
if p == NoPiece {
s += "-"
} else {
if darkMode {
s += p.DarkString()
} else {
s += p.String()
}
}
s += " "
}
Expand Down
17 changes: 13 additions & 4 deletions piece.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,19 @@ func (p Piece) String() string {
return pieceUnicodes[int(p)]
}

// TODO: This is a constant slice
//
//nolint:gochecknoglobals // This is a constant slice.
var pieceUnicodes = []string{" ", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟"}
// DarkString is equivalent to String() except colors reversed for terminal
// windows in dark mode
func (p Piece) DarkString() string {
return pieceDarkUnicodes[int(p)]
}

// TODO: These are constant slices
var (
//nolint:gochecknoglobals // This is a constant slice.
pieceUnicodes = []string{" ", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟"}
//nolint:gochecknoglobals // This is a constant slice.
pieceDarkUnicodes = []string{" ", "♚", "♛", "♜", "♝", "♞", "♟", "♔", "♕", "♖", "♗", "♘", "♙"}
)

// getFENChar returns the FEN character representation of a piece
// Returns a single byte representing the piece.
Expand Down
Loading