def test_board(): board = Board() board_length = len(board) assert board_length == 3 for cell in board_length: assertEqual(len(cell),3)
def test_EmptyBoard(self): # Niguem joga nenhuma posicao. # Todas as posicoes sao livres. board = Board() for row in range(3): for col in range(3): self.assertTrue(board.IsFree(row, col))
def test_win_row1(self): board = Board() board.reset_board() self.assertEqual(0, board.player_move(0,0,1)) self.assertEqual(0, board.player_move(1,0,2)) self.assertEqual(0, board.player_move(0,1,1)) self.assertEqual(0, board.player_move(1,1,2)) self.assertEqual(1, board.player_move(0,2,1))
def test_minimax(): new_game = Game() new_board = Board() new_board.board = [["O", "O", "X"], ["X", "?", "O"], ["?", "?", "X"]] new_game.board = new_board new_game.player1 = Player(1, "Human", "O") new_game.player2 = Player(2, "Computer", "X") assert new_game.expert_ai(new_game.board, -math.inf, math.inf, "X") == (1, 2, 1)
def test_minimax_2(): new_game = Game() new_board = Board() new_board.board = [["?", "O", "?"], ["X", "O", "?"], ["O", "X", "X"]] new_game.board = copy.deepcopy(new_board) new_game.player1 = Player(1, "Human", "O") new_game.player2 = Player(2, "Computer", "X") assert new_game.expert_ai(new_game.board, -math.inf, math.inf, "X") == (0, 0, 2)
def test_StopsAfterNine(self): board = Board() player_one = MockCountingPlayer("x") player_two = MockCountingPlayer("o") game = GamePlay() game.TheGame(player_one, player_two) # In total, the players should take 9 turns. self.assertEqual(player_one.counter + player_two.counter, 9) # One player should have 4 turns and the other should have 5 turns. self.assertEqual(abs(player_one.counter - player_two.counter), 1)
def test_PlayedPosition(self): # Um jogador vai jogar uma posicao... Teste se a mudansa foi feita no # tabuleiro. Repare que a valor do "player_id" nao importa. board = Board() player_id = "x" row = 2 col = 1 board.PlayPosition(row, col, player_id) self.assertTrue(board.HasPosition(row, col, player_id)) self.assertFalse(board.IsFree(row, col))
def test_minimax_3(): new_game = Game() new_board = Board() new_board.board = [["?", "?", "?"], ["?", "O", "?"], ["X", "O", "X"]] new_game.board = new_board new_game.player1 = Player(1, "Human", "O") new_game.player2 = Player(2, "Computer", "X") new_game.current_turn = new_game.player2 result = new_game.expert_ai(new_game.board, -math.inf, math.inf, "X") assert result[1] == 0 assert result[2] == 1
def test_StopsEarlyIfWinner(self): board = Board() # One player doesn't do anything (except count turns). player_one = MockCountingPlayer("x") player_two = MockAdvancingPlayer("o") # Other player plays one position at a time (and counts turns). # Once one row is full, game play should stop early. # Verify that it took less than 9 turns. game = GamePlay() game.TheGame(player_one, player_two) self.assertLess(player_one.counter + player_two.counter, 9)
def test_simple_draw(self): board = Board() board.reset_board() self.assertEqual(0, board.player_move(2,2,2)) self.assertEqual(0, board.player_move(0,0,1)) self.assertEqual(0, board.player_move(0,1,2)) self.assertEqual(0, board.player_move(0,2,1)) self.assertEqual(0, board.player_move(1,0,2)) self.assertEqual(0, board.player_move(1,1,1)) self.assertEqual(0, board.player_move(1,2,2)) self.assertEqual(0, board.player_move(2,1,1)) self.assertEqual(-1, board.player_move(2,0,2))
def test_PlayerWinsWithOtherDiagonal(self): # Um jogador vai jogar 3 posicoes na transversal. Teste que assim vai # vencer com diagonal, e nao com linha ou coluna. board = Board() player_id = "x" board.PlayPosition(2, 0, player_id) board.PlayPosition(1, 1, player_id) board.PlayPosition(0, 2, player_id) self.assertTrue(board.WonWithTransversal(player_id)) self.assertFalse(board.WonWithRow(player_id)) self.assertFalse(board.WonWithCol(player_id))
def test_EscolheParaGanharComLinha(self): board = Board() player_id = "x" computer_player = ComputerPlayer(computer_id=player_id, human_id="o") # Set pattern: # * | x | x Expect (0, 0) # ---+---+--- # | | # ---+---+--- # | | board.PlayPosition(0, 1, player_id) board.PlayPosition(0, 2, player_id) computer_player.Play(board) self.assertTrue(board.HasPosition(0, 0, player_id)) self.assertTrue(board.PlayerWon(player_id))
def test_NaoDeixaOutroJogadorVencerComLinha(self): board = Board() player_id = "x" other_player_id = "o" computer_player = ComputerPlayer(computer_id=player_id, human_id=other_player_id) # Set pattern: # * | o | o Expect (0, 0) to be "x" # ---+---+--- # | | # ---+---+--- # | | board.PlayPosition(0, 1, other_player_id) board.PlayPosition(0, 2, other_player_id) computer_player.Play(board) self.assertTrue(board.HasPosition(0, 0, player_id))
def next_move_and_result(player, board_string): new_board = Board() flat_board = board_string.split() board_array = [flat_board[0:3], flat_board[3:6], flat_board[6:9]] new_board.board = board_array new_game = Game() new_game.board = new_board new_game.player1 = Player(1, "Human", "O") new_game.player2 = Player(2, "Computer", "X") result, best_row, best_column = new_game.expert_ai(new_game.board, player) board_array[best_row][best_column] = "*" new_board_string = "\n ".join(" ".join(row) for row in board_array) return """ Position score: {} {} """.format(result, new_board_string)
def test_VaiJogarParaVencerEmVezDeBloquar(self): board = Board() player_id = "x" other_player_id = "o" computer_player = ComputerPlayer(computer_id=player_id, human_id=other_player_id) # Set pattern: # | o | o Expect (2, 2) to be "x" # ---+---+--- # | | # ---+---+--- # x | x | * board.PlayPosition(0, 1, other_player_id) board.PlayPosition(0, 2, other_player_id) board.PlayPosition(2, 0, player_id) board.PlayPosition(2, 1, player_id) computer_player.Play(board) self.assertTrue(board.HasPosition(2, 2, player_id)) self.assertTrue(board.PlayerWon(player_id))
def test_EscolheParaGanharComLinha(self): board = Board() player_id = "x" computer_player = ComputerPlayer(computer_id=player_id, human_id="o")
def __init__(self): self._board = Board() self.state = 1 self.turns = 0
def test_NaoDeixaOutroJogadorVencerComOutroTraversal(self): board = Board() player_id = "x" other_player_id = "o" computer_player = ComputerPlayer(computer_id=player_id, human_id=other_player_id)
def test_state(self): board = Board() board.update((0, 0), circle=True) board.update((0, 1), circle=True) board.update((0, 2), circle=True) assert board.game_state == board.CIRCLE_WON
def test_IsValidForDigit(self): # Verifique que IsValid() retorne true se e um int >=0, <9. board = Board() human_player = HumanPlayer(player_id="x") for val in range(9): self.assertTrue(board, human_player.IsValid(board, chr(val)))
def test_IsValidForBadInput(self): # Verifique que IsValid() retorne false se nao e int >=0, <9. board = Board() human_player = HumanPlayer(player_id="x") self.assertFalse(human_player.IsValid(board, "a"))
def test_free(self): board = Board() board.update((0, 0), circle=True) assert board.is_free((0, 0)) == False assert board.is_free((1, 0)) == True