def test_it_with_four_by_four(self): board = BaseBoard(4) board.board_state = {1: "x", 2: "x", 3: "x", 5: "o", 6: "o", 7: "o", 8: "x", 9: "x", 10: "o", 11: "x", 12: "x", 13: "o", 14: "x", 15: "o", 16: "x"} self.assertEqual(4,self.x_minimax.next_move(board))
def test_trim_board(self): board = BaseBoard(3) board.make_move(4, "x") board.make_move(9, "o") board_string = board.__str__() expected = " x o" self.assertEqual(expected, self.formatter.trim_board(board_string))
def test_board_gets_updated_and_shows_win(self): easy_win = {1: "o", 2: "o"} WIN_MOVE = 3 board = BaseBoard(3) generator = MoveGenerator(board) self.assertEqual(WIN_MOVE, generator.next_move(easy_win)) self.assertEqual({1: "o", 2: "o", 3: "o"}, board.state())
def test_winner_with_tie(self): board = BaseBoard(base=3) board.board_state = {1:"x",2:"x",3:"o", 4:"o",5:"x",6:"x", 7:"x",8:"o",9:"o"} self.assertTrue(board.is_full()) self.assertEqual(None,board.winner())
def test_trim_board(self): board = BaseBoard(3) board.make_move(4,"x") board.make_move(9,"o") board_string = board.__str__() expected = " x o" self.assertEqual(expected,self.formatter.trim_board(board_string))
def test_next_move_on_full_board(self): board = BaseBoard(3) board.board_state = {1:"x", 2:"o", 3:"x", 4:"o", 5:"x", 6:"x", 7:"x", 8:"o", 9:"o"} ai = ImpossibleAI("x") self.assertEqual(None,ai.next_move(board))
def test_board_gets_updated_and_shows_win(self): easy_win = {1:"o", 2:"o"} WIN_MOVE = 3 board = BaseBoard(3) generator = MoveGenerator(board) self.assertEqual(WIN_MOVE, generator.next_move(easy_win)) self.assertEqual( {1:"o",2:"o",3:"o"}, board.state())
def test_over_with_full_fours_board(self): board = BaseBoard(4) board.board_state = {1:"o",2:"x",3:"x",4:"o", 5:"x",6:"o",7:"o",8:"x", 9:"x",10:"o",11:"o",12:"x", 13:"x",14:"x",15:"o",16:"x"} self.assertFalse(board.winner()) self.assertTrue(board.over())
def test_that_players_dont_move_when_game_over(self): board = BaseBoard(3) board.set_state({1:"x",2:"o",3:"x", 4:"o",5:"x",6:"o", 7:"x",8:"o",9:"x"}) game = Game(self.player_one,self.player_two,board) game.run() self.assertFalse(self.player_one.next_move.called) self.assertFalse(self.player_two.next_move.called)
def test_format_for_speech(self): board = BaseBoard(3) board.board_state = {1:"x", 8:"o", 9:"x"} board_string = board.__str__() row_one = " row 1 x empty empty" row_two = " row 2 empty empty empty" row_three = " row 3 empty o x" expected = (row_one + row_two + row_three) self.assertEqual(expected,self.formatter.format_for_speech(board_string))
def test_player_prompts(self): mock = SimpleMockPrompter([1]) player = HumanPlayer("x",mock) player.next_move(BaseBoard(3)) available_moves = "\nAvailable moves are " + str(BaseBoard(3).available_moves()) move_prompt = "\nPlease select a move: " token_prompt = player.token.capitalize() + " turn" prompts = [available_moves, move_prompt, token_prompt] for prompt in prompts: self.assertTrue(prompt in mock.history_string())
def test_format_for_speech(self): board = BaseBoard(3) board.board_state = {1: "x", 8: "o", 9: "x"} board_string = board.__str__() row_one = " row 1 x empty empty" row_two = " row 2 empty empty empty" row_three = " row 3 empty o x" expected = (row_one + row_two + row_three) self.assertEqual(expected, self.formatter.format_for_speech(board_string))
def test_format_for_speech_with_fours_board(self): board = BaseBoard(4) board.board_state = {1:"x", 8:"o", 9:"x"} board_string = board.__str__() row_one = " row 1 x empty empty empty" row_two = " row 2 empty empty empty o" row_three = " row 3 x empty empty empty" row_four = " row 4 empty empty empty empty" expected = (row_one + row_two + row_three + row_four) self.assertTrue(self.fours_formatter.is_board_string(board_string)) self.assertEqual(expected,self.fours_formatter.format_for_speech(board_string))
def test_that_board_is_printed_when_game_is_over(self): self.player_one.next_move = mock.MagicMock(return_value=1) self.player_two.next_move = mock.MagicMock(return_value=3) board = BaseBoard(3) board.set_state({1:"x",2:"x",3:"x"}) fake_printer = FakePrinter() game = Game(self.player_one,self.player_two,board,prompter=fake_printer) game.run() self.assertEqual(True,game.__over__()) expected_string = game.board.__str__() self.assertTrue(expected_string in fake_printer.history)
def test_format_for_speech_with_fours_board(self): board = BaseBoard(4) board.board_state = {1: "x", 8: "o", 9: "x"} board_string = board.__str__() row_one = " row 1 x empty empty empty" row_two = " row 2 empty empty empty o" row_three = " row 3 x empty empty empty" row_four = " row 4 empty empty empty empty" expected = (row_one + row_two + row_three + row_four) self.assertTrue(self.fours_formatter.is_board_string(board_string)) self.assertEqual(expected, self.fours_formatter.format_for_speech(board_string))
def test_that_board_is_printed_when_game_is_over(self): self.player_one.next_move = mock.MagicMock(return_value=1) self.player_two.next_move = mock.MagicMock(return_value=3) board = BaseBoard(3) board.set_state({1: "x", 2: "x", 3: "x"}) fake_printer = FakePrinter() game = Game(self.player_one, self.player_two, board, prompter=fake_printer) game.run() self.assertEqual(True, game.__over__()) expected_string = game.board.__str__() self.assertTrue(expected_string in fake_printer.history)
def test_next_move_on_full_board(self): board = BaseBoard(3) board.board_state = { 1: "x", 2: "o", 3: "x", 4: "o", 5: "x", 6: "x", 7: "x", 8: "o", 9: "o" } ai = ImpossibleAI("x") self.assertEqual(None, ai.next_move(board))
def test_that_players_dont_move_when_game_over(self): board = BaseBoard(3) board.set_state({ 1: "x", 2: "o", 3: "x", 4: "o", 5: "x", 6: "o", 7: "x", 8: "o", 9: "x" }) game = Game(self.player_one, self.player_two, board) game.run() self.assertFalse(self.player_one.next_move.called) self.assertFalse(self.player_two.next_move.called)
def test_for_prompts(self): mock = self.prompter([1]) humanoid = Humanoid("x", mock) humanoid.next_move(BaseBoard(3)) prompts = [ humanoid.turn_prompt(), "Available moves are ", humanoid.move_prompt(), "Please select a move: " ] for prompt in prompts: self.assertTrue(prompt in mock.history_string())
def test_for_prompts(self): board = BaseBoard(3) fake_prompter = mock.Mock() fake_prompter.display = mock.MagicMock() minimax = mock.Mock() minimax.next_move = mock.MagicMock(return_value=3) computer = ImpossibleAI("x", fake_prompter, minimax) computer.next_move(board) fake_prompter.display.assert_called_with(computer.turn_prompt() + computer.move_prompt() + "3")
def test_that_players_move(self): board = BaseBoard(3) self.player_one.next_move.side_effect = [1, 2, 3] self.player_two.next_move.side_effect = [4, 5, 6] game = Game(self.player_one, self.player_two, board) self.assertEqual(self.player_one, game.current_player) game.run() self.assertEqual(self.player_two, game.current_player) self.player_one.next_move.assert_called_with(board) self.player_two.next_move.assert_called_with(board)
def test_it_with_four_by_four(self): board = BaseBoard(4) board.board_state = { 1: "x", 2: "x", 3: "x", 5: "o", 6: "o", 7: "o", 8: "x", 9: "x", 10: "o", 11: "x", 12: "x", 13: "o", 14: "x", 15: "o", 16: "x" } self.assertEqual(4, self.x_minimax.next_move(board))
def test_available_moves_with_partial_board(self): board = BaseBoard(3) board.board_state = {1:"x",5:"o"} self.assertEqual(range(2,5)+range(6,10),board.available_moves())
def test_erase_move(self): board = BaseBoard(base=2) board.make_move(3,"x") board.erase_move(3) self.assertEqual({},board.state())
def setUp(self): self.board = BaseBoard(3) self.prompter = SimpleMockPrompter
def test_over_with_win(self): self.assertFalse(BaseBoard(base=3).over()) board = BaseBoard(base=3) board.board_state = {1:"x",2:"x",3:"x"} self.assertTrue(board.over())
def test_over_with_non_full_board(self): board = BaseBoard(3) board.board_state = {1:"x", 2:"o"} self.assertFalse(board.over())
def setUp(self): self.x_minimax = Minimax("x", 20) self.o_minimax = Minimax("o", 20) self.board = BaseBoard(3)
def test_over_with_full_threes_board(self): board = BaseBoard(3) board.board_state = {1:"x",2:"x",3:"o", 4:"o",5:"x",6:"x", 7:"x",8:"o",9:"o"} self.assertEqual(True,board.over())
def test_make_move(self): board = BaseBoard(3) board.make_move(1,"x") self.assertEqual({1:"x"},board.state())
def test_set_state_doesnt_alter_original_object(self): board = BaseBoard(3) original_state = {"state":"stuff"} board.set_state(original_state) board.make_move(2,"x") self.assertEqual({"state":"stuff"}, original_state)
def test_winner_with_twos_board(self): board = BaseBoard(base=2) board.board_state = {1:"you",4:"you"} self.assertEqual("you",board.winner())
def test_winner(self): board = BaseBoard(base=3) board.board_state = {1:"me",2:"me",3:"me"} self.assertEqual("me",board.winner())
def test_integration_with_instantiated_minimax(self): minimax = Minimax("o", 1) ai = ImpossibleAI("o", minimax=minimax) board = BaseBoard(3) self.assertTrue(ai.next_move(board) in range(1, 10))
def test_winner_with_no_win(self): board = BaseBoard(base=3) board.board_state = {1:"x",2:"x",3:"o"} self.assertEqual(None,board.winner())
def test_next_move_on_board_thats_won(self): board = BaseBoard(3) board.board_state = {1: "x", 2: "x", 3: "x"} ai = ImpossibleAI("x") self.assertEqual(None, ai.next_move(board))
def __init__(self,board=None): if board is None: board = BaseBoard(3) self.board = board
def test_set_state(self): board = BaseBoard(3) self.assertEqual({},board.state()) board.set_state({1:"x"}) self.assertEqual({1:"x"},board.state())
def test_full_on_full_board(self): board = BaseBoard(base=3) board.board_state = {1:"o",2:"o",3:"x", 4:"x",5:"x",6:"o", 7:"o",8:"o",9:"x"} self.assertTrue(board.is_full())
def test_next_move_on_board_thats_won(self): board = BaseBoard(3) board.board_state = {1:"x", 2:"x", 3:"x"} ai = ImpossibleAI("x") self.assertEqual(None,ai.next_move(board))
def test_it_with_two_by_two(self): board = BaseBoard(2) board.board_state = {1: "x", 3: "shoe", 4: "moo"} self.assertEqual(2, self.x_minimax.next_move(board))
def test_full_with_win(self): board = BaseBoard(3) board.board_state = {1:"x",2:"x",3:"x"} self.assertFalse(board.is_full())
def test_next_move_on_board_won_with_other_token(self): board = BaseBoard(3) board.board_state = {1:"o", 2:"o", 3:"o"} ai = ImpossibleAI("x") self.assertEqual(None,ai.next_move(board))
def test_full_with_fours_board(self): board = BaseBoard(4) board.board_state = {1:"o",2:"o",3:"x", 4:"x",5:"x",6:"o", 7:"o",8:"o",9:"x"} self.assertFalse(board.is_full())
def test_integration_with_default_minimax(self): ai = ImpossibleAI("x") board = BaseBoard(3) board.board_state = {1:"x", 2:"x"} move = ai.next_move(board) self.assertEqual(3,move)
def test_available_moves_with_fours_board(self): self.assertEqual(range(1,17),BaseBoard(base=4).available_moves())
def test_next_move_on_empty_board(self): board = BaseBoard(3) minimax = mock.Mock() minimax.next_move = mock.MagicMock(return_value=2) ai = ImpossibleAI("x", minimax=minimax) self.assertEqual(2, ai.next_move(board))
def test_available_moves_with_threes_board(self): self.assertEqual(range(1,10),BaseBoard(3).available_moves())
def test_next_move_on_board_won_with_other_token(self): board = BaseBoard(3) board.board_state = {1: "o", 2: "o", 3: "o"} ai = ImpossibleAI("x") self.assertEqual(None, ai.next_move(board))
def test_state_returns_only_non_junk(self): garbage_state = {0:"", 5:"o", 6:"x", 9:"x"} board = BaseBoard(3) board.board_state = garbage_state self.assertEqual( {5:"o", 6:"x", 9:"x"}, board.state())
def test_integration_with_default_minimax(self): ai = ImpossibleAI("x") board = BaseBoard(3) board.board_state = {1: "x", 2: "x"} move = ai.next_move(board) self.assertEqual(3, move)
def test_available_moves_with_full_board(self): board = BaseBoard(3) board.board_state = {1:"o",2:"o",3:"x", 4:"x",5:"x",6:"o", 7:"o",8:"o",9:"x"} self.assertEqual([],board.available_moves())
def test_integration_with_minimax_on_4(self): ai = ImpossibleAI("o") board = BaseBoard(4) move = ai.next_move(board) self.assertTrue(move in range(1, 17))
def test_available_moves_with_win_board(self): board = BaseBoard(3) board.board_state = {1:"o",2:"o",3:"o"} # Board assumes available moves not called when game over self.assertEqual(range(4,10),board.available_moves())