def create(): b = [[ Rook("black"), Knight("black"), Bishop("black"), Queen("black"), King("black"), Bishop("black"), Knight("black"), Rook("black") ], [ Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black") ]] for i in range(2, 6): b.append([ BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece() ]) b.append([ Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white") ]) b.append([ Rook("white"), Knight("white"), Bishop("white"), Queen("white"), King("white"), Bishop("white"), Knight("white"), Rook("white") ]) return b
def promotioned(self, piece, l): position = None if piece.getcolor() == "White": info = self.table.getwhitepieces() else: info = self.table.getblackpieces() for i in range(0, len(info)): if info[i].getid() == piece.getid(): position = i break if l == "Bishop": k = info[position] = Bishop.Bishop(piece.getid(), piece.getcolor(), piece.getx(), piece.gety()) self.table.updatepiece(info[position], info[position].getx(), info[position].gety()) elif l == "Rook": k = info[position] = Rook.Rook(piece.getid(), piece.getcolor(), piece.getx(), piece.gety()) self.table.updatepiece(info[position], info[position].getx(), info[position].gety()) elif l == "Queen": k = info[position] = Queen.Queen(piece.getid(), piece.getcolor(), piece.getx(), piece.gety()) self.table.updatepiece(info[position], info[position].getx(), info[position].gety()) elif l == "Knight": k = info[position] = Knight.Knight(piece.getid(), piece.getcolor(), piece.getx(), piece.gety()) self.table.updatepiece(info[position], info[position].getx(), info[position].gety()) else: return None return k
def test_is_free_roadmap(board): roadmap = board.create_horizontal_roadmap(Position(x=5, y=3), Position(x=3, y=3)) assert board.is_free_roadmap(roadmap[:-1]) board = Board() board[3][4].put_piece(Knight(Color.BLACK, Position(x=4, y=3))) roadmap = board.create_horizontal_roadmap(Position(x=5, y=3), Position(x=3, y=3)) assert not board.is_free_roadmap(roadmap[:-1]) board = Board() board[3][3].put_piece(Knight(Color.BLACK, Position(x=3, y=3))) roadmap = board.create_horizontal_roadmap(Position(x=5, y=3), Position(x=3, y=3)) assert board.is_free_roadmap(roadmap[:-1])
def test_white_pawn_promotion(): __ = BlankPiece() p1 = Pawn("white") p2 = Pawn("white") # Enemy rook rr = Rook("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, rr, __, __], # 0 [__, __, p1, __, __, p2, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] # Left-most pawn assert_length(board[1][2].get_attack_set(board, [1, 2], []), 0) assert_contains(board[1][2].get_move_set(board, [1, 2], []), [ Move.pawn_promotion([1, 2], [0, 2], Queen("white")), Move.pawn_promotion([1, 2], [0, 2], Bishop("white")), Move.pawn_promotion([1, 2], [0, 2], Knight("white")), Move.pawn_promotion([1, 2], [0, 2], Rook("white")) ]) assert_length(board[1][5].get_attack_set(board, [1, 5], []), 0) assert_length(board[1][5].get_move_set(board, [1, 5], []), 0)
def __init__(self, initial_pieces=None): self.check = None self.__turn = 'white' self.__squares = {} self.__initial_pieces = initial_pieces or { 'a1': Rook('white'), 'b1': Knight('white'), 'c1': Bishop('white'), 'd1': Queen('white'), 'e1': King('white'), 'f1': Bishop('white'), 'g1': Knight('white'), 'h1': Rook('white'), 'a2': Pawn('white'), 'b2': Pawn('white'), 'c2': Pawn('white'), 'd2': Pawn('white'), 'e2': Pawn('white'), 'f2': Pawn('white'), 'g2': Pawn('white'), 'h2': Pawn('white'), 'a8': Rook('black'), 'b8': Knight('black'), 'c8': Bishop('black'), 'd8': Queen('black'), 'e8': King('black'), 'f8': Bishop('black'), 'g8': Knight('black'), 'h8': Rook('black'), 'a7': Pawn('black'), 'b7': Pawn('black'), 'c7': Pawn('black'), 'd7': Pawn('black'), 'e7': Pawn('black'), 'f7': Pawn('black'), 'g7': Pawn('black'), 'h7': Pawn('black'), } for _x in x: for _y in y: self.__squares[_x + _y] = None if _x + _y in self.__initial_pieces: self.__squares[_x + _y] = self.__initial_pieces[_x + _y]
def __init__(self): self.classic = ([ Rook.Rook(0, "White", 0, 0), Knight.Knight(1, "White", 1, 0), Bishop.Bishop(2, "White", 2, 0), Queen.Queen(3, "White", 3, 0), King.King(4, "White", 4, 0), Bishop.Bishop(5, "White", 5, 0), Knight.Knight(6, "White", 6, 0), Rook.Rook(7, "White", 7, 0), Pawn.Pawn(8, "White", 0, 1), Pawn.Pawn(9, "White", 1, 1), Pawn.Pawn(10, "White", 2, 1), Pawn.Pawn(11, "White", 3, 1), Pawn.Pawn(12, "White", 4, 1), Pawn.Pawn(13, "White", 5, 1), Pawn.Pawn(14, "White", 6, 1), Pawn.Pawn(15, "White", 7, 1) ], [ Pawn.Pawn(16, "Black", 0, 6), Pawn.Pawn(17, "Black", 1, 6), Pawn.Pawn(18, "Black", 2, 6), Pawn.Pawn(19, "Black", 3, 6), Pawn.Pawn(20, "Black", 4, 6), Pawn.Pawn(21, "Black", 5, 6), Pawn.Pawn(22, "Black", 6, 6), Pawn.Pawn(23, "Black", 7, 6), Rook.Rook(24, "Black", 0, 7), Knight.Knight(25, "Black", 1, 7), Bishop.Bishop(26, "Black", 2, 7), King.King(27, "Black", 4, 7), Queen.Queen(28, "Black", 3, 7), Bishop.Bishop(29, "Black", 5, 7), Knight.Knight(30, "Black", 6, 7), Rook.Rook(31, "Black", 7, 7) ]) self.table = Table.Table(self.classic[0], self.classic[1]) self.shift = "White" self.player1 = ("Player 1", "White") self.player2 = ("Player 2", "Black") self.winner = None self.finish = False
def test_pieces_can_capture_opponent_pieces(): board = Board(initial_pieces={ 'a8': King('black'), 'e5': Pawn('black'), 'f3': Knight('white') }) assert board.pieces_quantity() == 3 knight = board.get_piece('f3') board.move('f3', 'e5') assert board.get_piece('e5') is knight assert board.pieces_quantity() == 2
def test_serialization(): __ = BlankPiece() wp = Pawn("white") bp = Pawn("black") br = Rook("black") bh = Knight("black") bb = Bishop("black") bq = Queen("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [br, bh, bb, bq, bk, __, __, __], # 0 [bp, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [wp, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] serializable_gameboard = convert_to_string_gameboard(board) expected_gameboard = [ ['br', 'bh', 'bb', 'bq', 'bk', '_', '_', '_'], ['bp', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['wp', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'] ] assert_length(serializable_gameboard, 8) assert_length(serializable_gameboard[3], 8) if expected_gameboard != serializable_gameboard: raise AssertionError("Expected gameboard" + str(expected_gameboard) + " but was " + str(serializable_gameboard)) new_board = convert_from_string_gameboard(expected_gameboard) for i in range(0, len(board)): for j in range(0, len(board[0])): expected_piece = board[i][j] actual_piece = new_board[i][j] if expected_piece.type != actual_piece.type: raise AssertionError("Expected piece type at " + str(i) + ", " + str(j) + " was " + expected_piece.type + ", but got " + actual_piece.type) if expected_piece.col != actual_piece.col: raise AssertionError("Expected piece col at " + str(i) + ", " + str( j) + " was " + expected_piece.col + " but got " + actual_piece.col)
def test_knight(): piece = Knight(Color.BLACK, Position(x=3, y=3)) assert piece.can_move_to(Position(x=4, y=5)) assert piece.can_move_to(Position(x=2, y=5)) assert piece.can_move_to(Position(x=4, y=1)) assert piece.can_move_to(Position(x=2, y=1)) assert piece.can_move_to(Position(x=1, y=2)) assert piece.can_move_to(Position(x=1, y=4)) assert piece.can_move_to(Position(x=5, y=2)) assert piece.can_move_to(Position(x=5, y=4)) # Bishop assert not piece.can_move_to(Position(x=2, y=2)) # Rook assert not piece.can_move_to(Position(x=3, y=4)) assert not piece.can_move_to(Position(x=2, y=3))
def test_knight_move(self): knight = Knight('white') self.assertTrue(knight.can_move('d4', 'b5')) self.assertTrue(knight.can_move('d4', 'c6')) self.assertTrue(knight.can_move('d4', 'e6')) self.assertTrue(knight.can_move('d4', 'f5')) self.assertTrue(knight.can_move('d4', 'b3')) self.assertTrue(knight.can_move('d4', 'c2')) self.assertTrue(knight.can_move('d4', 'e2')) self.assertTrue(knight.can_move('d4', 'f3'))
def get_attackers( self, start_pos: Position, color: Color ) -> Generator[Position, None, None]: all_directions = list(Diagonal) + list(Direction) positions_by_dir = self.get_positions_in_direction(start_pos, *all_directions) positions_by_dir['Knight'] = filter(self.is_in_bounds, Knight.possible_positions(start_pos)) diagonals = [d.name for d in Diagonal] # TODO(yavor): cleanup from bug fix directions = [d.name for d in Direction] for direction, positions in positions_by_dir.items(): direction = getattr(direction, 'name', direction) # TODO(yavor): cleanup bug fix for position in positions: if not self.is_empty(position) and self.are_enemies(color, position): enemy = self[position.rank][position.file] enemy_type = enemy.figure_type if direction == 'Knight' and enemy_type is Type.KNIGHT: yield position if direction in diagonals and enemy_type in [Type.BISHOP, Type.QUEEN]: yield position elif direction in directions and enemy_type in [Type.ROOK, Type.QUEEN]: yield position elif enemy_type is Type.KING and start_pos.dist(position) == 1: yield position elif enemy_type is Type.PAWN: direction = Direction.DOWN if enemy.color == Color.WHITE else Direction.UP pawn_positions = { Position(rank=start_pos.rank + direction, file=start_pos.file + Direction.RIGHT), Position(rank=start_pos.rank + direction, file=start_pos.file + Direction.LEFT) } if position in pawn_positions: yield position elif not self.is_empty(position) and direction != 'Knight': # every other piece is blockable so we can stop going further break
def test_castling_movesets(): board = get_castling_board() assert_contains( board[7][4].get_attack_set(board, [7, 4]), create_list_of_moves(MoveType.NORMAL, [7, 4], [[7, 3], [7, 5]])) assert_contains( board[7][4].get_move_set(board, [7, 4]), create_list_of_moves(MoveType.CASTLING, [7, 4], [[7, 1], [7, 6]])) # Pretend that r1 has moved and show it is removed from the castling set board[7][0].has_never_moved = False assert_contains(board[7][4].get_move_set(board, [7, 4]), create_list_of_moves(MoveType.CASTLING, [7, 4], [[7, 6]])) # Add a Knight next to the r2 to show it removes it's option for castling board[7][6] = Knight("white") assert_length(board[7][4].get_move_set(board, [7, 4]), 0)
def test_knight_capture(self): board = Board(initial_pieces={ 'a8': King('black'), 'e5': Pawn('black'), 'f3': Knight('white') }) pieces = [ piece for piece in board.squares.values() if piece is not None ] self.assertEqual(3, len(pieces)) knight = board.squares['f3'] board.move('f3', 'e5') self.assertIs(knight, board.squares['e5']) pieces = [ piece for piece in board.squares.values() if piece is not None ] self.assertEqual(2, len(pieces))
def convert_from_string_gameboard(serializable_gameboard): """ Get gameboard from the serializable entity. Note: This information is only useful for display purposes. (information such as whether castling is still a valid move does not exist here). :param game_board: list of lists containing "wp" or "bk" to represent white pawn or black knight. :return: list of list containing instances of the Piece class """ gameboard = [] for row in serializable_gameboard: gameboard_row = [] for piece in row: piece_col = "" if piece == "_": gameboard_row.append(BlankPiece()) else: if piece[0] == "w": piece_col = "white" elif piece[0] == "b": piece_col = "black" if piece[1] == "r": gameboard_row.append(Rook(piece_col)) elif piece[1] == "h": gameboard_row.append(Knight(piece_col)) elif piece[1] == "b": gameboard_row.append(Bishop(piece_col)) elif piece[1] == "q": gameboard_row.append(Queen(piece_col)) elif piece[1] == "k": gameboard_row.append(King(piece_col)) elif piece[1] == "p": gameboard_row.append(Pawn(piece_col)) gameboard.append(gameboard_row) return gameboard
class TestKnight(unittest.TestCase): def setUp(self): self.coords = Coordinates('d', 4) self.knight = Knight(self.coords, 'white') def test_knight_initialization(self): self.assertEqual(self.knight.coordinates, self.coords) def test_knight_position(self): self.assertTupleEqual((3, 3), self.knight.position) def test_knight_resource_name(self): self.assertEqual('white_knight.png', self.knight.resource_name) def test_knight_cant_move_ahead(self): new_coord = Coordinates('d', 5) self.assertFalse(self.knight.can_move_to(new_coord)) def test_knight_cant_move_behind(self): new_coord = Coordinates('d', 3) self.assertFalse(self.knight.can_move_to(new_coord)) def test_knight_cant_move_to_the_left(self): new_coord = Coordinates('c', 4) self.assertFalse(self.knight.can_move_to(new_coord)) def test_knight_cant_move_to_the_right(self): new_coord = Coordinates('e', 4) self.assertFalse(self.knight.can_move_to(new_coord)) def test_knight_cant_move_diagonally(self): new_coord = Coordinates('e', 5) self.assertFalse(self.knight.can_move_to(new_coord)) def test_knight_can_move_on_l(self): new_coord = Coordinates('e', 7) self.assertTrue(self.knight.can_move_to(new_coord))
def build_board(self): board = [[None] * self.UPPER_BOUNDS for n in range(self.UPPER_BOUNDS)] board[0][1] = Knight() return board
def test_fail_knight_capture_ally(self): board = Board(initial_pieces={ 'e5': Pawn('white'), 'f3': Knight('white') }) self.assertRaises(ImpossibleMove, board.move, 'f3', 'e5')
def test_create_pieces(self): self.assertEqual(create_piece('k'), King()) self.assertEqual(create_piece('q'), Queen()) self.assertEqual(create_piece('b'), Bishop()) self.assertEqual(create_piece('r'), Rook()) self.assertEqual(create_piece('n'), Knight())
def test_insufficient_material_states(): __ = BlankPiece() wk = King("white") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, wk, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true("Just kings should impossible to checkmate", is_impossible_to_reach_checkmate(board)) bB = Bishop("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, wk, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, bB, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true("King vs King and bishop should be impossible to checkmate", is_impossible_to_reach_checkmate(board)) bH = Knight("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, wk, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, bH, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true("King vs King and Knight should be impossible checkmate", is_impossible_to_reach_checkmate(board)) wb = Bishop("white") bb = Bishop("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, bb], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true( "King and Bishop vs King and Bishop should be impossible if they are on the same color square", is_impossible_to_reach_checkmate(board)) # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, bb], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_false( "King and Bishop vs King and Bishop should be POSSIBLE to reach checkmate if they're on different " "colours", is_impossible_to_reach_checkmate(board)) # Does not matter the amount of Bishops as long as they are on the same color square # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, wb, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, wb, __, bb], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, wb] # 7 ] assert_true( "King and Bishop(s) vs King and Bishop(s) should be impossible if they are on the same color square", is_impossible_to_reach_checkmate(board)) # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, wb, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, wb, __, bb], # 5 [__, __, __, __, __, __, __, wb], # 6 [__, __, __, __, __, __, __, wb] # 7 ] assert_false( "King and Bishop(s) vs King and Bishop(s) should be POSSIBLE as not all on same color square", is_impossible_to_reach_checkmate(board))
def test_fail_knight_move(self): knight = Knight('white') self.assertFalse(knight.can_move('d4', 'd3')) self.assertFalse(knight.can_move('d4', 'b2')) self.assertFalse(knight.can_move('d4', 'd1')) self.assertFalse(knight.can_move('d4', 'h8'))
def setUp(self): self.coords = Coordinates('d', 4) self.knight = Knight(self.coords, 'white')
def test_knight_name(self): knight = Knight('white') self.assertEqual('Knight', knight.name)
def test_knight_is_black(self): knight = Knight('black') self.assertEqual('black', knight.color)
def test_knight_is_white(self): knight = Knight('white') self.assertEqual('white', knight.color)
def test_teleporting_pieces(player_col, opponent_col): _ = BlankPiece() k = King(player_col) k.has_never_moved = False h = Knight(player_col) # Enemy rook e = Rook(opponent_col) # Friendly Pawn f = Pawn(player_col) f.has_never_moved = False # 0 1 2 3 4 5 6 7 board = [ [h, _, _, _, _, _, _, _], # 0 [_, _, _, _, _, _, _, _], # 1 [_, _, _, f, _, e, _, _], # 2 [_, _, _, _, _, _, _, _], # 3 [_, _, _, _, h, _, _, _], # 4 [_, _, _, _, _, _, _, _], # 5 [_, h, _, _, _, _, _, _], # 6 [_, _, _, _, _, _, _, k] # 7 ] # Top-left knight assert_length(board[0][0].get_move_set(board, [0, 0], []), 0) assert_contains( board[0][0].get_attack_set(board, [0, 0], []), create_list_of_moves(MoveType.NORMAL, [0, 0], [ [1, 2], [2, 1], ])) # Knight near bottom assert_length(board[6][1].get_move_set(board, [6, 1], []), 0) assert_contains( board[6][1].get_attack_set(board, [6, 1], []), create_list_of_moves(MoveType.NORMAL, [6, 1], [[4, 0], [4, 2], [5, 3], [7, 3]])) # Middle knight assert_length(board[4][4].get_move_set(board, [4, 4], []), 0) assert_contains( board[4][4].get_attack_set(board, [4, 4], []), create_list_of_moves( MoveType.NORMAL, [4, 4], [[2, 5], [6, 5], [6, 3], [3, 2], [5, 2], [3, 6], [5, 6]])) # Bottom-right king assert_length(board[7][7].get_move_set(board, [7, 7], []), 0) assert_contains( board[7][7].get_attack_set(board, [7, 7], []), create_list_of_moves( MoveType.NORMAL, [7, 7], [ # Up [6, 7], # Left [7, 6], # north-east [6, 6] ]))
import unittest from chess.pieces import Knight knight = Knight() class TestKnight(unittest.TestCase): def test_get_moves(self): self.assertEqual( knight.get_moves("F3"), sorted(["G5", "H4", "E5", "D4", "E1", "D2", "G1", "H2"])) if __name__ == "__main__": unittest.main()
def test_piece_cant_capture_an_ally(): board = Board(initial_pieces={'e5': Pawn('white'), 'f3': Knight('white')}) with pytest.raises(ImpossibleMove): board.move('f3', 'e5')