def test_player_1_winner(self): game = Game(False) @patch('builtins.input') def run(m_input): m_input.side_effect = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] game.start() run() game.declare_winner() state = game.get_state() self.assertTrue(state.get('is_player_1_winner')) self.assertEqual(['❌', '⭕', '❌', '⭕', '❌', '⭕', '❌', '8️⃣', '9️⃣'], state.get('board'))
def test_mutates_game_state(self): game = Game() moves = Moves(game) assert moves.game is game assert moves.game.state is game.state moves.perform({"position": [1, 1], "player": "X"}) self.assertEqual(len(game.state["moves"]), 1)
def test_move_must_be_within_board(self): game = Game() moves = Moves(game) move = {"position": [-1, 2], "player": "X"} self.assertRaises( Exception, moves.valid, )
def test_empty_positions_list(self): game = Game() moves = Moves(game) self.assertEqual(len(moves.get_empty_positions()), 9) move = {"position": [1, 1], "player": "X"} assert move["position"] not in moves.get_occupied_positions() moves.perform(move) assert move["position"] not in moves.get_empty_positions() self.assertEqual(len(moves.get_empty_positions()), 8)
def test_player_from_symbol(self): game = Game() player = Player(game) Moves(game).perform({ "player": "X", "position": [1,1] }) self.assertEqual(player.player_from_symbol("X"), player.get_players()[0])
def test_empty_constructor(self): """ Game's constructor takes a single, optional parameter: a dict containing a complete or partial game state, which is merged with a copy of the default state to produce the instance's starting state. """ subject = Game() modified_default = default_state modified_default.update({"active_player": "X"}) self.assertEqual(subject.state, modified_default)
def test_second_row_victory(self): game = Game() victory = Victory(game) moves = Moves(game) winning_line = [[2, 1], [2, 2], [2, 3]] winning_moves = [{ "player": "X", "position": position } for position in winning_line] moves.perform_all(winning_moves) self.assertEquals(victory.winning_symbol(), "X")
def test_diagonal_victory(self): game = Game() victory = Victory(game) moves = Moves(game) winning_line = [[1, 1], [2, 2], [3, 3]] winning_moves = [{ "player": "X", "position": position } for position in winning_line] moves.perform_all(winning_moves) assert victory.is_present()
def test_victory_by_filled_row(self): game = Game() victory = Victory(game) moves = Moves(game) # create a winning line consisting of the top row winning_line = [[1, 1], [1, 2], [1, 3]] winning_moves = [{ "player": "X", "position": position } for position in winning_line] moves.perform_all(winning_moves) assert victory.is_present()
def test_victory_has_winner(self): game = Game() victory = Victory(game) moves = Moves(game) # create a winning line consisting of the top row winning_line = ThroughLine(game).rows()[0] winning_moves = [{ "player": "X", "position": position } for position in winning_line] moves.perform_all(winning_moves) self.assertEquals(victory.winning_symbol(), "X")
def test_next_states_on_nearly_full_board(self): game = Game() subject = GameAIPresenter(game) subject.move(1,1,"O") self.assertEqual(len(subject.potential_next_states()),8) subject.move(2,2,"X") self.assertEqual(len(subject.potential_next_states()),7) subject.move(3,3,"O") self.assertEqual(len(subject.potential_next_states()),6) subject.move(1,2,"X") self.assertEqual(len(subject.potential_next_states()),5) subject.move(1,3,"O") self.assertEqual(len(subject.potential_next_states()),4)
def test_three_moves_required(self): game = Game() moves = Moves(game) victory = Victory(game) # create a winning line consisting of the top row winning_line = ThroughLine(game).rows()[0] winning_moves = [{ "player": "X", "position": position } for position in winning_line] for move in winning_moves: moves.perform(move) assert not victory.is_present() or move["position"] == [1, 3]
def test_four_corners(self): corners = Board(Game()).corners() self.assertEqual(len(corners), 4)
def test_board_positions_are_lists(self): # they're not tuples because I have a dream of sending game states # back and forth as JSON positions = Board(Game()).positions() for position in positions: self.assertIsInstance(position, list)
def setUp(self): self.game = Game() self.subject = ThroughLine(self.game) self.board_size = Board(self.game).get_size()
def test_default_game_state_is_not_victorious(self): game = Game() victory = Victory(game) assert not victory.is_present()
def test_nondefault_valid_symbols(self): game = Game({ "players": [ { "symbol": "B", "name": "B"}, \ {"symbol": "A", "name": "A"}] }) player = Player(game) self.assertEqual(set(player.valid_symbols()),{"A","B"})
def test_unknown_key_exception(self): with self.assertRaises(Exception): Game({"badkey": []})
def test_switching_active_player(self): game = Game() player = Player(game) self.assertEqual(player.get_active_symbol(),"X") player.switch_active() self.assertEqual(player.get_active_symbol(),"O")
def test_default_game_state_has_no_winner(self): game = Game() victory = Victory(game) self.assertEqual(victory.winning_symbol(), None)
#! /usr/bin/env python from t3.game import Game g = Game() g.play()
def setUp(self): self.g = Game()
def test_next_states_on_empty_board(self): game = Game() subject = GameAIPresenter(game) self.assertEqual(len(subject.potential_next_states()),9)
def test_nine_board_positions_by_default(self): game = Game() board = Board(game) self.assertEqual(len(board.positions()), 9)
def test_update_board_size(self): subject = Game({"board": {"size": 5}}) self.assertEqual(subject.state["board"]["size"], 5)
def test_get_opponent_symbol(self): game = Game() player = Player(game) symbol = player.get_opponent_symbol("O") self.assertEqual(symbol,"X")
def test_clone_returns_copy(self): subject = Game() clone = subject.clone() assert subject is not clone assert subject.state is not clone.state
def test_starting_player_is_valid(self): game = Game({ "players": [ { "symbol": "B", "name": "B"}, \ {"symbol": "A", "name": "A"}] }) player = Player(game) self.assertIn(player.get_active_symbol(),player.valid_symbols())
def setUp(self): self.game = Game()
def test_update_symbol(self): game = Game({ "players": [ { "symbol": "X", "name": "X"}, {"symbol": "Y", "name": "Y"}] }) player = Player(game)