def test_it_can_return_available_squares_in_a_flattened_board(self): board_array = [["x", None, "o"], [None, "o", "o"], ["x", "o", None]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) flattened_board = game.get_board().flattened_board() test_strategy = MinimaxStrategy() assert test_strategy.available_squares(flattened_board) == [2, 4, 9]
def test_that_run_returns_a_game_over_message(self, capsys): board = [ ["@", "x", "x"], ["x", "@", "x"], ["@", "x", "@"], ] test_board = Board(board) test_rules = Rules(test_board) test_game = Game(test_board, test_rules) test_game_over = GameOver() game_params = { 'messages': FakeCliMessages(), 'game_input': Input(), 'cli_output': Output(), 'validator': Validator() } human_player = Player("Player 1", "x", "foo") computer_player = Player("Computer", "@", "foo") test_game.add_player(human_player) test_game.add_player(computer_player) test_game.set_current_player(computer_player) test_game_over.run(test_game, game_params) captured = capsys.readouterr() assert captured.out.strip().split("\n")[-1] == "game has ended"
def test_that_it_can_check_for_a_win(self): board = [ ["@", "x", "x"], ["x", "@", "x"], ["@", "x", "@"], ] test_board = Board(board) test_rules = Rules(test_board) test_game = Game(test_board, test_rules) test_messages = CliMessages() test_game_config = GameConfig() test_game_over = GameOver() player_settings_dict = { 'name': 'Player 1', 'icon': 'x', 'turn_order': '1', 'computer_name': 'Computer', 'computer_icon': '@' } result = test_game_config.create_players(player_settings_dict, test_game) assert test_game_over.check_for_win( result, test_messages) == "\nComputer wins!"
def test_that_it_can_unmark_a_square(self, test_game_2): board = [[None, None, None], ["x", "o", "x"], ["o", "x", "x"]] test_board = Board(board) test_rules = Rules(test_board) test_game = Game(test_board, test_rules) square = 9 test_game.unmark_square(square) expected_board = [ [None, None, None], ["x", "o", "x"], ["o", "x", None], ] assert test_game.get_board().board() == expected_board
def test_that_it_can_mark_a_square_on_an_empty_board_correctly( self, test_game_2): board = Board() rules = Rules(board) game = Game(board, rules) square = 9 icon = "x" game.mark_square(square, icon) expected_board = [ [None, None, None], [None, None, None], [None, None, "x"], ] assert game.get_board().board() == expected_board
def test_it_can_return_a_player_score_value(self): board_array = [["x", "x", "o"], ["x", "o", "o"], ["x", "o", "x"]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) test_strategy = MinimaxStrategy() assert test_strategy.player_score_value(game) == -1
def test_game(self): board = [ ["o", "x", "x"], ["x", "o", "x"], ["o", "x", "o"], ] test_board = Board(board) test_rules = Rules(test_board) return Game(test_board, test_rules)
def test_that_intro_prints_a_welcome_message(self, capsys, test_valid_game_config, test_game_config): board = Board() rules = Rules(board) game = Game(board, rules) test_game_config.intro(game, test_valid_game_config) captured = capsys.readouterr() assert captured.out.split("\n\n")[0].strip( ) == "Welcome to Tictactoe, human vs computer version"
def test_that_run_returns_a_game_object(self, test_game_params): board_array = [["x", "x", "o"], ["o", "x", "o"], ["x", "o", "x"]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", InputStrategy()) computer_player = Player("Computer", "o", "foo") game.add_player(human_player) game.add_player(computer_player) game.set_current_player(human_player) test_game_loop = GameLoop() result = test_game_loop.run(game, test_game_params) assert game == result
def test_that_game_loop_can_mark_the_board(self, test_game_params): board_array = [["x", "x", "o"], ["o", None, "o"], ["x", "o", "x"]] expected_board_array = [["x", "x", "o"], ["o", "x", "o"], ["x", "o", "x"]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", InputStrategy()) computer_player = Player("Computer", "o", "foo") game.add_player(human_player) game.add_player(computer_player) game.set_current_player(human_player) test_game_loop = GameLoop() test_game_loop.mark_board(5, game, test_game_params) assert game.get_board().board() == expected_board_array
def test_input_move(self): game_params = { 'messages': CliMessages(), 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) strategy = FakeInputStrategy() human_player = Player("player 1", "x", strategy) assert human_player.select_move(game, game_params) == 5
def test_that_it_picks_a_win(self): board_array = [["x", "o", "x"], ["x", "o", "x"], ["o", None, None]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", "foo") computer_player = Player("Computer", "o", MinimaxStrategy()) game.add_player(computer_player) game.add_player(human_player) game.set_current_player(computer_player) test_strategy = MinimaxStrategy() result = test_strategy.minimax(game) assert result == 8
def test_minimax_move(self): game_params = { 'messages': CliMessages(), 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) strategy = FakeMinimaxStrategy() computer_player = Player("computer", "o", strategy) assert computer_player.select_move(game, game_params) == "5"
def test_that_it_runs_the_loop_again_if_selected_move_is_a_filled_position( self, capsys, test_game_params): board_array = [["x", "x", "o"], ["o", None, "o"], ["x", "o", "x"]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", InputStrategy()) computer_player = Player("Computer", "o", "foo") game.add_player(human_player) game.add_player(computer_player) game.set_current_player(human_player) test_game_loop = GameLoop() test_game_loop.place_move(game, test_game_params) captured = capsys.readouterr() expected_output = "Player 1 selects square 7. Placing Player 1's move.\nThat was an invalid move, please try again. Please select a move between 1 - 9:\nPlayer 1 selects square 5. Placing Player 1's move." assert captured.out.split("\n\n")[0] == expected_output
def run(self): messages = CliMessages() game_params = { 'messages': messages, 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) game = GameConfig().run(game, game_params) game = GameLoop().run(game, game_params) GameOver().run(game, game_params)
def test_that_run_returns_a_game_object_with_an_empty_board( self, test_game_config): game_params = { 'messages': FakeCliMessages(), 'cli_input': FakePlayerConfigInput(), 'cli_output': Output(), 'validator': FakeValidator() } board = Board() rules = Rules(board) game = Game(board, rules) result = test_game_config.run(game, game_params) assert result.get_board().board() == [[None, None, None], [None, None, None], [None, None, None]]
def test_that_best_move_returns_a_move(self): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("363", "7"), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", "foo") computer_player = Player("Computer", "o", "foo") game.add_player(human_player) game.add_player(computer_player) game.set_current_player(computer_player) test_input_strategy = InputStrategy() assert test_input_strategy.best_move(game, game_params) == "7"
def test_that_create_players_creates_the_players_and_returns_an_updated_game( self, test_game_config): board = Board() rules = Rules(board) game = Game(board, rules) player_settings_dict = { 'name': 'Player 1', 'icon': 'x', 'turn_order': '1', 'computer_name': 'Computer', 'computer_icon': '@' } result = test_game_config.create_players(player_settings_dict, game) assert len(result.get_players()) == 2 assert result.get_players()[0] == result.get_current_player() assert result.get_players()[0].name == 'Player 1' assert result.get_players()[1].name == 'Computer'
def test_it_can_return_the_rules(self, test_game, test_board_array): test_board = Board(test_board_array) test_rules = "foo" test_game = Game(test_board, test_rules) assert test_game.get_rules() == "foo"
def test_game_2(self): board = [[None, None, None], ["x", "o", "x"], ["o", "x", None]] test_board = Board(board) test_rules = Rules(test_board) return Game(test_board, test_rules)
def create_game(self, board, rules): return Game(board, rules)
def test_game(self): test_board = Board() test_rules = Rules(test_board) test_game = Game(test_board, test_rules)
def test_that_it_can_place_a_move(self): board_array = [["x", "x", "o"], ["x", None, "o"], ["x", "o", "x"]] expected_board_array = [["x", "x", "o"], ["x", "o", "o"], ["x", "o", "x"]] board = Board(board_array) rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", "foo") computer_player = Player("Computer", "o", "foo") game.add_player(human_player) game.add_player(computer_player) game.set_current_player(computer_player) test_strategy = MinimaxStrategy() test_strategy.place_move(game, 5) assert game.get_board().empty_squares() == [] assert game.get_board().board() == expected_board_array assert game.get_current_player().icon == "x"