def main(): tokens = { 'Red': isolation.Board.RED_TOKEN, 'Blue': isolation.Board.BLUE_TOKEN } exceptions = [] scores = {"aqua": 0, "random": 0} for i in range(10): us = random.choice(list(tokens.keys())) them = 'Red' if us == 'Blue' else 'Blue' try: if us == 'Red': isolation.Board.set_dimensions(6, 8) our_player = aqua.Aqua('Red', isolation.Board.RED_TOKEN) match = isolation.Match( RandomPlayer('Blue', isolation.Board.BLUE_TOKEN), our_player, isolation.Board()) else: isolation.Board.set_dimensions(6, 8) our_player = aqua.Aqua('Blue', isolation.Board.BLUE_TOKEN) match = isolation.Match( our_player, RandomPlayer('Red', isolation.Board.RED_TOKEN), isolation.Board()) match.start_play() if match.winner() == our_player: scores["aqua"] = scores["aqua"] + 1 else: scores["random"] = scores["random"] + 1 except Exception as e: exceptions.append(e) print("Aqua's Score: ", scores["aqua"]) print("Enemy Score: ", scores["random"]) print("Error count: ", len(exceptions))
def run_tournament(agent_classes): """ Play a bunch of matches between the teams listed in teams :param agent_classes: a dictionary of player classes :return: """ # Create new red players # red_agents = {team: agent_classes[team](team, isolation.Board.RED_TOKEN) for team in agent_classes} # # blue_agents = {team: agent_classes[team](team, isolation.Board.BLUE_TOKEN) for team in agent_classes} pairings = [ pair for pair in itertools.product(agent_classes.keys(), agent_classes.keys()) if pair[0] != pair[1] ] print(pairings) for team1, team2 in pairings: # Flip a coin to see who goes first if random.choice(('H', 'T')) == 'H': blue_player = agent_classes[team1](team1, isolation.Board.BLUE_TOKEN) red_player = agent_classes[team2](team2, isolation.Board.RED_TOKEN) else: blue_player = agent_classes[team2](team2, isolation.Board.BLUE_TOKEN) red_player = agent_classes[team1](team1, isolation.Board.RED_TOKEN) board = isolation.Board() match = isolation.Match(blue_player, red_player, board) match.start_play() moves = board.moves() filename = f'{blue_player.name()}_{red_player.name()}.txt' with open(filename, 'w') as log_file: print('\n'.join(str(move) for move in moves), file=log_file) input('Hit RETURN to continue')
tiled_spaces.discard(to_space_id) # if space_id not in board.start_squares(): # tiled_spaces.add(space_id) tiled_spaces.add(space_id) if debug: print('possible push outs:', tiled_spaces) push_out_space_id = random.choice(list(tiled_spaces)) # print(' Moving to', to_space_id, 'and pushing out', push_out_space_id) move = isolation.Move(to_space_id, push_out_space_id) if debug: print(' ', move) return move if __name__ == '__main__': # Create a match isolation.Board.set_dimensions(6, 8) match = isolation.Match(RandomPlayer('Blue', isolation.Board.BLUE_TOKEN), RandomPlayer('Red', isolation.Board.RED_TOKEN), isolation.Board()) match.start_play() # # Play 100 more matches # for i in range(100): # match = isolation.Match(RandomPlayer('Blue', isolation.Board.BLUE_TOKEN), # RandomPlayer('Red', isolation.Board.RED_TOKEN)) # print(match.start_play()) # print('*' * 40)
opponent_location = board.token_location(self._opponent) self_neighbor_tiles = board.neighbor_tiles(self_location) opponent_neighbor_tiles = board.neighbor_tiles(opponent_location) return len(self_neighbor_tiles) == 0 or len( opponent_neighbor_tiles) == 0 def is_max_node(self): return self._token is isolation.Board.BLUE_TOKEN if __name__ == '__main__': # # Create a match isolation.Board.set_dimensions(6, 8) board = isolation.Board() # board.set_state(16, 20, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,14, 15, 24, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,46, 47]) # board.set_state(13, 9, [0, 4, 5, 6, 8, 12, 18, 19, 21, 23, 24, 25, 26, 28, 30, 32, 33, 34, 35, 36, 41, 42, 43, 44]) match = isolation.Match( PlayerAgent('Blue', isolation.Board.BLUE_TOKEN), randomplayer.RandomPlayer('Red', isolation.Board.RED_TOKEN), board) match.start_play() # # Play 100 more matches # for i in range(100): # match = isolation.Match(PlayerAgent('Blue', isolation.Board.BLUE_TOKEN), # randomplayer.RandomPlayer('Red', isolation.Board.RED_TOKEN), # isolation.Board()) # print(match.start_play()) # print('*' * 40)
print(board) space_id = board.token_location(self.token()) print('Current location:', space_id) move_to_spaces = board.neighbor_tiles(space_id) to_space_id = int(input('Moves {}: '.format(move_to_spaces))) push_out_space_ids = board.tile_square_ids() push_out_space_ids.add(space_id) push_out_space_ids.discard(to_space_id) push_out_space_id = int(input('Tiles {}: '.format(push_out_space_ids))) move = isolation.Move(to_space_id, push_out_space_id) return move if __name__ == '__main__': isolation.Board.set_dimensions(6, 8) # # Create a match # ref = isolation.Match(HumanPlayer('Blue', isolation.Board.BLUE_TOKEN), # HumanPlayer('Red', isolation.Board.RED_TOKEN), # Board()) # ref.start_play() # Create a match using a script in a file print() print('Start scripted game') ref = isolation.Match(HumanPlayer('Blue', isolation.Board.BLUE_TOKEN), HumanPlayer('Red', isolation.Board.RED_TOKEN), isolation.Board()) ref.start_play()
print('Current location:', space_id) move_to_spaces = board.neighbor_tiles(space_id) to_space_id = int(input('Moves {}: '.format(move_to_spaces))) push_out_space_ids = board.tile_square_ids() push_out_space_ids.add(space_id) push_out_space_ids.discard(to_space_id) push_out_space_id = int(input('Tiles {}: '.format(push_out_space_ids))) move = isolation.Move(to_space_id, push_out_space_id) return move if __name__ == '__main__': isolation.Board.set_dimensions(6, 8) # # Create a match # ref = isolation.Match(HumanPlayer('Blue', isolation.Board.BLUE_TOKEN), # HumanPlayer('Red', isolation.Board.RED_TOKEN), # Board()) # ref.start_play() # Create a match using a script in a file print() print('Start scripted game') ref = isolation.Match(HumanPlayer('Blue', isolation.Board.BLUE_TOKEN, '2018-11-25 15:52:29.435889.csv'), HumanPlayer('Red', isolation.Board.RED_TOKEN, '2018-11-25 15:52:29.435889.csv'), isolation.Board()) ref.start_play()
def run_tournament(agent_classes, start_match_index=0, tournament_run_count=1, debug=False): """ Play a bunch of matches between the teams listed in teams :param debug: if set to true, it requires user input before going to the next match :param tournament_run_count: specifies how many tournaments to run :param start_match_index: if the code breaks, you can choose where to start :param agent_classes: a dictionary of player classes :return: """ # Create new red players # red_agents = {team: agent_classes[team](team, isolation.Board.RED_TOKEN) for team in agent_classes} # # blue_agents = {team: agent_classes[team](team, isolation.Board.BLUE_TOKEN) for team in agent_classes} pairings = [ pair for pair in itertools.product(agent_classes.keys(), agent_classes.keys()) if pair[0] != pair[1] ] print(pairings) wins = {team: 0 for team in agent_classes} results = [] for i in range(tournament_run_count): for team1, team2 in pairings[start_match_index:]: # Flip a coin to see who goes first if random.choice(('H', 'T')) == 'H': blue_player = agent_classes[team1](team1, isolation.Board.BLUE_TOKEN) red_player = agent_classes[team2](team2, isolation.Board.RED_TOKEN) else: blue_player = agent_classes[team2](team2, isolation.Board.BLUE_TOKEN) red_player = agent_classes[team1](team1, isolation.Board.RED_TOKEN) board = isolation.Board() match = isolation.Match(blue_player, red_player, board) match.start_play() wins[match.winner().name()] += 1 results.append({ 'blue': blue_player.name(), 'red': red_player.name(), 'winner': match.winner().name() }) moves = board.moves() filename = f'{blue_player.name()}_{red_player.name()}.txt' if not os.path.isdir('Matches'): os.mkdir('Matches') with open('Matches/' + filename, 'w') as log_file: print('\n'.join(str(move) for move in moves), file=log_file) with open('results.txt', 'w') as results_file: print('\n'.join(str(result) for result in results), file=results_file) if debug: input('Hit RETURN to continue')
} # The local team is always blue local_team = input('Enter the local team color: ') remote_team = input('Enter the remote team color: ') blue_player_local = input( 'Is the local player BLUE [y/n]? ').strip() in 'yY' match_id = int(input('Enter the match ID: ')) if blue_player_local: blue_player = LocalPlayer(f'Local Player {local_team}', isolation.Board.BLUE_TOKEN, match_id, local_team, agent_classes[local_team]) red_player = RemotePlayer(f'Remote Player {remote_team}', isolation.Board.RED_TOKEN, match_id, remote_team, agent_classes[remote_team]) else: blue_player = RemotePlayer(f'Remote Player {remote_team}', isolation.Board.BLUE_TOKEN, match_id, remote_team, agent_classes[remote_team]) red_player = LocalPlayer(f'Local Player {remote_team}', isolation.Board.RED_TOKEN, match_id, remote_team, agent_classes[remote_team]) board = isolation.Board() match = isolation.Match(blue_player, red_player, board) match.start_play()
Make a move on the isolation board :param board: an Board object :return: Return a Move object """ print("\n{} taking turn: ".format(self._name), end='') tiled_spaces = board.push_outable_square_ids() my_space_id = board.token_location(self._token) to_space_id = PlayerAgent.self_heuristic(self, board) tiled_spaces.discard(to_space_id) tiled_spaces.add(my_space_id) neighbors = board.neighbor_tiles(my_space_id) print('possible moves:', neighbors) push_out_space_id = PlayerAgent.push_out_heuristic(self, board, to_space_id) move = isolation.Move(to_space_id, push_out_space_id) print(' ', move) return move if __name__ == '__main__': # Create a match isolation.Board.set_dimensions(6, 8) match = isolation.Match(PlayerAgent('Blue', isolation.Board.BLUE_TOKEN), PlayerAgent('Red', isolation.Board.RED_TOKEN), isolation.Board()) match.start_play()