def test_players(player1: Player, player2: Player, iterations: int) -> None: """ test_players is a function that runs <iterations> number of games between player1 and player2 """ white = 0 black = 0 ties = 0 for _ in range(iterations): game = ReversiGame() prev_move = (-1, -1) while game.get_winner() is None: move = player1.make_move(game, prev_move) game.try_make_move(move) if game.get_winner() is None: prev_move = player2.make_move(game, move) game.try_make_move(prev_move) if game.get_winner() == 'white': print('White WINS') white += 1 elif game.get_winner() == 'black': print('Black WINS') black += 1 else: print('TIE') ties += 1 print("Player 1 Wins: " + str(black)) print("Player 2 Wins: " + str(white))
def _run_ai_simulation(game_surface: pygame.Surface, size: int, player1: Union[MobilityTreePlayer, PositionalTreePlayer, RandomPlayer, ReversiGame, MCTSTimeSavingPlayer], player2: Union[MobilityTreePlayer, PositionalTreePlayer, RandomPlayer, ReversiGame, MCTSTimeSavingPlayer]) -> None: if size == 8: background = pygame.image.load('assets/gameboard8.png') elif size == 6: background = pygame.image.load('assets/gameboard6.png') else: raise ValueError("invalid size.") game_surface.blit(background, (0, 0)) pygame.display.flip() game = ReversiGame(size) previous_move = '*' board = game.get_game_board() _draw_game_state(game_surface, background, size, board) pass_move = pygame.image.load('assets/pass.png') player1_side = BLACK while game.get_winner() is None: if previous_move == '*' or game.get_current_player() == player1_side: move = player1.make_move(game, previous_move) else: move = player2.make_move(game, previous_move) previous_move = move game.make_move(move) if move == 'pass': surface = game_surface game_surface.blit(pass_move, (300, 300)) pygame.display.flip() pygame.time.wait(500) game_surface.blit(surface, (0, 0)) pygame.display.flip() else: board = game.get_game_board() _draw_game_state(game_surface, background, size, board) pygame.time.wait(500) winner = game.get_winner() if winner == BLACK: victory = pygame.image.load('assets/player1_victory.png') game_surface.blit(victory, (300, 300)) pygame.display.flip() pygame.time.wait(3000) return elif winner == WHITE: defeat = pygame.image.load('assets/player2_victory.png') game_surface.blit(defeat, (300, 300)) pygame.display.flip() pygame.time.wait(3000) return else: draw = pygame.image.load('assets/draw.png') game_surface.blit(draw, (300, 300)) pygame.display.flip() pygame.time.wait(3000) return
def __init__(self, window: VisualReversi, size: int) -> None: tk.Frame.__init__(self) self.window = window self._board_pos = (84, 120) self._board_pixel_size = 637 self.previous_move_dis = '' self._click_wanted = tk.BooleanVar() # initialize game self.game = ReversiGame(size)
def run_game(black: Player, white: Player, size: int, verbose: bool = False) -> tuple[str, list[str]]: """Run a Reversi game between the two given players. Return the winner and list of moves made in the game. """ game = ReversiGame(size) move_sequence = [] previous_move = None timer = {BLACK: 0, WHITE: 0} current_player = black if verbose: game.print_game() while game.get_winner() is None: t0 = time.time() # record time before player make move previous_move = current_player.make_move(game, previous_move) t = time.time() # record time after player make move game.make_move(previous_move) move_sequence.append(previous_move) if verbose: if current_player is black: print(f'{BLACK} moved {previous_move}. Used {t - t0:.2f}s') else: print(f'{WHITE} moved {previous_move}. Used {t - t0:.2f}s') game.print_game() if current_player is black: timer[BLACK] += t - t0 current_player = white else: timer[WHITE] += t - t0 current_player = black # print winner if verbose: print(f'Winner: {game.get_winner()}') print( f'{BLACK}: {game.get_num_pieces()[BLACK]}, {WHITE}: {game.get_num_pieces()[WHITE]}' ) print( f'{BLACK} used {timer[BLACK]:.2f}s, {WHITE} used {timer[WHITE]:.2f}s' ) return game.get_winner(), move_sequence
def check_same(player1: Player, player2: Player) -> None: """ check_same is a function that determines if two players return the same move throughout a game. this is particularly useful for comparison between MinimaxPlayer and MinimaxABPlayer. It also gives the time that each player takes to find a move. You must comment out the random.shuffle() line of code in both players before testing """ game = ReversiGame() prev_move = (-1, -1) while game.get_winner() is None: start_time = time.time() print("Player 1 CHOOSING") move1 = player1.make_move(game, prev_move) print("--- %s seconds ---" % (time.time() - start_time)) start_time = time.time() print("Player 2 CHOOSING") move2 = player2.make_move(game, prev_move) print("--- %s seconds ---" % (time.time() - start_time)) print("Player 1 chose: ", move1, " Player 2 chose: ", move2) assert move1 == move2 game.try_make_move(move1) prev_move = move1
from window import Window from reversi import ReversiGame from board_manager import BoardManager from ai_players import MinimaxABPlayer if __name__ == "__main__": # Initialize PyGame pygame.init() # Create a window wrapper class instance window = Window() # Create a ReversiGame instance # Change human_player to 1 or -1 to play against AI, 0 if no human player game = ReversiGame(human_player=1) # Set to 1 by default. # Setup the BoardManager instance board_manager = BoardManager(window) ui_handler = UIHandler() # Minimax Player player1 = MinimaxABPlayer(2, 8) player2 = MinimaxABPlayer(2, 8) colour_to_player = {1: player1, -1: player2} # Set number of games num_games_left = 40
def _run_ai_game(game_surface: pygame.Surface, size: int, ai_player: Union[MobilityTreePlayer, PositionalTreePlayer, RandomPlayer, ReversiGame, MCTSTimeSavingPlayer], user_side: str = BLACK) -> None: if size == 8: background = pygame.image.load('assets/gameboard8.png') elif size == 6: background = pygame.image.load('assets/gameboard6.png') else: raise ValueError("invalid size.") game_surface.blit(background, (0, 0)) pygame.display.flip() game = ReversiGame(size) previous_move = '*' if user_side == BLACK: ai_side: str = WHITE else: ai_side: str = BLACK board = game.get_game_board() _draw_game_state(game_surface, background, size, board) pass_move = pygame.image.load('assets/pass.png') while game.get_winner() is None: if (previous_move == '*' and user_side == WHITE) or game.get_current_player() == user_side: if game.get_valid_moves() == ['pass']: game.make_move('pass') previous_move = 'pass' surface = game_surface game_surface.blit(pass_move, (300, 300)) pygame.display.flip() pygame.time.wait(1000) game_surface.blit(surface, (0, 0)) pygame.display.flip() continue while True: event = pygame.event.wait() if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if 585 <= mouse_pos[0] <= 795 and 10 <= mouse_pos[1] <= 41: return else: move = _search_for_move(mouse_pos, size) print(move) if move == '' or move not in game.get_valid_moves(): continue else: previous_move = move game.make_move(move) board = game.get_game_board() _draw_game_state(game_surface, background, size, board) pygame.time.wait(1000) break if event.type == pygame.QUIT: return else: move = ai_player.make_move(game, previous_move) previous_move = move game.make_move(move) if move == 'pass': surface = game_surface game_surface.blit(pass_move, (300, 300)) pygame.display.flip() pygame.time.wait(1000) game_surface.blit(surface, (0, 0)) pygame.display.flip() else: board = game.get_game_board() _draw_game_state(game_surface, background, size, board) winner = game.get_winner() if winner == user_side: victory = pygame.image.load('assets/victory.png') game_surface.blit(victory, (300, 300)) pygame.display.flip() pygame.time.wait(3000) return elif winner == ai_side: defeat = pygame.image.load('assets/defeat.png') game_surface.blit(defeat, (300, 300)) pygame.display.flip() pygame.time.wait(3000) return else: draw = pygame.image.load('assets/draw.png') game_surface.blit(draw, (300, 300)) pygame.display.flip() pygame.time.wait(3000) return