def assert_symmetry_is(self, num_skip, pieces, expected_pieces): board = Board() set_board(board, pieces) state_generator = self.symmetry.get_symmetries(board.state) for _ in range(num_skip + 1): symmetric_state = next(state_generator) assert_board_state_tuple_is(self, expected_pieces, symmetric_state)
def test_make_human_move(self, get_move_mock): get_move_mock.return_value = 6 self.game.start_game({"x": "Random", "o": "Human"}) self.game.make_computer_move() set_board(self.game.controller.board, "OXX|O-X|---") self.assert_game_info_is( Board.O, [0, 3, 6], Board.X, "OXX|O-X|O--", self.game.make_human_move(" 7 "))
def assert_try_move_is(self, expected_winner, pieces_before, pieces_after, position, piece): set_board(self.board, pieces_before) with self.board.try_move(position, piece) as (winner, state): self.assertEqual(expected_winner, winner) assert_board_is(self, self.board, pieces_after) assert_board_state_tuple_is(self, pieces_after, state) assert_board_is(self, self.board, pieces_before)
def assert_values_after_reward_are(self, values, pieces_list, winner): self.player.set_piece(Board.X) values_dict = {} for pieces, value in zip(pieces_list, values): set_board(self.board, pieces) self.player.store_state() values_dict[get_board_state_tuple(pieces)] = value self.player.set_reward(winner) self.assertEqual(sorted(values_dict.keys()), sorted(self.player.values.keys())) for key, value in self.player.values.items(): self.assertAlmostEqual(self.player.values[key], value)
def test_get_move_for_invalid_move(self, input_mock, stdout_mock): set_board(self.board, "XO-|---|---") input_mock.side_effect = ["1", "2", "3"] self.assertEqual(2, self.player.get_move()) self.assertEqual([call("Enter move: ")]*3, input_mock.call_args_list) self.assertEqual("Invalid move\nInvalid move\n", stdout_mock.getvalue())
def assert_board_format_is(self, formatted_pieces, pieces=""): set_board(self.board, pieces) self.assertEqual(get_expected_formatted_board(formatted_pieces), self.board.format_board())
def assert_winner_is(self, winner, pieces=""): set_board(self.board, pieces) self.assertEqual(winner, self.board.get_winner())
def assert_winning_positions_are(self, positions, pieces=""): set_board(self.board, pieces) self.assertEqual(positions, self.board.get_winning_positions())
def assert_get_available_moves_are(self, positions, pieces=""): set_board(self.board, pieces) self.assertEqual(positions, self.board.get_available_moves())
def assert_move_is(self, valid, position, pieces=""): set_board(self.board, pieces) self.assertEqual(valid, self.board.is_valid_move(position))
def test_make_computer_move(self, get_move_mock): get_move_mock.return_value = 4 self.game.start_game({"x": "Random", "o": "Human"}) set_board(self.game.controller.board, "XXO|O-X|OOX") self.assert_game_info_is(Board.X, [0, 4, 8], Board.O, "XXO|OXX|OOX", self.game.make_computer_move())