ches: added fen

FEN is a notation to describe a state
of a chess game.
Added a initChess function, which is able
to create a chess object from FEN
master
TiynGER 4 years ago
parent c195f2da6a
commit a383e52bd7

@ -1,12 +1,15 @@
import algorithm
import sequtils
import strutils
import sugar
import tables import tables
from strutils import parseInt
type type
Color* = enum Color* = enum
## `Color` describes the possible color of players. ## `Color` describes the possible color of players.
Black = -1, Black = -1,
White = 1 White = 1
Board = array[0..119, int] ## \ Board = array[120, int] ## \
## `Board` saves the position of the chess pieces. ## `Board` saves the position of the chess pieces.
CastleRights = tuple CastleRights = tuple
## `CastleRights` contains the rights to castling for each player. ## `CastleRights` contains the rights to castling for each player.
@ -41,6 +44,8 @@ type
const const
Block = 999 ## \ Block = 999 ## \
## `Block` is the value assigned to empty blocked fields in a board. ## `Block` is the value assigned to empty blocked fields in a board.
Empty = 0 ## \
## `Empty` is the value assigned to empty fields on a board.
WPawn* = 1 WPawn* = 1
## `WPawn` is the value assigned to a square in a board with a white pawn. ## `WPawn` is the value assigned to a square in a board with a white pawn.
WKnight* = 2 ## \ WKnight* = 2 ## \
@ -226,7 +231,7 @@ proc initBoard(): Board =
Block, Block, Block, Block, Block, Block, Block, Block, Block, Block] Block, Block, Block, Block, Block, Block, Block, Block, Block, Block]
return board return board
proc initBoard(board: array[0..63, int]): Board = proc initBoard(board: array[64, int]): Board =
## Create and return a board with pieces in position of choice, described in ## Create and return a board with pieces in position of choice, described in
## `board`. ## `board`.
let board = [ let board = [
@ -255,11 +260,10 @@ proc initBoard(board: array[0..63, int]): Board =
proc initChess*(): Chess = proc initChess*(): Chess =
## Create and return a Chess object. ## Create and return a Chess object.
let chess = Chess(board: initBoard(), let chess = Chess(board: initBoard(),
to_move: Color.White, previousBoard: @[], previousCastleRights: @[], to_move: Color.White, castleRights: (true, true, true, true))
fiftyMoveCounter: 0, castleRights: (true, true, true, true))
return chess return chess
proc initChess(board: array[0..63, int], color: Color): Chess = proc initChess(board: array[64, int], color: Color): Chess =
## Create and return a Chess object based on a position of choice. ## Create and return a Chess object based on a position of choice.
## `board` describes the pieces, `color` the color that is about to move. ## `board` describes the pieces, `color` the color that is about to move.
let board = initBoard(board) let board = initBoard(board)
@ -282,6 +286,49 @@ proc initChess(board: array[0..63, int], color: Color): Chess =
to_move: color, castleRights: (wk, wq, bk, bq)) to_move: color, castleRights: (wk, wq, bk, bq))
return chess return chess
proc initChess*(fen: string): Chess =
## Create and return a Chess object from `fen`.
var revPieceChar = toSeq(PieceChar.pairs).map(y => (y[1], y[0])).toTable
revPieceChar[" "] = Empty
var fenArr = fen.split(" ")
var squares: array[64, int]
var tmp: seq[int]
var squaresInd: int
for i, subc in fenArr[0].reversed():
if subc == '/':
continue
if subc.isDigit():
for j in 1..parseInt($subc):
squares[squaresInd] = revPieceChar[" "]
squaresInd += 1
continue
else:
squares[squaresInd] = revPieceChar[$subc]
squaresInd += 1
var board = initBoard(squares)
var toMove: Color
if fenArr[1] == "w":
toMove = Color.White
else:
toMove = Color.Black
var castleRights: CastleRights
if fenArr[2].contains("K"):
castleRights.wk = true
if fenArr[2].contains("Q"):
castleRights.wq = true
if fenArr[2].contains("k"):
castleRights.bk = true
if fenArr[2].contains("q"):
castleRights.bq = true
if fenArr[3] != "-":
if toMove == Color.White:
board[fieldToInd(fenArr[3])] = BEnPassant
else:
board[fieldToInd(fenArr[3])] = WEnPassant
var fiftyMoveCounter = parseInt(fenArr[4])
return Chess(board: board, toMove: toMove, castleRights: castleRights,
fiftyMoveCounter: fiftyMoveCounter)
proc echoBoard*(chess: Chess, color: Color) = proc echoBoard*(chess: Chess, color: Color) =
## Prints out the given `board` with its pieces as characters and line ## Prints out the given `board` with its pieces as characters and line
## indices from perspecive of `color`. ## indices from perspecive of `color`.
@ -543,8 +590,7 @@ proc genPawnPromotion(move: Move, color: Color): seq[Move] =
var promotions = newSeq[Move]() var promotions = newSeq[Move]()
let start = move.start let start = move.start
let dest = move.dest let dest = move.dest
if (fieldToInd("h8") <= dest and dest <= fieldToInd("a8")) or if (fieldToInd("h8") >= dest) or (fieldToInd("a8") >= dest):
(fieldToInd("h1") <= dest and dest <= fieldToInd("a1")):
for piece in WKnight..WQueen: for piece in WKnight..WQueen:
promotions.add(getMove(start, dest, piece, color)) promotions.add(getMove(start, dest, piece, color))
return promotions return promotions

@ -83,188 +83,53 @@ testSuite ChessTest of TestSuite:
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
method testIsStalemateTrueWhite() = method testIsStalemateTrueWhite() =
self.chess = initChess([ self.chess = initChess("8/5kp1/8/5b2/1p6/n1p1p1p1/Kbpp2p1/nrqr4 w - - 0 0")
0, 0, 0, 0, BRook, BQueen, BRook, BKnight,
0, BPawn, 0, 0, BPawn, BPawn, BBishop, WKing,
0, BPawn, 0, BPawn, 0, BPawn, 0, BKnight,
0, 0, 0, 0, 0, 0, BPawn, 0,
0, 0, BBishop, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, BPawn, BKing, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.White)
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
method testIsStalemateTrueBlack() = method testIsStalemateTrueBlack() =
self.chess = initChess([ self.chess = initChess("8/5KP1/8/5B2/1P6/N1P1P1P1/kBPP2P1/NRQR4 b - - 0 0")
0, 0, 0, 0, WRook, WQueen, WRook, WKnight,
0, WPawn, 0, 0, WPawn, WPawn, WBishop, BKing,
0, WPawn, 0, WPawn, 0, WPawn, 0, WKnight,
0, 0, 0, 0, 0, 0, WPawn, 0,
0, 0, WBishop, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, WPawn, WKing, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
method testIsStalemateInsufficientMaterialTrue() = method testIsStalemateInsufficientMaterialTrue() =
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/8/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/5N2/N7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, WKnight,
0, 0, WKnight, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/5n2/n7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BKnight,
0, 0, BKnight, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/n7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BKnight,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/N7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, WKnight,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/b7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BBishop,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/B7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, WBishop,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(self.chess.isStalemate(Color.Black)) self.check(self.chess.isStalemate(Color.Black))
self.check(self.chess.isStalemate(Color.White)) self.check(self.chess.isStalemate(Color.White))
method testIsStalemateInsufficientMaterialFalse() = method testIsStalemateInsufficientMaterialFalse() =
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/p7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
self.check(not self.chess.isStalemate(Color.White)) self.check(not self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/P7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
self.check(not self.chess.isStalemate(Color.White)) self.check(not self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/r7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
self.check(not self.chess.isStalemate(Color.White)) self.check(not self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/R7/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
self.check(not self.chess.isStalemate(Color.White)) self.check(not self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/p5B1/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, WBishop, 0, 0, 0, 0, 0, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
self.check(not self.chess.isStalemate(Color.White)) self.check(not self.chess.isStalemate(Color.White))
self.chess = initChess([ self.chess = initChess("8/2k5/8/8/P4b2/8/4K3/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, BBishop, 0, 0, 0, 0, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
self.check(not self.chess.isStalemate(Color.Black)) self.check(not self.chess.isStalemate(Color.Black))
self.check(not self.chess.isStalemate(Color.White)) self.check(not self.chess.isStalemate(Color.White))
@ -295,16 +160,7 @@ testSuite ChessTest of TestSuite:
self.check(not self.chess.isDrawClaimable()) self.check(not self.chess.isDrawClaimable())
method testIsDrawClaimableFiftyMoveRuleTrue() = method testIsDrawClaimableFiftyMoveRuleTrue() =
self.chess = initChess([ self.chess = initChess("4k3/1R6/8/5K2/7B/8/r7/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BRook,
0, 0, 0, 0, 0, 0, 0, 0,
WBishop, 0, 0, 0, 0, 0, 0, 0,
0, 0, WKing, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, WRook, 0,
0, 0, 0, BKing, 0, 0, 0, 0
], Color.Black)
self.chess.checkedMove(notationToMove("a2a5", Color.Black)) self.chess.checkedMove(notationToMove("a2a5", Color.Black))
self.chess.checkedMove(notationToMove("f5g6", Color.White)) self.chess.checkedMove(notationToMove("f5g6", Color.White))
self.chess.checkedMove(notationToMove("a5e5", Color.Black)) self.chess.checkedMove(notationToMove("a5e5", Color.Black))
@ -408,16 +264,7 @@ testSuite ChessTest of TestSuite:
self.check(self.chess.isDrawClaimable()) self.check(self.chess.isDrawClaimable())
method testIsDrawClaimableFiftyMoveRuleFalseNinetyFour() = method testIsDrawClaimableFiftyMoveRuleFalseNinetyFour() =
self.chess = initChess([ self.chess = initChess("4k3/1R6/8/5K2/7B/8/r7/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BRook,
0, 0, 0, 0, 0, 0, 0, 0,
WBishop, 0, 0, 0, 0, 0, 0, 0,
0, 0, WKing, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, WRook, 0,
0, 0, 0, BKing, 0, 0, 0, 0
], Color.Black)
self.chess.checkedMove(notationToMove("a2a5", Color.Black)) self.chess.checkedMove(notationToMove("a2a5", Color.Black))
self.chess.checkedMove(notationToMove("f5g6", Color.White)) self.chess.checkedMove(notationToMove("f5g6", Color.White))
self.chess.checkedMove(notationToMove("a5e5", Color.Black)) self.chess.checkedMove(notationToMove("a5e5", Color.Black))
@ -521,16 +368,7 @@ testSuite ChessTest of TestSuite:
self.check(self.chess.isDrawClaimable()) self.check(self.chess.isDrawClaimable())
method testIsDrawClaimableFiftyMoveRuleFalseCapture() = method testIsDrawClaimableFiftyMoveRuleFalseCapture() =
self.chess = initChess([ self.chess = initChess("4k3/1R6/5p2/5K2/7B/8/r7/8 b - - 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, BRook,
0, 0, 0, 0, 0, 0, 0, 0,
WBishop, 0, 0, 0, 0, 0, 0, 0,
0, 0, WKing, 0, 0, 0, 0, 0,
0, 0, BPawn, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, WRook, 0,
0, 0, 0, BKing, 0, 0, 0, 0
], Color.Black)
self.chess.checkedMove(notationToMove("a2a5", Color.Black)) self.chess.checkedMove(notationToMove("a2a5", Color.Black))
self.chess.checkedMove(notationToMove("f5g6", Color.White)) self.chess.checkedMove(notationToMove("f5g6", Color.White))
self.chess.checkedMove(notationToMove("a5e5", Color.Black)) self.chess.checkedMove(notationToMove("a5e5", Color.Black))
@ -649,17 +487,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnSingleFalseIntoEnemyPiece() = method testCheckedMovePawnSingleFalseIntoEnemyPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/8/8/pppppppp/PPPPPPPP/8/8/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "4" & $file & "5", Color.White)) test = self.chess.checkedMove(notationToMove($file & "4" & $file & "5", Color.White))
self.check(not test) self.check(not test)
@ -668,17 +496,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnSingleFalseIntoOwnPiece() = method testCheckedMovePawnSingleFalseIntoOwnPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/pppppppp/pppppppp/8/8/PPPPPPPP/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) test = self.chess.checkedMove(notationToMove($file & "2" & $file & "3", Color.White))
self.check(not test) self.check(not test)
@ -696,17 +514,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnDoubleFalseAlreadyMoved() = method testCheckedMovePawnDoubleFalseAlreadyMoved() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/8/pppppppp/8/8/PPPPPPPP/8/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White))
self.check(not test) self.check(not test)
@ -715,17 +523,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnDoubleFalseThroughEnemyPiece() = method testCheckedMovePawnDoubleFalseThroughEnemyPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/pppppppp/PPPPPPPP/8/8/pppppppp/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White))
self.check(not test) self.check(not test)
@ -734,17 +532,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnDoubleFalseThroughOwnPiece() = method testCheckedMovePawnDoubleFalseThroughOwnPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/pppppppp/pppppppp/8/8/PPPPPPPP/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White))
self.check(not test) self.check(not test)
@ -753,17 +541,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnDoubleFalseIntoEnemyPiece() = method testCheckedMovePawnDoubleFalseIntoEnemyPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/pppppppp/8/PPPPPPPP/pppppppp/8/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White))
self.check(not test) self.check(not test)
@ -772,17 +550,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnDoubleFalseIntoOwnPiece() = method testCheckedMovePawnDoubleFalseIntoOwnPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/pppppppp/8/pppppppp/PPPPPPPP/8/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
for file in "abcdefgh": for file in "abcdefgh":
test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) test = self.chess.checkedMove(notationToMove($file & "3" & $file & "5", Color.White))
self.check(not test) self.check(not test)
@ -791,16 +559,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnCaptureTrueWhite() = method testCheckedMovePawnCaptureTrueWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/8/8/pppppppp/PPPPPPPP/8/8/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -819,16 +578,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnCaptureTrueBlack() = method testCheckedMovePawnCaptureTrueBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/8/8/pppppppp/PPPPPPPP/8/8/R3K2R b - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.Black)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -847,16 +597,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnCaptureFalseWhite() = method testCheckedMovePawnCaptureFalseWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/8/8/8/PPPPPPPP/8/8/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -875,16 +616,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnCaptureFalseBlack() = method testCheckedMovePawnCaptureFalseBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/8/8/pppppppp/8/8/8/R3K2R b - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.Black)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -903,16 +635,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnEnPassantTrueWhite() = method testCheckedMovePawnEnPassantTrueWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -953,16 +676,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnEnPassantTrueBlack() = method testCheckedMovePawnEnPassantTrueBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R b - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.Black)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -1003,16 +717,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnEnPassantFalseWhite() = method testCheckedMovePawnEnPassantFalseWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -1053,16 +758,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMovePawnEnPassantFalseBlack() = method testCheckedMovePawnEnPassantFalseBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R b - - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.Black)
var str: string var str: string
str = "abcdefgh" str = "abcdefgh"
for ind, file in str: for ind, file in str:
@ -1104,16 +800,7 @@ testSuite ChessTest of TestSuite:
## Tests for King moves ## Tests for King moves
method testCheckedMoveKingCastleTrueWhite() = method testCheckedMoveKingCastleTrueWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(test) self.check(test)
@ -1123,16 +810,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveKingCastleTrueBlack() = method testCheckedMoveKingCastleTrueBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R b KQkq - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.Black)
self.chess = pos self.chess = pos
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(test) self.check(test)
@ -1142,16 +820,10 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveKingCastleFalseAlreadyMovedKing() = method testCheckedMoveKingCastleFalseAlreadyMovedKing() =
var test: bool var test: bool
let pos = initChess([ var pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R b KQkq - 0 0")
WRook, 0, 0, 0, WKing, 0, 0, WRook, pos.checkedMove(notationToMove("e8d8", Color.Black))
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, pos.checkedMove(notationToMove("e1d1", Color.White))
0, 0, 0, 0, 0, 0, 0, 0, self.chess = pos
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, 0, 0, BKing, 0, 0, BRook
], Color.White)
self.chess.checkedMove(notationToMove("d1e1", Color.White)) self.chess.checkedMove(notationToMove("d1e1", Color.White))
self.chess.checkedMove(notationToMove("d8e8", Color.White)) self.chess.checkedMove(notationToMove("d8e8", Color.White))
self.chess = pos self.chess = pos
@ -1166,16 +838,11 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveKingCastleFalseAlreadyMovedRook() = method testCheckedMoveKingCastleFalseAlreadyMovedRook() =
var test: bool var test: bool
let pos = initChess([ var pos = initChess("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 0")
0, WRook, 0, WKing, 0, 0, WRook, 0, pos.checkedMove(notationToMove("a1b1", Color.White))
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, pos.checkedMove(notationToMove("a8b8", Color.Black))
0, 0, 0, 0, 0, 0, 0, 0, pos.checkedMove(notationToMove("h1g1", Color.White))
0, 0, 0, 0, 0, 0, 0, 0, pos.checkedMove(notationToMove("h8g8", Color.Black))
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
0, BRook, 0, BKing, 0, 0, BRook, 0
], Color.White)
self.chess = pos self.chess = pos
self.chess.checkedMove(notationToMove("b1a1", Color.White)) self.chess.checkedMove(notationToMove("b1a1", Color.White))
self.chess.checkedMove(notationToMove("b8a8", Color.Black)) self.chess.checkedMove(notationToMove("b8a8", Color.Black))
@ -1192,148 +859,85 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveKingCastleFalseThroughCheck() = method testCheckedMoveKingCastleFalseThroughCheck() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/pppRpRpp/8/8/8/8/PPPrPrPP/R3K2R w KQkq - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, WPawn, BRook, WPawn, BRook, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, WRook, BPawn, WRook, BPawn, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
self.chess.checkedMove(notationToMove("e1g1", Color.White)) self.chess.checkedMove(notationToMove("e1g1", Color.White))
self.check(not test) self.check(not test)
self.chess = initChess("r3k2r/pppRpRpp/8/8/8/8/PPPrPrPP/R3K2R b KQkq - 0 0")
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
test = self.chess.checkedMove(notationToMove("e8g8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8g8", Color.Black))
self.check(not test) self.check(not test)
method testCheckedMoveKingCastleFalseIntoCheck() = method testCheckedMoveKingCastleFalseIntoCheck() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r3k2r/ppRpppRp/8/8/8/8/PPrPPPrP/R3K2R w KQkq - 0 0")
WRook, 0, 0, WKing, 0, 0, 0, WRook,
WPawn, BRook, WPawn, WPawn, WPawn, BRook, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, WRook, BPawn, BPawn, BPawn, WRook, BPawn, BPawn,
BRook, 0, 0, BKing, 0, 0, 0, BRook
], Color.White)
self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
self.chess.checkedMove(notationToMove("e1g1", Color.White)) self.chess.checkedMove(notationToMove("e1g1", Color.White))
self.check(not test) self.check(not test)
self.chess = initChess("r3k2r/ppRpppRp/8/8/8/8/PPrPPPrP/R3K2R b KQkq - 0 0")
test = self.chess.checkedMove(notationToMove("e8g8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8g8", Color.Black))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
method testCheckedMoveKingCastleFalseThroughOwnPiece() = method testCheckedMoveKingCastleFalseThroughOwnPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r2bkb1r/pppppppp/8/8/8/8/PPPPPPPP/R2BKB1R w KQkq - 0 0")
WRook, 0, WBishop, WKing, WBishop, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, BBishop, BKing, BBishop, 0, 0, BRook
], Color.White)
self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
self.chess.checkedMove(notationToMove("e1g1", Color.White)) self.chess.checkedMove(notationToMove("e1g1", Color.White))
self.check(not test) self.check(not test)
self.chess = initChess("r2bkb1r/pppppppp/8/8/8/8/PPPPPPPP/R2BKB1R b KQkq - 0 0")
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
test = self.chess.checkedMove(notationToMove("e8g8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8g8", Color.Black))
self.check(not test) self.check(not test)
method testCheckedMoveKingCastleFalseThroughEnemyPiece() = method testCheckedMoveKingCastleFalseThroughEnemyPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r2BkB1r/pppppppp/8/8/8/8/PPPPPPPP/R2bKb1R w KQkq - 0 0")
WRook, 0, BBishop, WKing, BBishop, 0, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, 0, WBishop, BKing, WBishop, 0, 0, BRook
], Color.White)
self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
self.chess.checkedMove(notationToMove("e1g1", Color.White)) self.chess.checkedMove(notationToMove("e1g1", Color.White))
self.check(not test) self.check(not test)
self.chess = initChess("r2BkB1r/pppppppp/8/8/8/8/PPPPPPPP/R2bKb1R b KQkq - 0 0")
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
test = self.chess.checkedMove(notationToMove("e8g8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8g8", Color.Black))
self.check(not test) self.check(not test)
method testCheckedMoveKingCastleFalseIntoOwnPiece() = method testCheckedMoveKingCastleFalseIntoOwnPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r1b1k1br/pppppppp/8/8/8/8/PPPPPPPP/R1b1K1bR w KQkq - 0 0")
WRook, WBishop, 0, WKing, 0, WBishop, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, BBishop, 0, BKing, 0, BBishop, 0, BRook
], Color.White)
self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
self.chess.checkedMove(notationToMove("e1g1", Color.White)) self.chess.checkedMove(notationToMove("e1g1", Color.White))
self.check(not test) self.check(not test)
self.chess = initChess("r1b1k1br/pppppppp/8/8/8/8/PPPPPPPP/R1b1K1bR b KQkq - 0 0")
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
test = self.chess.checkedMove(notationToMove("e8g8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8g8", Color.Black))
self.check(not test) self.check(not test)
method testCheckedMoveKingCastleFalseIntoEnemyPiece() = method testCheckedMoveKingCastleFalseIntoEnemyPiece() =
var test: bool var test: bool
let pos = initChess([ self.chess = initChess("r1B1k1Br/pppppppp/8/8/8/8/PPPPPPPP/R1B1K1BR w KQkq - 0 0")
WRook, BBishop, 0, WKing, 0, BBishop, 0, WRook,
WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn,
BRook, WBishop, 0, BKing, 0, WBishop, 0, BRook
], Color.White)
self.chess = pos
test = self.chess.checkedMove(notationToMove("e1c1", Color.White)) test = self.chess.checkedMove(notationToMove("e1c1", Color.White))
self.check(not test) self.check(not test)
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
self.chess.checkedMove(notationToMove("e1g1", Color.White)) self.chess.checkedMove(notationToMove("e1g1", Color.White))
self.check(not test) self.check(not test)
self.chess = initChess("r1B1k1Br/pppppppp/8/8/8/8/PPPPPPPP/R1B1K1BR b KQkq - 0 0")
test = self.chess.checkedMove(notationToMove("e8c8", Color.Black))
self.check(not test)
test = self.chess.checkedMove(notationToMove("e8g8", Color.Black)) test = self.chess.checkedMove(notationToMove("e8g8", Color.Black))
self.check(not test) self.check(not test)
method testCheckedMoveKingWhite() = method testCheckedMoveKingWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/8/3PKp2/8/8/8/8 w - f6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, WEnPassant, 0, 0, 0,
0, 0, BPawn, WKing, WPawn, 0, 0, 0,
0, 0, BEnPassant, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.White)
let start = "e5" let start = "e5"
let legalMoves = ["f4", "d4", "f5", "f6", "e6", "d6"] let legalMoves = ["f4", "d4", "f5", "f6", "e6", "d6"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1350,16 +954,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveKingBlack() = method testCheckedMoveKingBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/8/3pkP2/8/8/8/8 b - f6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, BEnPassant, 0, 0, 0,
0, 0, WPawn, BKing, BPawn, 0, 0, 0,
0, 0, WEnPassant, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.Black)
let start = "e5" let start = "e5"
let legalMoves = ["f4", "d4", "f5", "f6", "e4", "d6"] let legalMoves = ["f4", "d4", "f5", "f6", "e4", "d6"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1377,16 +972,7 @@ testSuite ChessTest of TestSuite:
## Tests for Bishop moves ## Tests for Bishop moves
method testCheckedMoveBishopWhite() = method testCheckedMoveBishopWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/4p3/3B4/2P5/8/8/8 w - c6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, BEnPassant, 0, WPawn, 0, 0,
0, 0, 0, 0, WBishop, 0, 0, 0,
0, 0, 0, BPawn, 0, WEnPassant, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.White)
let start = "d5" let start = "d5"
let legalMoves = ["e4", "f3", "g2", "h1", "c6", "b7", "a8", "e6"] let legalMoves = ["e4", "f3", "g2", "h1", "c6", "b7", "a8", "e6"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1403,16 +989,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveBishopBlack() = method testCheckedMoveBishopBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/4P3/3b4/2p5/8/8/8 b - c6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WEnPassant, 0, BPawn, 0, 0,
0, 0, 0, 0, BBishop, 0, 0, 0,
0, 0, 0, WPawn, 0, BEnPassant, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.Black)
let start = "d5" let start = "d5"
let legalMoves = ["e4", "f3", "g2", "h1", "c6", "b7", "a8", "e6"] let legalMoves = ["e4", "f3", "g2", "h1", "c6", "b7", "a8", "e6"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1430,16 +1007,7 @@ testSuite ChessTest of TestSuite:
## Tests for Knight moves ## Tests for Knight moves
method testCheckedMoveKnightWhite() = method testCheckedMoveKnightWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/5p2/3N4/8/2P5/8/8 w - f4 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WEnPassant, 0, WPawn, 0, 0,
0, 0, BEnPassant, 0, 0, 0, 0, 0,
0, 0, 0, 0, WKnight, 0, 0, 0,
0, 0, BPawn, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.White)
let start = "d5" let start = "d5"
let legalMoves = ["f6", "f4", "e3", "b4", "b6", "c7", "e7"] let legalMoves = ["f6", "f4", "e3", "b4", "b6", "c7", "e7"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1456,16 +1024,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveKnightBlack() = method testCheckedMoveKnightBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/5P2/3n4/8/2p5/8/8 b - f4 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, BEnPassant, 0, BPawn, 0, 0,
0, 0, WEnPassant, 0, 0, 0, 0, 0,
0, 0, 0, 0, BKnight, 0, 0, 0,
0, 0, WPawn, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.Black)
let start = "d5" let start = "d5"
let legalMoves = ["f6", "f4", "e3", "b4", "b6", "c7", "e7"] let legalMoves = ["f6", "f4", "e3", "b4", "b6", "c7", "e7"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1483,16 +1042,7 @@ testSuite ChessTest of TestSuite:
## Tests for Rook moves ## Tests for Rook moves
method testCheckedMoveRookWhite() = method testCheckedMoveRookWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/8/3Rp3/3P4/8/8/8 w - c6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, WPawn, 0, 0, 0,
0, 0, 0, BPawn, WRook, WEnPassant, 0, 0,
0, 0, 0, 0, BEnPassant, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.White)
let start = "d5" let start = "d5"
let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5"] let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1509,16 +1059,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveRookBlack() = method testCheckedMoveRookBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/8/3rP3/3p4/8/8/8 b - c6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, BPawn, 0, 0, 0,
0, 0, 0, WPawn, BRook, BEnPassant, 0, 0,
0, 0, 0, 0, WEnPassant, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.Black)
let start = "d5" let start = "d5"
let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5"] let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5"]
let str = "abcdefgh" let str = "abcdefgh"
@ -1536,16 +1077,7 @@ testSuite ChessTest of TestSuite:
## Tests for Queen moves ## Tests for Queen moves
method testCheckedMoveQueenWhite() = method testCheckedMoveQueenWhite() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/8/3Qp3/3P4/8/8/8 w - c6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, WPawn, 0, 0, 0,
0, 0, 0, BPawn, WQueen, WEnPassant, 0, 0,
0, 0, 0, 0, BEnPassant, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.White)
let start = "d5" let start = "d5"
let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5", "h1", "g2", let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5", "h1", "g2",
"f3", "e4", "c4", "b3", "a2", "g8", "f7", "e6", "c6", "b7", "a8"] "f3", "e4", "c4", "b3", "a2", "g8", "f7", "e6", "c6", "b7", "a8"]
@ -1563,16 +1095,7 @@ testSuite ChessTest of TestSuite:
method testCheckedMoveQueenBlack() = method testCheckedMoveQueenBlack() =
var test: bool var test: bool
let pos = initChess([ let pos = initChess("8/8/8/3qP3/3p4/8/8/8 b - c6 0 0")
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, BPawn, 0, 0, 0,
0, 0, 0, WPawn, BQueen, BEnPassant, 0, 0,
0, 0, 0, 0, WEnPassant, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
], Color.Black)
let start = "d5" let start = "d5"
let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5", "h1", "g2", let legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5", "h1", "g2",
"f3", "e4", "c4", "b3", "a2", "g8", "f7", "e6", "c6", "b7", "a8"] "f3", "e4", "c4", "b3", "a2", "g8", "f7", "e6", "c6", "b7", "a8"]

@ -184,7 +184,7 @@ proc bestMove*(chess: Chess, depth: int): Move =
var tmpChess = chess var tmpChess = chess
tmpChess.checkedMove(move) tmpChess.checkedMove(move)
var tmpEval = -tmpChess.negaMax(depth, LoVal, HiVal) var tmpEval = -tmpChess.negaMax(depth, LoVal, HiVal)
echo("move:", moveToNotation(move, tmpChess.board), "; eval:", tmpEval) #echo("move:", moveToNotation(move, tmpChess.board), "; eval:", tmpEval)
if tmpEval > bestEval: if tmpEval > bestEval:
bestEval = tmpEval bestEval = tmpEval
bestMove = move bestMove = move

Loading…
Cancel
Save