From 97406d4da38aa39cb107aa0b2e1214376d075879 Mon Sep 17 00:00:00 2001 From: TiynGER Date: Mon, 14 Dec 2020 17:52:49 +0100 Subject: [PATCH] tests: added unittests Due to the official nim documentation the snake_case was now swapped for camelCase. Tests for nearly any move were added. A new procedure can initialize a game with a given 8x8 board with pieces and determines if they have been moved already. --- chess.nim | 414 ++++++++++++--------- game.nim | 47 +-- test.nim | 1067 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1318 insertions(+), 210 deletions(-) create mode 100644 test.nim diff --git a/chess.nim b/chess.nim index ed3440d..1f71796 100644 --- a/chess.nim +++ b/chess.nim @@ -12,7 +12,7 @@ type Game* = object pieces*: Pieces moved: Moved - to_move*: Color + toMove*: Color ## Move as object Move* = object start: int @@ -92,7 +92,28 @@ var FileChar = { "h": 0 }.newTable -proc init_board(): Pieces = +proc getField*(pieces: Pieces, field: int): int = + return pieces[field] + +proc setField(pieces: var Pieces, field: int, val: int): bool {.discardable.} = + if (val in PieceChar): + try: + pieces[field] = val + return true + except Exception: + return false + +proc getField*(moved: Moved, field: int): bool = + return moved[field] + +proc setField(moved: var Moved, field: int, val: bool): bool {.discardable.} = + try: + moved[field] = val + return true + except Exception: + return false + +proc initBoard(): Pieces = ## Create and return a board with pieces in starting position. let board = [ Block, Block, Block, Block, Block, Block, Block, Block, Block, Block, @@ -109,56 +130,73 @@ proc init_board(): Pieces = Block, Block, Block, Block, Block, Block, Block, Block, Block, Block] return board -proc get_move*(start: int, dest: int, prom: int, color: Color): Move = - ## Get a move object from `start` to `dest` with an eventual promition to `prom` - var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color) - if (KnightID > prom or QueenID < prom): - move.prom = QueenID - return move - -proc get_move*(start: int, dest: int, color: Color): Move = - ## Get a move object from `start` to `dest` with automatic promition to `queen` - var move = Move(start: start, dest: dest, prom: QueenID * ord(color), color: color) - return move +proc initBoard(pieces: array[0..63, int]): Pieces = + ## Create and return a board with pieces in position of choice + let board = [ + Block, Block, Block, Block, Block, Block, Block, Block, Block, Block, + Block, Block, Block, Block, Block, Block, Block, Block, Block, Block, + Block, pieces[0], pieces[1], pieces[2], pieces[3], pieces[4], pieces[5], + pieces[6], pieces[7], Block, + Block, pieces[8], pieces[9], pieces[10], pieces[11], pieces[12], pieces[13], + pieces[14], pieces[15], Block, + Block, pieces[16], pieces[17], pieces[18], pieces[19], pieces[20], pieces[ + 21], pieces[22], pieces[23], Block, + Block, pieces[24], pieces[25], pieces[26], pieces[27], pieces[28], pieces[ + 29], pieces[30], pieces[31], Block, + Block, pieces[32], pieces[33], pieces[34], pieces[35], pieces[36], pieces[ + 37], pieces[38], pieces[39], Block, + Block, pieces[40], pieces[41], pieces[42], pieces[43], pieces[44], pieces[ + 45], pieces[46], pieces[47], Block, + Block, pieces[48], pieces[49], pieces[50], pieces[51], pieces[52], pieces[ + 53], pieces[54], pieces[55], Block, + Block, pieces[56], pieces[57], pieces[58], pieces[59], pieces[60], pieces[ + 61], pieces[62], pieces[63], Block, + Block, Block, Block, Block, Block, Block, Block, Block, Block, Block, + Block, Block, Block, Block, Block, Block, Block, Block, Block, Block] + return board -proc init_moved(): Moved = +proc initMoved(): Moved = ## Create and return a board of pieces moved. var moved: Moved return moved -proc init_game*(): Game = +proc initGame*(): Game = ## Create and return a Game object. - let game = Game(pieces: init_board(), moved: init_moved(), + let game = Game(pieces: initBoard(), moved: initMoved(), to_move: Color.White) return game -proc get_field(pieces: Pieces, field: int): int = - return pieces[field] - -proc set_field(pieces: var Pieces, field: int, val: int): bool {.discardable.} = - if (val in PieceChar): - try: - pieces[field] = val - return true - except Exception: - return false +proc initGame*(pieces: array[0..63, int], color: Color): Game = + ## Create ad return a Game object based on a position of choice. + let pieces = initBoard(pieces) + let compare = initBoard() + var moved = initMoved() + var same_piece: bool + for ind in pieces.low..pieces.high: + same_piece = (pieces[ind] != compare[ind]) + moved.setField(ind, same_piece) + let game = Game(pieces: pieces, moved: moved, + to_move: color) + return game -proc get_field(moved: Moved, field: int): bool = - return moved[field] +proc getMove*(start: int, dest: int, prom: int, color: Color): Move = + ## Get a move object from `start` to `dest` with an eventual promition to `prom` + var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color) + if (KnightID > prom or QueenID < prom): + move.prom = QueenID + return move -proc set_field(moved: var Moved, field: int, val: bool): bool {.discardable.} = - try: - moved[field] = val - return true - except Exception: - return false +proc getMove*(start: int, dest: int, color: Color): Move = + ## Get a move object from `start` to `dest` with automatic promition to `queen` + var move = Move(start: start, dest: dest, prom: QueenID * ord(color), color: color) + return move -proc echo_board*(game: Game, color: Color) = +proc echoBoard*(game: Game, color: Color) = ## Prints out the given `board` with its pieces as characters and line indices from perspecive of `color`. var line_str = "" if (color == Color.Black): for i in countup(0, len(game.pieces)-1): - if (game.pieces.get_field(i) == 999): + if (game.pieces.getField(i) == 999): continue line_str &= PieceChar[game.pieces[i]] & " " if ((i+2) %% 10 == 0): @@ -167,7 +205,7 @@ proc echo_board*(game: Game, color: Color) = echo "h g f e d c b a" else: for i in countdown(len(game.pieces)-1, 0): - if (game.pieces.get_field(i) == 999): + if (game.pieces.getField(i) == 999): continue line_str &= PieceChar[game.pieces[i]] & " " if ((i-1) %% 10 == 0): @@ -175,21 +213,21 @@ proc echo_board*(game: Game, color: Color) = echo line_str echo "a b c d e f g h" -proc field_to_ind*(file: string, line: int): int = +proc fieldToInd*(file: string, line: int): int = ## Calculate board index from `file` and `line` of a chess board. try: return 1+(line+1)*10+FileChar[file] except IndexDefect, ValueError: return -1 -proc field_to_ind*(field: string): int = +proc fieldToInd*(field: string): int = ## Calculate board index from `field` of a chess board. try: - return field_to_ind($field[0], parseInt($field[1])) + return fieldToInd($field[0], parseInt($field[1])) except IndexDefect, ValueError: return -1 -proc ind_to_field*(ind: int): string = +proc indToField*(ind: int): string = ## Calculate field name from board index `ind`. let line = (int)ind/10-1 let file_ind = (ind)%%10-1 @@ -197,7 +235,32 @@ proc ind_to_field*(ind: int): string = if FileChar[file] == file_ind: return $file & $line -proc gen_bishop_dests(game: Game, field: int, color: Color): seq[int] = +proc notationToMove*(notation: string, color: Color): Move = + ## Convert simplified algebraic chess `notation` to a move object, color of player is `color`. + try: + var move: Move + var start = fieldToInd(notation[0..1]) + var dest = fieldToInd(notation[2..3]) + move = getMove(start, dest, color) + if (len(notation) > 4): + var promStr = $notation[4] + var prom: int + case promStr: + of "Q": + prom = QueenID * ord(color) + of "R": + prom = RookID * ord(color) + of "B": + prom = BishopID * ord(color) + of "N": + prom = KnightID * ord(color) + move = getMove(start, dest, prom, color) + return move + except IndexError: + var move: Move + return move + +proc genBishopDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible destinations for a bishop with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -206,18 +269,19 @@ proc gen_bishop_dests(game: Game, field: int, color: Color): seq[int] = var target: int for move in Bishop_Moves: dest = field+move - target = game.pieces.get_field(dest) - while (target != 999 and (ord(color) * target <= 0) or target == EnPassantID): + target = game.pieces.getField(dest) + while (target != 999 and (ord(color) * target <= 0) or target == + EnPassantID or target == -EnPassantID): res.add(dest) if (ord(color) * target < 0 and ord(color) * target > -EnPassantID): break dest = dest+move - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) return res except IndexDefect: return @[] -proc gen_rook_dests(game: Game, field: int, color: Color): seq[int] = +proc genRookDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible destinations for a rook with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -226,18 +290,19 @@ proc gen_rook_dests(game: Game, field: int, color: Color): seq[int] = var target: int for move in Rook_Moves: dest = field+move - target = game.pieces.get_field(dest) - while (target != 999 and (ord(color) * target <= 0) or target == EnPassantID): + target = game.pieces.getField(dest) + while (target != 999 and (ord(color) * target <= 0) or target == + EnPassantID or target == -EnPassantID): res.add(dest) if (ord(color) * target < 0 and ord(color) * target > -EnPassantID): break dest = dest+move - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) return res except IndexDefect: return @[] -proc gen_queen_dests(game: Game, field: int, color: Color): seq[int] = +proc genQueenDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible destinations for a queen with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -246,18 +311,19 @@ proc gen_queen_dests(game: Game, field: int, color: Color): seq[int] = var target: int for move in Queen_Moves: dest = field+move - target = game.pieces.get_field(dest) - while (target != 999 and (ord(color) * target <= 0) or target == EnPassantID): + target = game.pieces.getField(dest) + while (target != 999 and (ord(color) * target <= 0) or target == + EnPassantID or target == -EnPassantID): res.add(dest) if (ord(color) * target < 0 and ord(color) * target > -EnPassantID): break dest = dest+move - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) return res except IndexDefect: return @[] -proc gen_king_castle_dest(game: Game, field: int, color: Color): seq[int] = +proc genKingCastleDest(game: Game, field: int, color: Color): seq[int] = ## Generate possible castle destinations for a king with specific `color` located at index `field` of `game` ## Returns a sequence of possible indices to move to. try: @@ -268,9 +334,9 @@ proc gen_king_castle_dest(game: Game, field: int, color: Color): seq[int] = var half_target: int for castle in King_Moves_White_Castle: dest = field + castle - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) half_dest = field + (int)castle/2 - half_target = game.pieces.get_field(half_dest) + half_target = game.pieces.getField(half_dest) if (target == 999 or (target != 0)): continue if (half_target == 999 or (half_target != 0)): @@ -281,7 +347,7 @@ proc gen_king_castle_dest(game: Game, field: int, color: Color): seq[int] = return @[] -proc gen_king_dests(game: Game, field: int, color: Color): seq[int] = +proc genKingDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible destinations for a king with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -290,16 +356,16 @@ proc gen_king_dests(game: Game, field: int, color: Color): seq[int] = var target: int for move in King_Moves: dest = field + move - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)): continue res.add(dest) - res.add(game.gen_king_castle_dest(field, color)) + res.add(game.genKingCastleDest(field, color)) return res except IndexDefect: return @[] -proc gen_knight_dests(game: Game, field: int, color: Color): seq[int] = +proc genKnightDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible destinations for a knight with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -308,7 +374,7 @@ proc gen_knight_dests(game: Game, field: int, color: Color): seq[int] = var target: int for move in Knight_Moves: dest = field + move - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)): continue res.add(dest) @@ -316,7 +382,7 @@ proc gen_knight_dests(game: Game, field: int, color: Color): seq[int] = except IndexDefect: return @[] -proc gen_pawn_attack_dests(game: Game, field: int, color: Color): seq[int] = +proc genPawnAttackDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible attack destinations for a pawn with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -324,8 +390,8 @@ proc gen_pawn_attack_dests(game: Game, field: int, color: Color): seq[int] = var dest: int var target: int for attacks in Pawn_Moves_White_Attack: - dest = field + attacks * ord(color) - target = game.pieces.get_field(dest) + dest = field + (attacks * ord(color)) + target = game.pieces.getField(dest) if (target == 999 or ord(color) * target >= 0): continue res.add(dest) @@ -333,7 +399,7 @@ proc gen_pawn_attack_dests(game: Game, field: int, color: Color): seq[int] = except IndexDefect: return @[] -proc gen_pawn_double_dests(game: Game, field: int, color: Color): seq[int] = +proc genPawnDoubleDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible double destinations for a pawn with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -342,16 +408,16 @@ proc gen_pawn_double_dests(game: Game, field: int, color: Color): seq[int] = var target: int for doubles in Pawn_Moves_White_Double: dest = field + doubles * ord(color) - target = game.pieces.get_field(dest) - if (game.moved.get_field(field) or (target != 0) or ( - game.pieces.get_field(dest+(S*ord(color))) != 0)): + target = game.pieces.getField(dest) + if (game.moved.getField(field) or (target != 0) or ( + game.pieces.getField(dest+(S*ord(color))) != 0)): continue res.add(dest) return res except IndexDefect: return @[] -proc gen_pawn_dests(game: Game, field: int, color: Color): seq[int] = +proc genPawnDests(game: Game, field: int, color: Color): seq[int] = ## Generate possible destinations for a pawn with specific `color` located at index `field` of `game`. ## Returns a sequence of possible indices to move to. try: @@ -360,189 +426,189 @@ proc gen_pawn_dests(game: Game, field: int, color: Color): seq[int] = var target: int for move in Pawn_Moves_White: dest = field + move * ord(color) - target = game.pieces.get_field(dest) + target = game.pieces.getField(dest) if (target != 0 and target != ord(color) * EnPassantID): continue res.add(dest) - res.add(game.gen_pawn_attack_dests(field, color)) - res.add(game.gen_pawn_double_dests(field, color)) + res.add(game.genPawnAttackDests(field, color)) + res.add(game.genPawnDoubleDests(field, color)) return res except IndexDefect: return @[] -proc piece_on(game: Game, color: Color, sequence: seq[int], +proc pieceOn(game: Game, color: Color, sequence: seq[int], pieceID: int): bool = ## Check if a piece with `pieceID` of a given `color` is in a field described in a `sequence` in a `game`. for check in sequence: - if game.pieces.get_field(check) == ord(color) * -1 * pieceID: + if game.pieces.getField(check) == ord(color) * -1 * pieceID: return true return false -proc is_attacked(game: Game, position: int, color: Color): bool = +proc isAttacked(game: Game, position: int, color: Color): bool = ## Check if a field is attacked by the opposite of `color` in a `game`. var attacked = false - attacked = attacked or game.piece_on(color, game.gen_pawn_attack_dests( + attacked = attacked or game.pieceOn(color, game.genPawnAttackDests( position, color), PawnID) - attacked = attacked or game.piece_on(color, game.gen_queen_dests(position, + attacked = attacked or game.pieceOn(color, game.genQueenDests(position, color), QueenID) - attacked = attacked or game.piece_on(color, game.gen_king_dests(position, + attacked = attacked or game.pieceOn(color, game.genKingDests(position, color), KingID) - attacked = attacked or game.piece_on(color, game.gen_rook_dests(position, + attacked = attacked or game.pieceOn(color, game.genRookDests(position, color), RookID) - attacked = attacked or game.piece_on(color, game.gen_bishop_dests(position, + attacked = attacked or game.pieceOn(color, game.genBishopDests(position, color), BishopID) - attacked = attacked or game.piece_on(color, game.gen_knight_dests(position, + attacked = attacked or game.pieceOn(color, game.genKnightDests(position, color), KnightID) return attacked -proc is_in_check*(game: Game, color: Color): bool = +proc isInCheck*(game: Game, color: Color): bool = ## Check if the King of a given `color` is in check in a `game`. var king_pos: int for i in countup(0, game.pieces.high): - if game.pieces.get_field(i) == ord(color) * KingID: + if game.pieces.getField(i) == ord(color) * KingID: king_pos = i - return game.is_attacked(king_pos, color) + return game.isAttacked(king_pos, color) -proc unchecked_move(game: var Game, start: int, dest: int): bool {.discardable.} = +proc uncheckedMove(game: var Game, start: int, dest: int): bool {.discardable.} = ## Moves a piece if possible from `start` position to `dest` position. ## Doesnt check boundaries, checks, movement. ## returns true if the piece moved, else false try: - let piece = game.pieces.get_field(start) - if game.pieces.set_field(start, 0): - if game.pieces.set_field(dest, piece): - game.moved.set_field(start, true) - game.moved.set_field(dest, true) + let piece = game.pieces.getField(start) + if game.pieces.setField(start, 0): + if game.pieces.setField(dest, piece): + game.moved.setField(start, true) + game.moved.setField(dest, true) return true else: - game.pieces.set_field(start, piece) + game.pieces.setField(start, piece) except IndexDefect, ValueError: return false -proc move_leads_to_check(game: Game, start: int, dest: int, +proc moveLeadsToCheck(game: Game, start: int, dest: int, color: Color): bool = ## Checks in a `game` if a move from `start` to `dest` puts the `color` king in check. var check = game - check.unchecked_move(start, dest) - return check.is_in_check(color) + check.uncheckedMove(start, dest) + return check.isInCheck(color) -proc remove_en_passant(pieces: var Pieces, color: Color): void = +proc removeEnPassant(pieces: var Pieces, color: Color): void = ## Removes every en passant of given `color` from the `game`. for field in pieces.low..pieces.high: - if pieces.get_field(field) == ord(color) * EnPassantID: - pieces.set_field(field, 0) + if pieces.getField(field) == ord(color) * EnPassantID: + pieces.setField(field, 0) -proc gen_legal_knight_moves(game: Game, field: int, color: Color): seq[Move] = +proc genLegalKnightMoves(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal knight moves starting from `field` in a `game` for a `color`. - if game.pieces.get_field(field) != KnightID * ord(color): + if game.pieces.getField(field) != KnightID * ord(color): return @[] var res = newSeq[Move]() - var moves = game.gen_knight_dests(field, color) + var moves = game.genKnightDests(field, color) for dest in moves: - if not game.move_leads_to_check(field, dest, color): - res.add(get_move(field, dest, color)) + if not game.moveLeadsToCheck(field, dest, color): + res.add(getMove(field, dest, color)) return res -proc gen_legal_bishop_moves(game: Game, field: int, color: Color): seq[Move] = +proc genLegalBishopMoves(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal bishop moves starting from `field` in a `game` for a `color`. - if game.pieces.get_field(field) != BishopID * ord(color): + if game.pieces.getField(field) != BishopID * ord(color): return @[] var res = newSeq[Move]() - var moves = game.gen_bishop_dests(field, color) + var moves = game.genBishopDests(field, color) for dest in moves: - if not game.move_leads_to_check(field, dest, color): - res.add(get_move(field, dest, color)) + if not game.moveLeadsToCheck(field, dest, color): + res.add(getMove(field, dest, color)) return res -proc gen_legal_rook_moves(game: Game, field: int, color: Color): seq[Move] = +proc genLegalRookMoves(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal rook moves starting from `field` in a `game` for a `color`. - if game.pieces.get_field(field) != RookID * ord(color): + if game.pieces.getField(field) != RookID * ord(color): return @[] var res = newSeq[Move]() - var moves = game.gen_rook_dests(field, color) + var moves = game.genRookDests(field, color) for dest in moves: - if not game.move_leads_to_check(field, dest, color): - res.add(get_move(field, dest, color)) + if not game.moveLeadsToCheck(field, dest, color): + res.add(getMove(field, dest, color)) return res -proc gen_legal_queen_moves(game: Game, field: int, color: Color): seq[Move] = +proc genLegalQueenMoves(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal queen moves starting from `field` in a `game` for a `color`. - if game.pieces.get_field(field) != QueenID * ord(color): + if game.pieces.getField(field) != QueenID * ord(color): return @[] var res = newSeq[Move]() - var moves = game.gen_queen_dests(field, color) + var moves = game.genQueenDests(field, color) for dest in moves: - if not game.move_leads_to_check(field, dest, color): - res.add(get_move(field, dest, color)) + if not game.moveLeadsToCheck(field, dest, color): + res.add(getMove(field, dest, color)) return res -proc gen_legal_king_moves(game: Game, field: int, color: Color): seq[Move] = +proc genLegalKingMoves(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal king moves starting from `field` in a `game` for a `color`. - if game.pieces.get_field(field) != KingID * ord(color): + if game.pieces.getField(field) != KingID * ord(color): return @[] var res = newSeq[Move]() - var moves = game.gen_king_dests(field, color) + var moves = game.genKingDests(field, color) for dest in moves: - if field - dest == W+W and game.is_attacked(dest+W, color): + if field - dest == W+W and game.isAttacked(dest+W, color): continue - if field - dest == E+E and game.is_attacked(dest+E, color): + if field - dest == E+E and game.isAttacked(dest+E, color): continue - if not game.move_leads_to_check(field, dest, color): - res.add(get_move(field, dest, color)) + if not game.moveLeadsToCheck(field, dest, color): + res.add(getMove(field, dest, color)) return res -proc gen_pawn_promotion(move: Move, color: Color): seq[Move] = +proc genPawnPromotion(move: Move, color: Color): seq[Move] = ## Generate all possible promotions of a `move` by `color`. var promotions = newSeq[Move]() let start = move.start let dest = move.dest if (90 < dest and dest < 99) or (20 < dest and dest < 29): for piece in KnightID..QueenID: - promotions.add(get_move(start, dest, piece, color)) + promotions.add(getMove(start, dest, piece, color)) return promotions -proc gen_legal_pawn_moves(game: Game, field: int, color: Color): seq[Move] = +proc genLegalPawnMoves(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal pawn moves starting from `field` in a `game` for a `color`. - if game.pieces.get_field(field) != PawnID * ord(color): + if game.pieces.getField(field) != PawnID * ord(color): return @[] var res = newSeq[Move]() - var moves = game.gen_pawn_dests(field, color) + var moves = game.genPawnDests(field, color) for dest in moves: - if not game.move_leads_to_check(field, dest, color): - var promotions = gen_pawn_promotion(get_move(field, dest, color), color) + if not game.moveLeadsToCheck(field, dest, color): + var promotions = genPawnPromotion(getMove(field, dest, color), color) if promotions != @[]: res.add(promotions) else: - res.add(get_move(field, dest, color)) + res.add(getMove(field, dest, color)) return res -proc gen_legal_moves*(game: Game, field: int, color: Color): seq[Move] = +proc genLegalMoves*(game: Game, field: int, color: Color): seq[Move] = ## Generates all legal moves starting from `field` in a `game` for a `color`. var legal_moves = newSeq[Move]() - var target = ord(color) * game.pieces.get_field(field) + var target = ord(color) * game.pieces.getField(field) if 0 < target and target < EnPassantID: legal_moves = case target: of PawnID: - game.gen_legal_pawn_moves(field, color) + game.genLegalPawnMoves(field, color) of KnightID: - game.gen_legal_knight_moves(field, color) + game.genLegalKnightMoves(field, color) of BishopID: - game.gen_legal_bishop_moves(field, color) + game.genLegalBishopMoves(field, color) of RookID: - game.gen_legal_rook_moves(field, color) + game.genLegalRookMoves(field, color) of QueenID: - game.gen_legal_queen_moves(field, color) + game.genLegalQueenMoves(field, color) of KingID: - game.gen_legal_king_moves(field, color) + game.genLegalKingMoves(field, color) else: @[] return legal_moves -proc gen_legal_moves*(game: Game, color: Color): seq[Move] = +proc genLegalMoves*(game: Game, color: Color): seq[Move] = ## Generates all legal moves in a `game` for a `color`. var legal_moves = newSeq[Move]() for field in game.pieces.low..game.pieces.high: - legal_moves.add(game.gen_legal_moves(field, color)) + legal_moves.add(game.genLegalMoves(field, color)) return legal_moves proc castling(game: var Game, kstart: int, dest_kingside: bool, @@ -551,7 +617,7 @@ proc castling(game: var Game, kstart: int, dest_kingside: bool, ## `dest_kingside` for kingside castling, else castling is queenside. ## This process checks for the legality of the move and performs the switch of `game.to_move` try: - if game.to_move != color: + if game.toMove != color: return false var kdest = kstart var rstart: int @@ -564,26 +630,26 @@ proc castling(game: var Game, kstart: int, dest_kingside: bool, rstart = kstart + (W+W+W+W) rdest = rstart + (E+E+E) kdest = kstart + (W+W) - if not game.moved.get_field(kstart) and not game.moved.get_field(rstart): + if not game.moved.getField(kstart) and not game.moved.getField(rstart): var check = false if (dest_kingside): - check = check or game.is_attacked(kstart, color) - check = check or game.is_attacked(kstart+(E), color) - check = check or game.is_attacked(kstart+(E+E), color) + check = check or game.isAttacked(kstart, color) + check = check or game.isAttacked(kstart+(E), color) + check = check or game.isAttacked(kstart+(E+E), color) else: - check = check or game.is_attacked(kstart, color) - check = check or game.is_attacked(kstart+(W), color) - check = check or game.is_attacked(kstart+(W+W), color) + check = check or game.isAttacked(kstart, color) + check = check or game.isAttacked(kstart+(W), color) + check = check or game.isAttacked(kstart+(W+W), color) if check: return false - game.unchecked_move(kstart, kdest) - game.unchecked_move(rstart, rdest) + game.uncheckedMove(kstart, kdest) + game.uncheckedMove(rstart, rdest) return true return false except IndexDefect, ValueError: return false -proc checked_move*(game: var Game, move: Move): bool {.discardable.} = +proc checkedMove*(game: var Game, move: Move): bool {.discardable.} = ## Tries to make a move in a given `game` with the piece of a given `color` from `start` to `dest`. ## This process checks for the legality of the move and performs the switch of `game.to_move` try: @@ -591,46 +657,46 @@ proc checked_move*(game: var Game, move: Move): bool {.discardable.} = let dest = move.dest let color = move.color let prom = move.prom - if game.to_move != color: + if game.toMove != color: return false var sequence = newSeq[Move]() - let piece = game.pieces.get_field(start) + let piece = game.pieces.getField(start) var create_en_passant = false var captured_en_passant = false var move: Move - move = get_move(start, dest, color) + move = getMove(start, dest, color) if (piece == PawnID * ord(color)): - create_en_passant = dest in game.gen_pawn_double_dests(start, color) - captured_en_passant = (game.pieces.get_field(dest) == -1 * ord(color) * EnPassantID) - sequence.add(game.gen_legal_moves(start, color)) + create_en_passant = dest in game.genPawnDoubleDests(start, color) + captured_en_passant = (game.pieces.getField(dest) == -1 * ord(color) * EnPassantID) + sequence.add(game.genLegalMoves(start, color)) if (move in sequence): - game.pieces.remove_en_passant(color) + game.pieces.removeEnPassant(color) if (piece == KingID * ord(color) and (start - dest == (W+W))): - game.castling(start, true, color) + return game.castling(start, true, color) elif (piece == KingID * ord(color) and (start - dest == (E+E))): - game.castling(start, false, color) + return game.castling(start, false, color) else: - game.unchecked_move(start, dest) - game.to_move = Color(ord(game.to_move)*(-1)) + game.uncheckedMove(start, dest) + game.toMove = Color(ord(game.toMove)*(-1)) if create_en_passant: - game.pieces.set_field(dest-(N*ord(color)), EnPassantID * ord(color)) + game.pieces.setField(dest-(N*ord(color)), EnPassantID * ord(color)) if captured_en_passant: - game.pieces.set_field(dest-(N*ord(color)), 0) + game.pieces.setField(dest-(N*ord(color)), 0) if ((90 < dest and dest < 99) or (20 < dest and dest < 29)) and - game.pieces.get_field(dest) == PawnID * ord(color): - game.pieces.set_field(dest, prom) + game.pieces.getField(dest) == PawnID * ord(color): + game.pieces.setField(dest, prom) return true except IndexDefect, ValueError: return false -proc has_no_moves(game: Game, color: Color): bool = +proc hasNoMoves(game: Game, color: Color): bool = ## Checks if a player of a given `color` has no legal moves in a `game`. - return (game.gen_legal_moves(color) == @[]) + return (game.genLegalMoves(color) == @[]) -proc is_checkmate*(game: Game, color: Color): bool = +proc isCheckmate*(game: Game, color: Color): bool = ## Checks if a player of a given `color` in a `game` is checkmate. - return game.has_no_moves(color) and game.is_in_check(color) + return game.hasNoMoves(color) and game.isInCheck(color) -proc is_stalemate*(game: Game, color: Color): bool = +proc isStalemate*(game: Game, color: Color): bool = ## Checks if a player of a given `color` in a `game` is stalemate. - return game.has_no_moves(color) and not game.is_in_check(color) + return game.hasNoMoves(color) and not game.isInCheck(color) diff --git a/game.nim b/game.nim index e60c4ee..5cce581 100644 --- a/game.nim +++ b/game.nim @@ -3,44 +3,19 @@ import rdstdin import ./chess -proc notation_to_move(notation: string, color: Color): Move = - ## Convert simplified algebraic chess `notation` to a move object, color of player is `color`. - try: - var move: Move - var start = field_to_ind(notation[0..1]) - var dest = field_to_ind(notation[2..3]) - move = get_move(start, dest, color) - if (len(notation) > 4): - var prom_str = $notation[4] - var prom: int - case prom_str: - of "Q": - prom = QueenID * ord(color) - of "R": - prom = RookID * ord(color) - of "B": - prom = BishopID * ord(color) - of "N": - prom = KnightID * ord(color) - move = get_move(start, dest, prom, color) - return move - except IndexError: - var move: Move - return move - -proc run_game(): void = - var game = init_game() - game.echo_board(game.to_move) - while not game.is_checkmate(game.to_move): +proc runGame(): void = + var game = initGame() + game.echoBoard(game.toMove) + while not game.isCheckmate(game.toMove): echo "Make a move" - echo game.to_move + echo game.toMove var move = readLine(stdin) - while not game.checked_move(notation_to_move(move, game.to_move)): + while not game.checkedMove(notationToMove(move, game.toMove)): move = readLine(stdin) - game.echo_board(game.to_move) - if game.is_checkmate(game.to_move): - echo $game.to_move & " was checkmated" - if game.is_stalemate(game.to_move): + game.echoBoard(game.toMove) + if game.isCheckmate(game.toMove): + echo $game.toMove & " was checkmated" + if game.isStalemate(game.toMove): echo "Stalemate" -run_game() +runGame() diff --git a/test.nim b/test.nim new file mode 100644 index 0000000..c26a070 --- /dev/null +++ b/test.nim @@ -0,0 +1,1067 @@ +import einheit +import algorithm + +import ./chess + +testSuite GameTest of TestSuite: + + var + game: Game + + method setup() = + self.game = initGame() + + ## Tests for isInCheck() + method testIsInCheckFalse() = + self.setup() + self.game.checkedMove(notationToMove("e2e4", Color.White)) + self.game.checkedMove(notationToMove("e7e5", Color.Black)) + self.game.checkedMove(notationToMove("d2d3", Color.White)) + self.game.checkedMove(notationToMove("d8h4", Color.Black)) + self.check(not self.game.isInCheck(Color.White)) + self.check(not self.game.isInCheck(Color.White)) + + method testIsInCheckTrueWhite() = + self.setup() + self.game.checkedMove(notationToMove("f2f4", Color.White)) + self.game.checkedMove(notationToMove("e7e5", Color.Black)) + self.game.checkedMove(notationToMove("e2e3", Color.White)) + self.game.checkedMove(notationToMove("d8h4", Color.Black)) + self.check(self.game.isInCheck(Color.White)) + + method testIsInCheckTrueBlack() = + self.setup() + self.game.checkedMove(notationToMove("e2e4", Color.White)) + self.game.checkedMove(notationToMove("f7f6", Color.Black)) + self.game.checkedMove(notationToMove("d2d4", Color.White)) + self.game.checkedMove(notationToMove("g7g5", Color.Black)) + self.game.checkedMove(notationToMove("d1h5", Color.White)) + self.check(self.game.isInCheck(Color.Black)) + + ## Tests for isCheckmate() + method testIsCheckmateFalseWhite() = + self.setup() + self.game.checkedMove(notationToMove("f2f4", Color.White)) + self.game.checkedMove(notationToMove("e7e5", Color.Black)) + self.game.checkedMove(notationToMove("e2e3", Color.White)) + self.game.checkedMove(notationToMove("d8h4", Color.Black)) + self.check(not self.game.isCheckmate(Color.White)) + + method testIsCheckmateFalseBlack() = + self.setup() + self.game.checkedMove(notationToMove("f2f4", Color.White)) + self.game.checkedMove(notationToMove("e7e5", Color.Black)) + self.game.checkedMove(notationToMove("e2e3", Color.White)) + self.game.checkedMove(notationToMove("d8h4", Color.Black)) + self.check(not self.game.isCheckmate(Color.Black)) + + method testIsCheckmateTrueWhite() = + self.setup() + self.game.checkedMove(notationToMove("f2f3", Color.White)) + self.game.checkedMove(notationToMove("e7e6", Color.Black)) + self.game.checkedMove(notationToMove("g2g4", Color.White)) + self.game.checkedMove(notationToMove("d8h4", Color.Black)) + self.check(self.game.isCheckmate(Color.White)) + + method testIsCheckmateTrueBlack() = + self.setup() + self.game.checkedMove(notationToMove("e2e4", Color.White)) + self.game.checkedMove(notationToMove("g7g5", Color.Black)) + self.game.checkedMove(notationToMove("d2d4", Color.White)) + self.game.checkedMove(notationToMove("f7f6", Color.Black)) + self.game.checkedMove(notationToMove("d1h5", Color.White)) + self.check(self.game.isCheckmate(Color.Black)) + + ## Tests for isStalemate() + method testIsStalemateFalse() = + self.setup() + self.game.checkedMove(notationToMove("f2f3", Color.White)) + self.game.checkedMove(notationToMove("e7e6", Color.Black)) + self.game.checkedMove(notationToMove("g2g4", Color.White)) + self.game.checkedMove(notationToMove("d8h4", Color.Black)) + self.check(not self.game.isStalemate(Color.White)) + self.check(not self.game.isStalemate(Color.Black)) + + method testIsStalemateTrueWhite() = + self.game = initGame([ + 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.game.isStalemate(Color.White)) + + method testIsStalemateTrueBlack() = + self.game = initGame([ + 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.game.isStalemate(Color.Black)) + + ## Tests for Pawn moves + method testCheckedMovePawnSingleTrue() = + self.setup() + var test: bool + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "6", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "3" & $file & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "6" & $file & "5", Color.Black)) + self.check(test) + + method testCheckedMovePawnSingleFalseIntoEnemyPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "4" & $file & "5", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "5" & $file & "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnSingleFalseIntoOwnPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "6", Color.Black)) + self.check(not test) + + method testCheckedMovePawnDoubleTrue() = + self.setup() + var test: bool + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "2" & $file & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "5", Color.Black)) + self.check(test) + + method testCheckedMovePawnDoubleFalseAlreadyMoved() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "6" & $file & "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnDoubleFalseThroughEnemyPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "6" & $file & "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnDoubleFalseThroughOwnPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "6" & $file & "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnDoubleFalseIntoEnemyPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "6" & $file & "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnDoubleFalseIntoOwnPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + for file in "abcdefgh": + test = self.game.checkedMove(notationToMove($file & "3" & $file & "5", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove($file & "6" & $file & "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnCaptureTrueWhite() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "5", Color.White)) + self.check(test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "5", Color.White)) + self.check(test) + + method testCheckedMovePawnCaptureTrueBlack() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "4", Color.Black)) + self.check(test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "4", Color.Black)) + self.check(test) + + method testCheckedMovePawnCaptureFalseWhite() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "5", Color.White)) + self.check(not test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "5", Color.White)) + self.check(not test) + + method testCheckedMovePawnCaptureFalseBlack() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "4", Color.Black)) + self.check(not test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "4", Color.Black)) + self.check(not test) + + method testCheckedMovePawnEnPassantTrueWhite() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "2" & $file & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "6", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $file & "5", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "7" & $str[ + ind+1] & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "6", Color.White)) + self.check(test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "5"))) + self.check(test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "2" & $file & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "6", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $file & "5", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "7" & $str[ + ind+1] & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "6", Color.White)) + self.check(test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "5"))) + self.check(test) + + method testCheckedMovePawnEnPassantTrueBlack() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "7" & $file & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $file & "4", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "2" & $str[ + ind+1] & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "3", Color.Black)) + self.check(test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "4"))) + self.check(test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "7" & $file & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $file & "4", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "2" & $str[ + ind+1] & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "3", Color.Black)) + self.check(test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "4"))) + self.check(test) + + method testCheckedMovePawnEnPassantFalseWhite() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "2" & $file & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "7" & $str[ + ind+1] & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $file & "5", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "6", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "6", Color.White)) + self.check(not test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "5"))) + self.check(not test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "2" & $file & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "7" & $str[ + ind+1] & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $file & "5", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "7" & $file & "6", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & + "6", Color.White)) + self.check(not test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "5"))) + self.check(not test) + + method testCheckedMovePawnEnPassantFalseBlack() = + var test: bool + let pos = initGame([ + 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 + str = "abcdefgh" + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "7" & $file & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "2" & $str[ + ind+1] & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $file & "4", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "3", Color.Black)) + self.check(not test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "4"))) + self.check(not test) + str.reverse() + for ind, file in str: + if ind < len(str)-1: + self.game = pos + test = self.game.checkedMove(notationToMove($file & "7" & $file & "5", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($str[ind+1] & "2" & $str[ + ind+1] & "4", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "5" & $file & "4", Color.Black)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "2" & $file & "3", Color.White)) + self.check(test) + test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & + "3", Color.Black)) + self.check(not test) + test = (0 == self.game.pieces.getField(fieldToInd($str[ind+1] & "4"))) + self.check(not test) + + ## Tests for King moves + method testCheckedMoveKingCastleTrueWhite() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(test) + self.game = pos + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(test) + + method testCheckedMoveKingCastleTrueBlack() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(test) + self.game = pos + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(test) + + method testCheckedMoveKingCastleFalseAlreadyMovedKing() = + var test: bool + let pos = initGame([ + WRook, 0, 0, 0, WKing, 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, 0, BKing, 0, 0, BRook + ], Color.White) + self.game.checkedMove(notationToMove("d1e1", Color.White)) + self.game.checkedMove(notationToMove("d8e8", Color.White)) + self.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseAlreadyMovedRook() = + var test: bool + let pos = initGame([ + 0, WRook, 0, WKing, 0, 0, WRook, 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, + 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.game = pos + self.game.checkedMove(notationToMove("b1a1", Color.White)) + self.game.checkedMove(notationToMove("b8a8", Color.Black)) + self.game.checkedMove(notationToMove("g1h1", Color.White)) + self.game.checkedMove(notationToMove("g8h8", Color.Black)) + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseThroughCheck() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseIntoCheck() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseThroughOwnPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseThroughEnemyPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseIntoOwnPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingCastleFalseIntoEnemyPiece() = + var test: bool + let pos = initGame([ + 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.game = pos + test = self.game.checkedMove(notationToMove("e1c1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8c8", Color.Black)) + self.check(not test) + self.game.checkedMove(notationToMove("e1g1", Color.White)) + self.check(not test) + test = self.game.checkedMove(notationToMove("e8g8", Color.Black)) + self.check(not test) + + method testCheckedMoveKingWhite() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["f4", "d4", "f5", "f6", "e6", "d6"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.White)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + method testCheckedMoveKingBlack() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["f4", "d4", "f5", "f6", "e4", "d6"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.Black)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + ## Tests for Bishop moves + method testCheckedMoveBishopWhite() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["e4", "f3", "g2", "h1", "c6", "b7", "a8", "e6"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.White)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + method testCheckedMoveBishopBlack() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["e4", "f3", "g2", "h1", "c6", "b7", "a8", "e6"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.Black)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + ## Tests for Knight moves + method testCheckedMoveKnightWhite() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["f6", "f4", "e3", "b4", "b6", "c7", "e7"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.White)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + method testCheckedMoveKnightBlack() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["f6", "f4", "e3", "b4", "b6", "c7", "e7"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.Black)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + ## Tests for Rook moves + method testCheckedMoveRookWhite() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.White)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + method testCheckedMoveRookBlack() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.Black)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + ## Tests for Queen moves + method testCheckedMoveQueenWhite() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5", "h1", "g2", + "f3", "e4", "c4", "b3", "a2", "g8", "f7", "e6", "c6", "b7", "a8"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.White)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + + method testCheckedMoveQueenBlack() = + var test: bool + let pos = initGame([ + 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 legalMoves = ["e5", "d6", "d7", "d8", "c5", "b5", "a5", "h1", "g2", + "f3", "e4", "c4", "b3", "a2", "g8", "f7", "e6", "c6", "b7", "a8"] + let str = "abcdefgh" + var move: string + for cha in str: + for num in 1..8: + self.game = pos + move = $cha & $num + test = self.game.checkedMove(notationToMove(start & move, Color.Black)) + if move in legalMoves: + self.check(test) + else: + self.check(not test) + +when isMainModule: + einheit.runTests()