def test_run(): updated_modules = [] info = open(INFO_LOCATION, 'w+') info.close() for module_index in range(len(modules)): if modules[module_index] == '-d': assert (module_index + 1) < len(modules), "Got the dir flag with no argument" module_directory = modules[module_index + 1] assert os.path.isdir(module_directory), "There is no directory named {}".format(module_directory) if not (module_directory[-1] == '/'): module_directory += '/' add_to_modules = get_list_of_filenames(module_directory) for module in add_to_modules: updated_modules.append(module) elif modules[module_index - 1] == '-d': continue else: add_to_modules = modules[module_index] updated_modules.append(add_to_modules) #print("here are the module names in pd: " + str(updated_modules)) teams = load_modules(updated_modules) play_tournament(teams) updated_modules = []
def main(): rounds = int(raw_input("Rounds: ")) scores = [] for _ in range(rounds): scores += [prisoners_dilemma.play_tournament(6, 4)] summ = 0 for a in scores: summ += a print("Average: " + str(summ / rounds) + "\nPoints: " + str(summ))
def main(): rounds = int(raw_input("Rounds: ")) '''scores = [] for _ in range(rounds): scores += [prisoners_dilemma.play_tournament(6, 4)] summ = 0 for a in scores: summ += a print("Average: " + str(summ / rounds) + "\nPoints: " + str(summ))''' numberOfPlayers = int(raw_input("Number of Players: ")) statisticsOnWhom = int(raw_input("Who should statistics be reported on?: ")) print("Points for " + str(rounds) + "-ish rounds: " + str(prisoners_dilemma.play_tournament(numberOfPlayers, statisticsOnWhom, rounds)))
def gather_intel(): global game_flag global best_strat_index list_test = [] my_modules = modules[:] if mendoza in my_modules: my_modules.remove(mendoza) scores, moves = prisoners_dilemma.play_tournament(my_modules) epic_list = scores[:] for index in range(len(my_modules)): list_test.append(sum(scores[index])) max_score = max(list_test) best_strat_index = list_test.index(max_score) if best_strat_index >= modules.index(mendoza): best_strat_index += 1 game_flag = 1
def main(): prisoners_dilemma.play_tournament(int(raw_input("Players: ")))
def play_strategy(strategy): #print("running play_strategy in pd") info = open(INFO_LOCATION, 'w+') info.close() for module_index in range(len(modules)): if modules[module_index] == '-d': assert (module_index + 1) < len(modules), "Got the dir flag with no argument" module_directory = modules[module_index + 1] assert os.path.isdir(module_directory), "There is no directory named {}".format(module_directory) if not (module_directory[-1] == '/'): module_directory += '/' add_to_modules = get_list_of_filenames(module_directory) for module in add_to_modules: updated_modules.append(module) elif modules[module_index - 1] == '-d': continue else: add_to_modules = modules[module_index] updated_modules.append(add_to_modules) if MODULE_LOCATION in updated_modules: updated_modules.remove(MODULE_LOCATION) else: assert False, "module not in updated_modules, MODULE_LOCATION may be incorrect" def collude_clone(): """always colludes""" location = make_file_in_prisoners_dilemma('collude.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' '*4 + 'return \'c\'\n') f.close() updated_modules.append(location) def betray_clone(): """always betrays""" location = make_file_in_prisoners_dilemma('betray.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'return \'b\'\n') f.close() updated_modules.append(location) def grudge_clone(): """colludes, but holds a grudge""" location = make_file_in_prisoners_dilemma('grudge.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if \'b\' in their_history:''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.write(' ' * 4 + 'else:''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.close() updated_modules.append(location) def early_history_clone(): """"uses early history, from example4.py""" location = make_file_in_prisoners_dilemma('early_history.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if len(my_history)==0:''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.write(' ' * 4 + 'else:''\n') f.write(' ' * 8 + 'recent_round_them = their_history[-1]''\n') f.write(' ' * 8 + 'recent_round_me = my_history[-1]''\n') f.write(' ' * 8 + 'for round in range(len(my_history)-1):''\n') f.write(' ' * 12 + 'prior_round_them = their_history[round]''\n') f.write(' ' * 12 + 'prior_round_me = my_history[round]''\n') f.write(' ' * 12 + 'if (prior_round_me == recent_round_me) and (prior_round_them == recent_round_them):''\n') f.write(' ' * 16 + 'return their_history[round]''\n') f.write(' ' * 8 + 'if my_history[-1]==\'c\' and their_history[-1]==\'b\':''\n') f.write(' ' * 12 + 'return \'b\'''\n') f.write(' ' * 8 + 'else:''\n') f.write(' ' * 12 + 'return \'c\'''\n') f.close() updated_modules.append(location) def once_clone(): """betrays 12 and 13""" location = make_file_in_prisoners_dilemma('once.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if len(my_history) == 12 or 13:''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.write(' ' * 4 + 'else: ''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.close() updated_modules.append(location) def antihistory_clone(): """counters early history""" location = make_file_in_prisoners_dilemma('antihistory.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if len(my_history) == 0 or 2:''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.write(' ' * 4 + 'else: ''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.close() updated_modules.append(location) def once_2_clone(): """counters WizalDOG""" location = make_file_in_prisoners_dilemma('once.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if len(my_history) <= 13:''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.write(' ' * 4 + 'else: ''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.close() updated_modules.append(location) def copycat_clone(): """copies previous round results""" location = make_file_in_prisoners_dilemma('copycat.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if len(my_history) == 0:''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.write(' ' * 4 + 'else:''\n') f.write(' ' * 8 + 'return their_history[-1]''\n') f.close() updated_modules.append(location) def ccb_clone(): """goes ccb over and over""" location = make_file_in_prisoners_dilemma('ccb.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if (len(my_history)+1)%3 == 0:''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.write(' ' * 4 + 'else: ''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.close() updated_modules.append(location) def educated_guess_clone(): """"counters educated guess""" location = make_file_in_prisoners_dilemma('educated_guess.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'betrays = len(list(filter(lambda x: x == \'b\', my_history))) - len(list(filter(lambda x: x == \'b\', their_history)))''\n') f.write(' ' * 4 + 'colludes = len(list(filter(lambda x: x == \'c\', my_history))) - len(list(filter(lambda x: x == \'c\', their_history)))''\n') f.write(' ' * 4 + 'if betrays < colludes:''\n') f.write(' ' * 8 + 'c = \'b\'''\n') f.write(' ' * 4 + 'else:''\n') f.write(' ' * 8 + 'c = \'c\'''\n') f.write(' ' * 4 + 'return c''\n') f.close() updated_modules.append(location) def wizal_dog_clone(): """"counters WizalBOB and ROB""" location = make_file_in_prisoners_dilemma('wizal_dog.py') f = open(location, 'w+') f.write('team_name = \'?\'\n') f.write('strategy_name = \'?\'\n') f.write('strategy_description = \'?\'\n') f.write('def move(my_history, their_history, my_score, their_score):\n') f.write(' ' * 4 + 'if len(my_history) < 15:''\n') f.write(' ' * 8 + 'return \'c\'''\n') f.write(' ' * 4 + 'elif str(their_history[0] + their_history[1] + their_history[2] + their_history[3]) == \'bccb\':''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.write(' ' * 4 + 'else:''\n') f.write(' ' * 8 + 'return \'b\'''\n') f.close() updated_modules.append(location) assert strategy in STRATEGIES, "Not a valid strategy in play_strategy" if strategy == 'COLLUDE': collude_clone() # print("Running collude_clone") elif strategy == 'BETRAY': betray_clone() # print("Running betray_clone") elif strategy == 'GRUDGE': grudge_clone() # print("Running grudge_clone") elif strategy == 'EARLY_HISTORY': early_history_clone() elif strategy == 'ONCE': once_clone() elif strategy == 'COPYCAT': copycat_clone() elif strategy == 'CCB': ccb_clone() elif strategy == "EDUCATED_GUESS": educated_guess_clone() elif strategy == "WIZAL_DOG": wizal_dog_clone() elif strategy == "ONCE_2": once_2_clone() elif strategy == "ANTIHISTORY": antihistory_clone() else: assert False, "No code for that strategy" #print("In pd: " + str(updated_modules)) teams = load_modules(updated_modules) all_scores = play_tournament(teams)[0] # print("all scores" + str(all_scores)) h = all_scores[::-1] my_score = h[0] scores_for_strategy[strategy] = my_score # print("Scores for " + strategy + ": " + str(scores_for_strategy[strategy])) their_scores = all_scores[0:len(all_scores) - 1] their_scores_against_me = [] for score in their_scores: lst = score[::-1] their_scores_against_me.append(lst[0]) their_scores_for_strategy[strategy] = their_scores_against_me # print("Opponent scores for "+ strategy + ": "+ str(their_scores_against_me)) for item in ["collude.py","betray.py", "grudge.py", "early_history.py", "once.py", "copycat.py", "ccb.py", "educated_guess.py", "wizal_dog.py", "once_2.py", "antihistory.py"]: if os.path.exists(os.path.join(PRISONERS_DILEMMA_LOCATION, item)): os.remove(os.path.join(PRISONERS_DILEMMA_LOCATION, item)) updated_modules[:] = [] os.remove(INFO_LOCATION)