def test_winner(self): board = Board() self.assertIsNone(board.winner) board = Board() board.state = ['x'] * 9 self.assertEqual(board.winner, 'x')
def test_init(self): board = Board() self.assertEqual(board.state, [i for i in range(9)]) custom_state = ['', 1, 2, 4, 6, 8, 11, 'b'] board = Board(state=custom_state) self.assertEqual(board.state, custom_state)
def test_available_positions(self): initial_state = [i for i in range(9)] board = Board() self.assertEqual(board.available_positions(), initial_state) new_state = initial_state[:] new_state[0] = 'x' board.state = new_state self.assertEqual(board.available_positions(), initial_state[1:])
def get_column(self, board=None) -> int: print("\n*** Hmmm... Let me think... ***") if not board: board = Board() columns = get_possible_moves(board) column = get_winning_column(board, columns) if column: return column column = get_winning_column(board, columns, opponent=True) if column: return column games_per_column = self.number_of_games // len(columns) win_rate = [] for column in columns: results = [ self.get_random_game_result(board, column) for _ in range(games_per_column) ] win_rate.append((results.count(True), column)) return max(win_rate)[1]
def test_many_diagonals(self): board = [EMPTY_CELL * 3, ''.join(['g', 'r', EMPTY_CELL])] b = Board(board) assert b.diagonals == [[EMPTY_CELL], ['g', EMPTY_CELL], ['r', EMPTY_CELL], [EMPTY_CELL], [EMPTY_CELL], [EMPTY_CELL, EMPTY_CELL], [EMPTY_CELL, 'r'], ['g']]
def test_minimax(self): """ I can't possibly test all the possibilities so I present you this one case. """ bot = UnbeatableBot('x') board = Board(state=['x', 'x', 'o', 'o', 'x', 'o', 'x', 'o', 8]) score = bot.minimax('x', board=board) self.assertEqual(score, 10)
def get_winning_column(board: Board, columns: List[int], opponent=False): copied_board = board.rows for column in columns: g = Game(board=Board(rows=copied_board)) try: color = g.get_color_to_move() if opponent: color = g.get_opponent_color() g.make_move(column, color) except GameDrawn: continue except GameWon: return column
def get_random_game_result(self, board, column) -> bool: board_copy = board.rows g = Game(player1=RandomBot(), player2=RandomBot(), board=Board(rows=board_copy)) my_color = g.current_color try: g.make_move(column) except GameDrawn: return False except GameWon: return True while True: try: g.make_move() except IndexError: pass except GameDrawn: return False except GameWon: return g.current_color == my_color
def test_default_board(self): b = Board() b2 = Board([]) assert b == b2
def test_one_cell(self): b = Board([EMPTY_CELL]) assert b.__str__() == EMPTY_CELL
def test_one_diagonal(self): board = [EMPTY_CELL] b = Board(board) assert b.diagonals == [[EMPTY_CELL], [EMPTY_CELL]]
def test_diagonals(self): b = Board() assert len(b.diagonals) == (b.shape[0] + b.shape[1] - 1) * 2
def test_multiple_moves(self): b = Board(['--', 'g-']) assert len(get_possible_moves(b)) > 1
def test_full_column(self): b = Board(['g']) c = b.get_column(0) assert is_column_full(c)
def test_zero_moves(self): b = Board(['g']) assert get_possible_moves(b) == []
def test_no_win(self): b = Board(['ggg-']) columns = [3] assert not get_winning_column(b, columns, opponent=True)
def test_win(self): b = Board(['ggg-']) columns = [3] assert get_winning_column(b, columns) == 3
def test_many_cells(self): b = Board(['rr', 'gg']) assert b.__repr__() == f"<Board(['rr', 'gg'])>"
def test_one_cell(self): b = Board([EMPTY_CELL]) assert b.__repr__() == f"<Board(['{EMPTY_CELL}'])>"
def test_many_cells(self): b = Board(['rr', 'gg']) assert b.__str__() == 'r r\ng g'
def test_no_win(self): b = Board(['rrr-']) columns = [3] assert not get_winning_column(b, columns)
def test_unqual_boards(self): b = Board(['r']) b2 = Board(['g']) assert b != b2
def test_win(self): b = Board(['rrr-']) columns = [3] assert get_winning_column(b, columns, opponent=True) == 3
def test_equal_boards(self): b = Board(['g']) b2 = Board(['g']) assert b == b2
def test_not_full_column(self): b = Board([EMPTY_CELL]) c = b.get_column(0) assert not is_column_full(c)
def test_default_board(self): b = Board() assert len(b) == 42
def test_one_move(self): b = Board(['-']) assert len(get_possible_moves(b)) == 1
def test_custom_board(self): board = [EMPTY_CELL * 12 for _ in range(6)] b = Board(board) assert len(b) == 72
def main(): # clear terminal os.system("clear") # print game info print() print(" --------------------------------") print(" | |") print(" | CONNECT 4 - UAIS PROJECT |") print(" | |") print(" --------------------------------") # get player 1 print() print(" Select player 1 type:") print(" 1. Human") print(" 2. Random") print(" 3. Minimax") choice = input() while choice not in ("1", "2", "3"): print(" invalid input, please try again") choice = input() if choice == "1": player1 = HumanPlayer() elif choice == "2": player1 = RandomPlayer() elif choice == "3": player1 = MinimaxPlayer() # get player 2 print() print(" Select player 2 type:") print(" 1. Human") print(" 2. Random") print(" 3. Minimax") choice = input() while choice not in ("1", "2", "3"): print(" invalid input, please try again") choice = input() if choice == "1": player2 = HumanPlayer() elif choice == "2": player2 = RandomPlayer() elif choice == "3": player2 = MinimaxPlayer() board = Board(player1, player2) playAgain = "Y" while playAgain == "Y": # this actually plays the game board.playGame() print(" Play again? (Y/N)") playAgain = input().upper() while playAgain not in ("Y", "N"): playAgain = input().upper()
def test_default(self): b = Board(shape=(1, 2)) b2 = Board(shape=(2, 1)) assert str(b.rows) == str(b2.columns)