def test_move_wrong_turn(self): new_board = Board('x', 'o') with pytest.raises(IllegalMoveException) as e: new_board.move(0, 1, 'o') assert e.value.error == 'It is not your turn.'
def test_tie_game(self): new_board = Board('x', 'o') new_board.move(0, 0, 'x') new_board.move(0, 2, 'o') new_board.move(0, 1, 'x') new_board.move(1, 0, 'o') new_board.move(1, 1, 'x') new_board.move(2, 1, 'o') new_board.move(1, 2, 'x') new_board.move(2, 2, 'o') new_board.move(2, 0, 'x') is_complete, winner = new_board.is_complete() assert is_complete is True assert not new_board.turn assert winner == ''
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_move_invalid_coordinate(self): new_board = Board('x', 'o') with pytest.raises(IllegalMoveException) as e: new_board.move(0, 10, 'x') assert e.value.error == 'Invalid coordinates.'
def test_create_start_board(self): start_board = Board.create_start_board() assert start_board is not None
def main() -> int: core.init() parser = argparse.ArgumentParser() parser.add_argument( "-l", "--level", help="AI player Level. Default is 0 (Easy). Higher is harder)", default=cfg.LEVEL, type=int) parser.add_argument('-d', '--debug', help="Debug mode", action='store_true') parser.add_argument('-C', '--cache', help="Enable persistent memoize cache", action='store_true') options = parser.parse_args() cfg.LEVEL = options.level cfg.__DEBUG__ = options.debug cfg.CACHE_ENABLED = options.cache log('Quoridor AI game, (C) 2009 by Jose Rodriguez (a.k.a. Boriel)') log('This program is Free') log('Initializing system...') pygame.init() clock = pygame.time.Clock() pygame.display.set_mode((800, 600)) pygame.display.set_caption(cfg.GAME_TITLE) screen = pygame.display.get_surface() screen.fill(Color(255, 255, 255)) board = core.BOARD = Board(screen) board.draw() log('System initialized OK') if cfg.CACHE_ENABLED: if not os.path.exists(cfg.CACHE_DIR): log('Cache directory {} not found. Creating it...'.format( cfg.CACHE_DIR)) os.makedirs(cfg.CACHE_DIR, exist_ok=True) if not os.path.isdir(cfg.CACHE_DIR): log( 'Could not create cache directory {}. Caching disabled'.format( cfg.CACHE_DIR), LogLevel.ERROR) cfg.CACHE_ENABLED = False cont = True while cont: clock.tick(cfg.FRAMERATE) pygame.display.flip() if not board.computing and not board.finished: if board.current_player.AI: board.computing = True thread = threading.Thread(target=board.computer_move) thread.start() cont = dispatch(pygame.event.get(), board) del board.rows pygame.quit() if cfg.NETWORK_ENABLED: board.server.terminate() if cfg.CACHE_ENABLED: for pawn in board.pawns: if pawn.AI is not None: pawn.AI.flush_cache() log('Memoized nodes: %i' % core.MEMOIZED_NODES) log('Memoized nodes hits: %i' % core.MEMOIZED_NODES_HITS) for pawn in board.pawns: log('Memoized distances for [%i]: %i' % (pawn.id, pawn.distances.MEMO_COUNT)) log('Memoized distances hits for [%i]: %i' % (pawn.id, pawn.distances.MEMO_HITS)) log('Exiting. Bye!') return 0
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
def test_remove_piece_empty_cell(self): board = Board() board.remove_piece(Position(3, 4)) assert board.get_piece(Position(3, 4)) is None
def is_position_enemy(pos: Position, colour: Colour, board: Board) -> bool: """Check if position occupied with enemy piece.""" # Retrieve piece. piece = board.get_piece(pos) return piece is not None and piece.colour != colour
def create_start_game() -> Game: start_board = Board.create_start_board() return Game(start_board, Colour.WHITE)
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)))
def main(): _ = Board.create_start_board() _ = Game.create_start_game()
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)), ]