def test_game_traces_game_sequence_correctly(): game = Game(CPUAgent(), CPUAgent()) game.apply_move(Action.top_left, 1) game.apply_move(Action.top_center, 2) game.apply_move(Action.top_right, 2) game.apply_move(Action.middle_left, 2) game.apply_move(Action.middle_center, 2) game.apply_move(Action.middle_right, 1) game.apply_move(Action.bottom_left, 1) game.apply_move(Action.bottom_center, 1) game.apply_move(Action.bottom_right, 2) expected_game_sequence = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2, 0, 0, 0, 0, 0, 0, 0], [1, 2, 2, 0, 0, 0, 0, 0, 0], [1, 2, 2, 2, 0, 0, 0, 0, 0], [1, 2, 2, 2, 2, 0, 0, 0, 0], [1, 2, 2, 2, 2, 1, 0, 0, 0], [1, 2, 2, 2, 2, 1, 1, 0, 0], [1, 2, 2, 2, 2, 1, 1, 1, 0], [1, 2, 2, 2, 2, 1, 1, 1, 2], ]) assert_array_equal(game.game_sequence, expected_game_sequence)
def test_game_detects_grid_is_full_correctly(grid, expected_grid_is_full, expected_result, force_win): game = Game(CPUAgent(), CPUAgent()) game.grid = grid if force_win: game.result = 1 game.check_if_grid_is_full() assert game.grid_is_full == expected_grid_is_full assert game.result == expected_result
def test_game_prints_correct_board(grid, expected_result): game = Game(CPUAgent(), CPUAgent()) game.grid = grid printed_board = game.print_board() wrappers = {"X": "\x1b[31m", "O": "\x1b[36m"} for pos in printed_board.keys(): wrapper_ascii = wrappers[expected_result[pos]]if expected_result[pos] in wrappers else "\x1b[32m" assert printed_board[pos] == f"{wrapper_ascii}{expected_result[pos]}\x1b[39m"
def play_human_vs_cpu(cpu_weights_path: str, cpu_mode: str) -> None: """ Plays a game between a human player and a CPU agent. Parameters ---------- cpu_weights_path: str Path to the JSON file with the first player's weights cpu_mode: str Player 1 policy. Whether best or random. """ assert cpu_mode in {"random", "best"} cpu_agent = CPUAgent() cpu_agent.load(cpu_weights_path) play_game_player_vs_comp(cpu_agent, cpu_mode)
def test_run_simulation_runs_correct_number_of_iterations(mocker, num_iterations): cpu_agent = CPUAgent() mocker.spy(ttt.training, "rewarding") mocker.spy(ttt.training, "play_game_cpu_vs_cpu") _run_simulation(num_iterations, cpu_agent, "best", "random", 0.1) assert ttt.training.rewarding.call_count == num_iterations * 2 assert ttt.training.play_game_cpu_vs_cpu.call_count == num_iterations
def test_game_initializes_correctly(): agent_1 = CPUAgent() agent_2 = HumanAgent() game = Game(agent_1, agent_2) assert game.player_1 == agent_1 assert game.player_2 == agent_2 assert_array_equal(game.player_1.grid, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])) assert_array_equal(game.player_2.grid, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])) assert_array_equal(game.grid, np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])) assert_array_equal(game.game_sequence, np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0]]))
def play_cpu_vs_cpu(cpu_1_weights_path: str, cpu_1_mode: str, cpu_2_weights_path: str, cpu_2_mode: str, num_rounds: int, display_board: bool = True, display_text: bool = True) -> None: """ Plays a game between two cpu players. Parameters ---------- cpu_1_weights_path: str Path to the JSON file with the first player's weights cpu_1_mode: str Player 1 policy. Whether best or random. cpu_2_weights_path: str Path to the JSON file with the second player's weights cpu_2_mode: str Player 1 policy. Whether best or random. num_rounds: int Number or rounds to be played. display_board: bool Whether to display the board after each move. display_text: bool Whether to display game information """ cpu_1_agent = CPUAgent() cpu_2_agent = CPUAgent() cpu_1_agent.load(cpu_1_weights_path) cpu_2_agent.load(cpu_2_weights_path) play_game_cpu_vs_cpu(cpu_1_agent, cpu_2_agent, cpu_1_mode, cpu_2_mode, num_rounds, display_board, display_text)
def test_game_detects_victory_correctly(grid, move, player_id, expected_result): game = Game(CPUAgent(), CPUAgent()) game.grid = grid game.apply_move(move, player_id) assert game.result == expected_result
def test_game_applies_move_correctly(move, player_id, expected_result): game = Game(CPUAgent(), CPUAgent()) game.apply_move(move, player_id) assert_array_equal(game.grid, expected_result)
def test_game_checks_diagonal_win_correctly(grid, expected_result): game = Game(CPUAgent(), CPUAgent()) game.grid = grid game.check_diagonal_win() assert game.result == expected_result
def sample_game(): cpu_agent = CPUAgent() return play_game_cpu_vs_cpu(cpu_agent, cpu_agent, "random", "random", 1, False, False)