Minor improvements to board module
This commit is contained in:
parent
24d1cd0c82
commit
85648d883c
|
@ -15,6 +15,7 @@ import ../util/matrix
|
||||||
|
|
||||||
|
|
||||||
import std/strutils
|
import std/strutils
|
||||||
|
import std/strformat
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
|
@ -36,12 +37,14 @@ type
|
||||||
Position* = object
|
Position* = object
|
||||||
piece*: Piece
|
piece*: Piece
|
||||||
location*: tuple[row, col: int]
|
location*: tuple[row, col: int]
|
||||||
Board* = ref object
|
ChessBoard* = ref object
|
||||||
|
## A chess board object
|
||||||
grid: Matrix[Piece]
|
grid: Matrix[Piece]
|
||||||
ply: PieceColor
|
# Currently active color
|
||||||
|
turn: PieceColor
|
||||||
# Number of half moves since
|
# Number of half moves since
|
||||||
# last piece capture. Used
|
# last piece capture or pawn movement.
|
||||||
# for the 50-move rule
|
# Used for the 50-move rule
|
||||||
halfMoveClock: int
|
halfMoveClock: int
|
||||||
# Full move counter. Increments
|
# Full move counter. Increments
|
||||||
# every 2 ply
|
# every 2 ply
|
||||||
|
@ -67,7 +70,21 @@ proc algebraicToPosition(s: string): tuple[row, col: int] {.inline.} =
|
||||||
result = (int(uint8(s[0]) - (uint8('a') - 1)), int(uint8(s[1]) - uint8('0')))
|
result = (int(uint8(s[0]) - (uint8('a') - 1)), int(uint8(s[1]) - uint8('0')))
|
||||||
|
|
||||||
|
|
||||||
proc newChessboardFromFEN*(state: string): Board =
|
proc `$`*(self: ChessBoard): string =
|
||||||
|
result &= "- - - - - - - -"
|
||||||
|
for row in self.grid:
|
||||||
|
result &= "\n"
|
||||||
|
for piece in row:
|
||||||
|
if piece.kind == Empty:
|
||||||
|
result &= " "
|
||||||
|
if piece.color == White:
|
||||||
|
result &= &"{char(piece.kind).toUpperAscii()} "
|
||||||
|
else:
|
||||||
|
result &= &"{char(piece.kind)} "
|
||||||
|
result &= "\n- - - - - - - -"
|
||||||
|
|
||||||
|
|
||||||
|
proc newChessboardFromFEN*(state: string): ChessBoard =
|
||||||
## Initializes a chessboard with the
|
## Initializes a chessboard with the
|
||||||
## state encoded by the given FEN string
|
## state encoded by the given FEN string
|
||||||
new(result)
|
new(result)
|
||||||
|
@ -110,11 +127,11 @@ proc newChessboardFromFEN*(state: string): Board =
|
||||||
# Active color
|
# Active color
|
||||||
case c:
|
case c:
|
||||||
of 'w':
|
of 'w':
|
||||||
result.ply = White
|
result.turn = White
|
||||||
of 'b':
|
of 'b':
|
||||||
result.ply = Black
|
result.turn = Black
|
||||||
else:
|
else:
|
||||||
raise newException(ValueError, "invalid next move identifier in FEN string")
|
raise newException(ValueError, "invalid active color identifier in FEN string")
|
||||||
of 2:
|
of 2:
|
||||||
# Castling availability
|
# Castling availability
|
||||||
case c:
|
case c:
|
||||||
|
@ -142,7 +159,7 @@ proc newChessboardFromFEN*(state: string): Board =
|
||||||
result.enPassantSquare.location = state[index..index+1].algebraicToPosition()
|
result.enPassantSquare.location = state[index..index+1].algebraicToPosition()
|
||||||
# Just for cleanliness purposes, we fill in the other positional metadata as
|
# Just for cleanliness purposes, we fill in the other positional metadata as
|
||||||
# well
|
# well
|
||||||
result.enPassantSquare.piece.color = if result.ply == Black: White else: Black
|
result.enPassantSquare.piece.color = if result.turn == Black: White else: Black
|
||||||
result.enPassantSquare.piece.kind = Pawn
|
result.enPassantSquare.piece.kind = Pawn
|
||||||
# Square metadata is 2 bytes long
|
# Square metadata is 2 bytes long
|
||||||
inc(index)
|
inc(index)
|
||||||
|
@ -168,3 +185,7 @@ proc newChessboardFromFEN*(state: string): Board =
|
||||||
inc(index)
|
inc(index)
|
||||||
|
|
||||||
|
|
||||||
|
proc newDefaultChessboard*: ChessBoard =
|
||||||
|
## Initializes a chessboard with the
|
||||||
|
## starting position
|
||||||
|
return newChessboardFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
Loading…
Reference in New Issue