269 lines
4.3 KiB
Go
269 lines
4.3 KiB
Go
package board
|
|
|
|
// File represents a file of squares on the chess board.
|
|
type File int8
|
|
|
|
// The following files exist:
|
|
const (
|
|
FileA File = iota
|
|
FileB
|
|
FileC
|
|
FileD
|
|
FileE
|
|
FileF
|
|
FileG
|
|
FileH
|
|
)
|
|
|
|
// FileChars contains the available file letters a-h.
|
|
const FileChars = "abcdefgh"
|
|
|
|
func (f File) String() string {
|
|
return FileChars[f : f+1]
|
|
}
|
|
|
|
// Next returns the next File.
|
|
func (f File) Next() File {
|
|
return File(f + 1)
|
|
}
|
|
|
|
// Previous returns the previous File.
|
|
func (f File) Previous() File {
|
|
return File(f - 1)
|
|
}
|
|
|
|
// Shift returns the theoretical File after shifting by the given int.
|
|
func (f File) Shift(i int) File {
|
|
return File(int(f) + i)
|
|
}
|
|
|
|
// IsValid returns true if this file is valid.
|
|
func (f File) IsValid() bool {
|
|
return FileA <= f && f <= FileH
|
|
}
|
|
|
|
// Rank represents a rank of squares on the chess board.
|
|
type Rank int8
|
|
|
|
// The following ranks exist:
|
|
const (
|
|
Rank1 Rank = iota
|
|
Rank2
|
|
Rank3
|
|
Rank4
|
|
Rank5
|
|
Rank6
|
|
Rank7
|
|
Rank8
|
|
)
|
|
|
|
// RankChars contains all rank chars
|
|
const RankChars = "12345678"
|
|
|
|
func (r Rank) String() string {
|
|
return RankChars[r : r+1]
|
|
}
|
|
|
|
// Next returns the next Rank.
|
|
func (r Rank) Next() Rank {
|
|
return Rank(r + 1)
|
|
}
|
|
|
|
// Previous returns the previous Rank.
|
|
func (r Rank) Previous() Rank {
|
|
return Rank(r - 1)
|
|
}
|
|
|
|
// Shift returns the theoretical Rank after shifting the given int.
|
|
func (r Rank) Shift(i int) Rank {
|
|
return Rank(int(r) + i)
|
|
}
|
|
|
|
// IsValid returns true if this is a valid rank.
|
|
func (r Rank) IsValid() bool {
|
|
return Rank1 <= r && r <= Rank8
|
|
}
|
|
|
|
// Color is defined as
|
|
type Color uint8
|
|
|
|
// Two colors exist
|
|
const (
|
|
NoColor Color = iota
|
|
White
|
|
Black
|
|
)
|
|
|
|
func (c Color) String() string {
|
|
switch c {
|
|
case White:
|
|
return "w"
|
|
case Black:
|
|
return "b"
|
|
}
|
|
return "-"
|
|
}
|
|
|
|
// Name returns the name of the color.
|
|
func (c Color) Name() string {
|
|
switch c {
|
|
case White:
|
|
return "White"
|
|
case Black:
|
|
return "Black"
|
|
}
|
|
return "NoColor"
|
|
}
|
|
|
|
// Other returns the opposite color.
|
|
func (c Color) Other() Color {
|
|
switch c {
|
|
case White:
|
|
return Black
|
|
case Black:
|
|
return White
|
|
}
|
|
return NoColor
|
|
}
|
|
|
|
// Side is defined as
|
|
type Side uint8
|
|
|
|
// The sides of the board are .
|
|
const (
|
|
QueenSide Side = iota
|
|
KingSide
|
|
)
|
|
|
|
// Square represents a square on the chess board.
|
|
type Square int8
|
|
|
|
// The following Squares exist
|
|
const (
|
|
NoSquare Square = iota - 1
|
|
A1
|
|
B1
|
|
C1
|
|
D1
|
|
E1
|
|
F1
|
|
G1
|
|
H1
|
|
A2
|
|
B2
|
|
C2
|
|
D2
|
|
E2
|
|
F2
|
|
G2
|
|
H2
|
|
A3
|
|
B3
|
|
C3
|
|
D3
|
|
E3
|
|
F3
|
|
G3
|
|
H3
|
|
A4
|
|
B4
|
|
C4
|
|
D4
|
|
E4
|
|
F4
|
|
G4
|
|
H4
|
|
A5
|
|
B5
|
|
C5
|
|
D5
|
|
E5
|
|
F5
|
|
G5
|
|
H5
|
|
A6
|
|
B6
|
|
C6
|
|
D6
|
|
E6
|
|
F6
|
|
G6
|
|
H6
|
|
A7
|
|
B7
|
|
C7
|
|
D7
|
|
E7
|
|
F7
|
|
G7
|
|
H7
|
|
A8
|
|
B8
|
|
C8
|
|
D8
|
|
E8
|
|
F8
|
|
G8
|
|
H8
|
|
)
|
|
|
|
// StrToSquareMap maps all Squares to the string representation.
|
|
var StrToSquareMap = map[string]Square{
|
|
"a1": A1, "a2": A2, "a3": A3, "a4": A4, "a5": A5, "a6": A6, "a7": A7, "a8": A8,
|
|
"b1": B1, "b2": B2, "b3": B3, "b4": B4, "b5": B5, "b6": B6, "b7": B7, "b8": B8,
|
|
"c1": C1, "c2": C2, "c3": C3, "c4": C4, "c5": C5, "c6": C6, "c7": C7, "c8": C8,
|
|
"d1": D1, "d2": D2, "d3": D3, "d4": D4, "d5": D5, "d6": D6, "d7": D7, "d8": D8,
|
|
"e1": E1, "e2": E2, "e3": E3, "e4": E4, "e5": E5, "e6": E6, "e7": E7, "e8": E8,
|
|
"f1": F1, "f2": F2, "f3": F3, "f4": F4, "f5": F5, "f6": F6, "f7": F7, "f8": F8,
|
|
"g1": G1, "g2": G2, "g3": G3, "g4": G4, "g5": G5, "g6": G6, "g7": G7, "g8": G8,
|
|
"h1": H1, "h2": H2, "h3": H3, "h4": H4, "h5": H5, "h6": H6, "h7": H7, "h8": H8,
|
|
}
|
|
|
|
// NewSquare returns an initialized Square.
|
|
func NewSquare(f File, r Rank) Square {
|
|
return Square((int(r) * 8) + int(f))
|
|
}
|
|
|
|
// Color returns the color of this Square.
|
|
func (sq Square) Color() Color {
|
|
if ((sq / 8) % 2) == (sq % 2) {
|
|
return Black
|
|
}
|
|
return White
|
|
}
|
|
|
|
// File returns the File this Square is in.
|
|
func (sq Square) File() File {
|
|
return File(uint(sq) % 8)
|
|
}
|
|
|
|
// Rank returns the Rank the Square is in.
|
|
func (sq Square) Rank() Rank {
|
|
return Rank(uint(sq) / 8)
|
|
}
|
|
|
|
// Side returns the side of board this square is on.
|
|
func (sq Square) Side() Side {
|
|
if sq.File() <= FileA && sq.File() <= FileD {
|
|
return QueenSide
|
|
}
|
|
return KingSide
|
|
}
|
|
|
|
func (sq Square) String() string {
|
|
if sq == NoSquare {
|
|
return "-"
|
|
}
|
|
return sq.File().String() + sq.Rank().String()
|
|
}
|
|
|
|
// IsValid returns true if this is a valid square.
|
|
func (sq Square) IsValid() bool {
|
|
return int(A1) <= int(sq) && int(sq) <= int(H8)
|
|
}
|
|
|
|
// Bitboard returns the Bitboard for this square.
|
|
func (sq Square) Bitboard() Bitboard {
|
|
return Bitboard(uint64(1) << (uint8(63) - uint8(sq)))
|
|
}
|