libchess/pkg/board/move.go

74 lines
1.3 KiB
Go
Raw Permalink Normal View History

package board
import (
"fmt"
"strings"
)
// MoveProp is defined as
// <piece><disambiguity><captures><square><promote><check|mate>
type MoveProp uint16
// Available MoveProps
const (
QueenSideCastle MoveProp = 1 << iota
KingSideCastle
Capture
EnPassant
Check
Mate
inCheck
)
// Move is defined as
type Move struct {
Piece PieceType
FromRank *Rank
FromFile *File
To Square
PromoteTo PieceType
Props MoveProp
}
func (m Move) String() string {
out := ""
if m.HasProp(KingSideCastle) {
out += "O-O"
} else if m.HasProp(QueenSideCastle) {
out += "O-O-O"
} else {
if m.Piece != NoPieceType && m.Piece != Pawn {
out += strings.ToUpper(m.Piece.String())
}
if m.FromFile != nil {
out += m.FromFile.String()
}
if m.FromRank != nil {
out += m.FromRank.String()
}
if m.HasProp(Capture) {
out += "x"
}
out += m.To.String()
if m.PromoteTo != NoPieceType {
out += fmt.Sprintf("=%s", m.PromoteTo.String())
}
}
if m.HasProp(Check) {
out += "+"
} else if m.HasProp(Mate) {
out += "#"
}
return out
}
// AddProp adds the given MoveProp to this move.
func (m *Move) AddProp(prop MoveProp) {
m.Props = m.Props | prop
}
// HasProp returns true if this move has the given MoveProp.
func (m Move) HasProp(prop MoveProp) bool {
return m.Props&prop > 0
}