Added test for the starting position
This commit is contained in:
parent
1f940a6e60
commit
3da384f8fc
|
@ -72,41 +72,35 @@ for _ in countup(0, 63):
|
|||
empty.add(Piece(kind: Empty, color: None))
|
||||
|
||||
|
||||
proc algebraicToPosition(s: string): Location {.inline.} =
|
||||
func rankToColumn(rank: int): int =
|
||||
## Converts a chess rank (1-indexed)
|
||||
## into a 0-indexed column value for our
|
||||
## board. This converter is necessary because
|
||||
## chess positions are indexed differently with
|
||||
## respect to our internal representation
|
||||
const indeces = [7, 6, 5, 4, 3, 2, 1, 0]
|
||||
return indeces[rank - 1]
|
||||
|
||||
|
||||
proc algebraicToPosition*(s: string): Location {.inline.} =
|
||||
## Converts a square location from algebraic
|
||||
## notation to its corresponding row and column
|
||||
## in the chess grid (0 indexed)
|
||||
if len(s) != 2:
|
||||
raise newException(ValueError, "algebraic position must be of length 2")
|
||||
var s = s.toLowerAscii()
|
||||
|
||||
var s = s.toLowerAscii()
|
||||
if s[0] notin 'a'..'h':
|
||||
raise newException(ValueError, &"algebraic position has invalid first character ('{s[0]}')")
|
||||
if s[1] notin '1'..'8':
|
||||
raise newException(ValueError, &"algebraic position has invalid second character ('{s[1]}')")
|
||||
|
||||
let file = int(uint8(s[0]) - uint8('a'))
|
||||
var rank: int
|
||||
case s[1]:
|
||||
of '1':
|
||||
rank = 7
|
||||
of '2':
|
||||
rank = 6
|
||||
of '3':
|
||||
rank = 5
|
||||
of '4':
|
||||
rank = 4
|
||||
of '5':
|
||||
rank = 3
|
||||
of '6':
|
||||
rank = 2
|
||||
of '7':
|
||||
rank = 1
|
||||
of '8':
|
||||
rank = 0
|
||||
else:
|
||||
discard
|
||||
# Convert the rank character to a number
|
||||
let rank = rankToColumn(int(uint8(s[1]) - uint8('0')))
|
||||
return (rank, file)
|
||||
|
||||
|
||||
proc getPiece*(self: ChessBoard, square: string): Piece =
|
||||
## Gets the piece on the given square
|
||||
## in algebraic notation
|
||||
|
@ -116,16 +110,20 @@ proc getPiece*(self: ChessBoard, square: string): Piece =
|
|||
|
||||
proc `$`*(self: ChessBoard): string =
|
||||
result &= "- - - - - - - -"
|
||||
for row in self.grid:
|
||||
#const indeces = [7, 6, 5, 4, 3, 2, 1, 0]
|
||||
for i, row in self.grid:
|
||||
result &= "\n"
|
||||
for piece in row:
|
||||
if piece.kind == Empty:
|
||||
result &= " "
|
||||
continue
|
||||
if piece.color == White:
|
||||
result &= &"{char(piece.kind).toUpperAscii()} "
|
||||
else:
|
||||
result &= &"{char(piece.kind)} "
|
||||
result &= &"{rankToColumn(i + 1) + 1}"
|
||||
result &= "\n- - - - - - - -"
|
||||
result &= "\na b c d e f g h"
|
||||
|
||||
|
||||
proc newChessboard: ChessBoard =
|
||||
|
@ -325,4 +323,39 @@ proc newChessboardFromFEN*(state: string): ChessBoard =
|
|||
proc newDefaultChessboard*: ChessBoard =
|
||||
## Initializes a chessboard with the
|
||||
## starting position
|
||||
return newChessboardFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
||||
return newChessboardFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
||||
|
||||
|
||||
when isMainModule:
|
||||
proc testPiece(piece: Piece, kind: PieceKind, color: PieceColor) =
|
||||
doAssert piece.kind == kind and piece.color == color, &"expected piece of kind {kind} and color {color}, got {piece.kind} / {piece.color} instead"
|
||||
|
||||
echo "Running tests"
|
||||
var b = newDefaultChessboard()
|
||||
# Ensure pawns are in the correct location
|
||||
for loc in ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"]:
|
||||
testPiece(b.getPiece(loc), Pawn, White)
|
||||
for loc in ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"]:
|
||||
testPiece(b.getPiece(loc), Pawn, Black)
|
||||
# Rooks
|
||||
testPiece(b.getPiece("a1"), Rook, White)
|
||||
testPiece(b.getPiece("h1"), Rook, White)
|
||||
testPiece(b.getPiece("a8"), Rook, Black)
|
||||
testPiece(b.getPiece("h8"), Rook, Black)
|
||||
# Knights
|
||||
testPiece(b.getPiece("b1"), Knight, White)
|
||||
testPiece(b.getPiece("g1"), Knight, White)
|
||||
testPiece(b.getPiece("b8"), Knight, Black)
|
||||
testPiece(b.getPiece("g8"), Knight, Black)
|
||||
# Bishops
|
||||
testPiece(b.getPiece("c1"), Bishop, White)
|
||||
testPiece(b.getPiece("f1"), Bishop, White)
|
||||
testPiece(b.getPiece("c8"), Bishop, Black)
|
||||
testPiece(b.getPiece("f8"), Bishop, Black)
|
||||
# Kings
|
||||
testPiece(b.getPiece("e1"), King, White)
|
||||
testPiece(b.getPiece("e8"), King, Black)
|
||||
# Queens
|
||||
testPiece(b.getPiece("d1"), Queen, White)
|
||||
testPiece(b.getPiece("d8"), Queen, Black)
|
||||
echo "All tests were successful"
|
|
@ -1,10 +1,7 @@
|
|||
import board
|
||||
import board as chess
|
||||
|
||||
|
||||
var b = newDefaultChessboard()
|
||||
echo b
|
||||
echo b.getPiece("a2")
|
||||
echo b.getPiece("a7")
|
||||
echo b.getPiece("a1")
|
||||
echo b.getPiece("a8")
|
||||
var board = newDefaultChessboard()
|
||||
echo board
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue