def test_remove_piece(self): board = Board() board.set_piece(Position(1, 2), Pieces.BLACK_BISHOP) assert board.get_piece(Position(1, 2)) == Pieces.BLACK_BISHOP board.remove_piece(Position(1, 2)) assert board.get_piece(Position(1, 2)) is None
def test_set_piece(self): board = Board() board.set_piece(Position(4, 4), Pieces.WHITE_ROOK) # TODO(amirov-m): What is the right order for actual and expected? assert board.get_piece(Position(4, 4)) == Pieces.WHITE_ROOK board.set_piece(Position(7, 7), Pieces.BLACK_PAWN) assert board.get_piece(Position(7, 7)) == Pieces.BLACK_PAWN
def test_get_positions_for_piece(self): board = Board() board.set_piece(Position(1, 0), Pieces.WHITE_KNIGHT) board.set_piece(Position(4, 0), Pieces.WHITE_KNIGHT) board.set_piece(Position(0, 7), Pieces.WHITE_KNIGHT) board.set_piece(Position(7, 7), Pieces.WHITE_KNIGHT) piece_positions = board.get_positions_for_piece(Pieces.WHITE_KNIGHT) assert set(piece_positions) == { Position(1, 0), Position(4, 0), Position(0, 7), Position(7, 7), }
def test_is_mate(self): """Test of is_check() method.""" assert not GameLogic.is_mate(self.game) # Create dummy board board = Board() board.set_piece(Position(0, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(1, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(0, 0), Piece(PieceType.KING, Colour.WHITE)) board.set_piece(Position(2, 0), Piece(PieceType.ROOK, Colour.BLACK)) history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] game = Game(board, Colour.WHITE, history_moves) assert GameLogic.is_mate(game)
class TestPositionsUnderThreat(unittest.TestCase): """Test of PositionsUnderThreat class. Create dummy board by means of setUp() method and check the correctness of pieces moves. """ def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 3), Pieces.WHITE_PAWN) self.board.set_piece(Position(3, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(5, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(0, 6), Pieces.WHITE_PAWN) self.board.set_piece(Position(7, 7), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 4), Pieces.WHITE_KING) self.board.set_piece(Position(6, 6), Pieces.WHITE_BISHOP) self.board.set_piece(Position(3, 7), Pieces.WHITE_ROOK) self.board.set_piece(Position(5, 5), Pieces.WHITE_QUEEN) self.board.set_piece(Position(7, 6), Pieces.WHITE_KNIGHT) self.board.set_piece(Position(4, 5), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 7), Pieces.BLACK_ROOK) def test_positions_under_king_threat(self): """Test of positions_under_king_threat() method.""" assert sorted( PositionsUnderThreat.positions_under_king_threat( Position(4, 4), Colour.WHITE, self.board)) == [ Position(3, 3), Position(3, 5), Position(4, 5), Position(5, 3) ] def test_positions_under_queen_threat(self): """Test of positions_under_queen_threat() method.""" assert sorted( PositionsUnderThreat.positions_under_queen_threat( Position(5, 5), Colour.WHITE, self.board)) == [ Position(4, 5), Position(4, 6), Position(5, 6), Position(5, 7), Position(6, 4), Position(6, 5), Position(7, 3), Position(7, 5), ] def test_positions_under_bishop_threat(self): """Test of positions_under_bishop_threat() method.""" assert sorted( PositionsUnderThreat.positions_under_bishop_threat( Position(6, 6), Colour.WHITE, self.board)) == [Position(5, 7), Position(7, 5)] def test_positions_under_knight_threat(self): """Test of positions_under_knight_threat() method.""" assert sorted( PositionsUnderThreat.positions_under_knight_threat( Position(7, 6), Colour.WHITE, self.board)) == [Position(5, 7), Position(6, 4)] def test_positions_under_rook_threat(self): """Test of positions_under_rook_threat() method.""" assert sorted( PositionsUnderThreat.positions_under_rook_threat( Position(3, 7), Colour.WHITE, self.board)) == [ Position(0, 7), Position(1, 7), Position(2, 7), Position(3, 5), Position(3, 6), Position(4, 7), Position(5, 7), ] def test_positions_under_pawn_threat(self): """Test of positions_under_pawn_threat() method.""" assert not PositionsUnderThreat.positions_under_pawn_threat( Position(4, 3), Colour.WHITE, self.board) assert sorted( PositionsUnderThreat.positions_under_pawn_threat( Position(3, 4), Colour.WHITE, self.board)) == [Position(2, 5), Position(4, 5)] assert sorted( PositionsUnderThreat.positions_under_pawn_threat( Position(5, 4), Colour.WHITE, self.board)) == [Position(4, 5), Position(6, 5)] assert sorted( PositionsUnderThreat.positions_under_pawn_threat( Position(0, 6), Colour.WHITE, self.board)) == [Position(1, 7)] assert not PositionsUnderThreat.positions_under_pawn_threat( Position(7, 7), Colour.WHITE, self.board) assert PositionsUnderThreat.positions_under_pawn_threat( Position(4, 5), Colour.BLACK, self.board) == [Position(3, 4), Position(5, 4)] def test_positions_under_threat(self): """Test of positions_under_threat() method.""" assert sorted( PositionsUnderThreat.positions_under_threat( Position(4, 4), Pieces.WHITE_KING, self.board)) == [ Position(3, 3), Position(3, 5), Position(4, 5), Position(5, 3) ] def test_all_positions_under_threat_for_side(self): """Test of all_positions_under_threat_for_side() method.""" assert sorted( PositionsUnderThreat.all_positions_under_threat_for_side( Colour.WHITE, self.board)) == [ Position(3, 4), Position(3, 7), Position(4, 7), Position(5, 4), Position(5, 5), Position(5, 6), Position(6, 7), Position(7, 7), ]
def test_set_piece_rewrite_piece(self): board = Board() board.set_piece(Position(1, 3), Pieces.WHITE_QUEEN) board.set_piece(Position(1, 3), Pieces.BLACK_KING) assert board.get_piece(Position(1, 3)) == Pieces.BLACK_KING
class TestGameLogic(unittest.TestCase): """Test of GameLogic class. Create dummy gsme by means of setUp() method and check the correctness of pieces moves. """ def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 0), Pieces.WHITE_KING) self.board.set_piece(Position(0, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(7, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(4, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 1), Pieces.WHITE_BISHOP) self.board.set_piece(Position(3, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(4, 3), Pieces.BLACK_ROOK) self.board.set_piece(Position(2, 2), Pieces.BLACK_PAWN) # Create game self.game = Game(self.board, Colour.WHITE, [Move(Position(3, 6), Position(3, 4))]) def test_is_check(self): """Test of is_check() method.""" assert not GameLogic.is_check(self.game.board, self.game.turn) def test_is_mate(self): """Test of is_check() method.""" assert not GameLogic.is_mate(self.game) # Create dummy board board = Board() board.set_piece(Position(0, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(1, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(0, 0), Piece(PieceType.KING, Colour.WHITE)) board.set_piece(Position(2, 0), Piece(PieceType.ROOK, Colour.BLACK)) history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] game = Game(board, Colour.WHITE, history_moves) assert GameLogic.is_mate(game) def test_make_move(self): """Test of make_move() method.""" # Check short castling game = GameLogic.make_move(Move(Position(4, 0), Position(6, 0)), self.game) assert game.turn != self.game.turn assert game.history_moves[-1] == Move(Position(4, 0), Position(6, 0)) assert game.board.get_piece(Position(4, 0)) is None assert game.board.get_piece(Position(5, 0)).type == PieceType.ROOK assert game.board.get_piece(Position(6, 0)).type == PieceType.KING assert game.board.get_piece(Position(7, 0)) is None # Check long castling game = GameLogic.make_move(Move(Position(4, 0), Position(2, 0)), self.game) assert game.turn != self.game.turn assert game.history_moves[-1] == Move(Position(4, 0), Position(2, 0)) assert game.board.get_piece(Position(4, 0)) is None assert game.board.get_piece(Position(3, 0)).type == PieceType.ROOK assert game.board.get_piece(Position(2, 0)).type == PieceType.KING assert game.board.get_piece(Position(1, 0)) is None assert game.board.get_piece(Position(0, 0)) is None # Check en_passant game = GameLogic.make_move(Move(Position(4, 4), Position(3, 5)), self.game) assert game.turn != self.game.turn assert game.history_moves[-1] == Move(Position(4, 4), Position(3, 5)) assert game.board.get_piece(Position(4, 4)) is None assert game.board.get_piece(Position(3, 5)).type == PieceType.PAWN assert game.board.get_piece(Position(3, 5)).colour == Colour.WHITE assert game.board.get_piece(Position(3, 4)) is None def test_is_move_possible(self): """Test of is_move_possible() method.""" assert not GameLogic.is_move_possible( self.game, Move(Position(4, 1), Position(3, 2))) assert not GameLogic.is_move_possible( self.game, Move(Position(4, 0), Position(3, 1))) assert not GameLogic.is_move_possible( self.game, Move(Position(4, 0), Position(4, -1))) assert not GameLogic.is_move_possible( self.game, Move(Position(2, 2), Position(2, 1))) assert GameLogic.is_move_possible(self.game, Move(Position(4, 0), Position(5, 1))) assert GameLogic.is_move_possible(self.game, Move(Position(4, 0), Position(2, 0))) assert GameLogic.is_move_possible(self.game, Move(Position(4, 0), Position(6, 0)))
class TestPieceMoves(unittest.TestCase): """Test of PieceMoves class. Create dummy board by means of setUp() method and check the correctness of pieces moves. """ def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 0), Pieces.WHITE_KING) self.board.set_piece(Position(0, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(0, 1), Pieces.WHITE_PAWN) self.board.set_piece(Position(7, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(7, 1), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(2, 7), Pieces.BLACK_ROOK) self.board.set_piece(Position(3, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 4), Pieces.BLACK_PAWN) # Create game history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] self.game = Game(self.board, Colour.WHITE, history_moves) def test_is_piece_touched(self): """Test of is_piece_touched() method.""" assert PieceMoves.is_piece_touched(Position(4, 4), self.game) assert PieceMoves.is_piece_touched(Position(3, 4), self.game) def test_en_passant_moves(self): """Test of en_passant_moves() method.""" assert sorted(PieceMoves.en_passant_moves(Position( 4, 4), self.game)) == [Move(Position(4, 4), Position(3, 5))] def test_castling_moves(self): """Test of castling_moves() method.""" assert sorted(PieceMoves.castling_moves(Position( 4, 0), self.game)) == [Move(Position(4, 0), Position(6, 0))] def test_pawn_moves(self): """Test of pawn_moves() method.""" assert sorted(PieceMoves.pawn_moves(Position(4, 4), self.game)) == [ Move(Position(4, 4), Position(3, 5)), Move(Position(4, 4), Position(4, 5)), ] def test_king_moves(self): """Test of king_moves() method.""" assert sorted(PieceMoves.king_moves(Position(4, 0), self.game)) == [ Move(Position(4, 0), Position(3, 0)), Move(Position(4, 0), Position(3, 1)), Move(Position(4, 0), Position(4, 1)), Move(Position(4, 0), Position(5, 0)), Move(Position(4, 0), Position(5, 1)), Move(Position(4, 0), Position(6, 0)), ] def test_all_moves(self): """Test of all_moves() method.""" assert sorted(PieceMoves.all_moves(self.game)) == [ Move(Position(0, 0), Position(1, 0)), Move(Position(0, 0), Position(2, 0)), Move(Position(0, 0), Position(3, 0)), Move(Position(0, 1), Position(0, 2)), Move(Position(0, 1), Position(0, 3)), *sorted(PieceMoves.king_moves(Position(4, 0), self.game)), Move(Position(4, 4), Position(3, 5)), Move(Position(4, 4), Position(4, 5)), Move(Position(7, 0), Position(5, 0)), Move(Position(7, 0), Position(6, 0)), Move(Position(7, 1), Position(7, 2)), Move(Position(7, 1), Position(7, 3)), ]