def select_move(self, piece_matrix): """ Higher level method/class that encapsulates search and move determination (logic located in search.py and agents.py). """ mastermind = MinimaxAgent(SEARCH_DEPTH) current_board = Board(piece_matrix, self.color) board_tree = BoardTree(current_board) return mastermind.get_action(board_tree)
def start_player_vs_ai(): agent = MinimaxAgent() data = GameData() screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) game.print_board() game.draw() pygame.display.update() pygame.time.wait(1000) agent_turn = choice([0, 1]) # Processes mouse and keyboard events, dispatching events to the event bus. # The events are handled by the ConnectGame and GameRenderer classes. while not game.game_data.game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game.quit() if event.type == pygame.MOUSEMOTION: bus.emit("mouse:hover", game.renderer, MouseHoverEvent(event.pos[0])) if event.type == pygame.MOUSEBUTTONDOWN: bus.emit("mouse:click", game, MouseClickEvent(event.pos[0])) if event.type == KEYDOWN: if event.key == pygame.K_z: mods: int = pygame.key.get_mods() if mods & pygame.KMOD_CTRL: bus.emit("game:undo", game) if data.turn == agent_turn and not game.game_data.game_over: game.make_movement(agent.get_move(data)) game.update() game.draw() game.update() game.draw()
def start(): agent1 = MinimaxAgent() # red agent2 = RandomAgent() # yellow delay = 0.5 data = GameData() screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) game.print_board() game.draw() pygame.display.update() pygame.time.wait(10) agent1_turn = 0 # Processes mouse and keyboard events, dispatching events to the event bus. # The events are handled by the ConnectGame and GameRenderer classes. while not game.game_data.game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game.quit() sleep(delay) if data.turn == agent1_turn and not game.game_data.game_over: game.make_movement(agent1.get_move(data)) game.update() game.draw() else: game.make_movement(agent2.get_move(data)) game.update() game.draw() game.update() game.draw()
parser.add_argument('nrows', type=int) parser.add_argument('ncols', type=int) parser.add_argument('--prune', action='store_true') parser.add_argument('--depth', type=int) args = parser.parse_args() # print("args:", args) players = [] for p in [args.p1, args.p2]: if p == 'r': player = RandomAgent() elif p == 'h': player = HumanAgent() elif p == 'c': if not args.depth: player = MinimaxAgent() else: if not args.prune: player = MinimaxHeuristicAgent(args.depth) else: player = MinimaxHeuristicPruneAgent(args.depth) players.append(player) start_state = GameState(args.nrows, args.ncols) results = [] w1 = 0 w2 = 0 tie = 0 for i in range(0, 100): results = play_game(players[0], players[1], start_state)
from games import Connect4 from interfaces import Connect4WebInterface from agents import RandomAgent, MinimaxAgent import time import threading game = Connect4() agent = MinimaxAgent(game=game) def alternate_turns(): while True: if game.winner is not None: pass # player 0 is human on interface elif game.player == 0: time.sleep(0.1) # player 1 is agent elif game.player == 1: agent.make_move() threading.Thread(target=alternate_turns).start() interface = Connect4WebInterface(game=game)