From 9c10d723a8232f40e32db6590769dc1d9b6d34d5 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Tue, 30 Jul 2013 22:39:42 -0400 Subject: [PATCH] more special roles --- tim.py | 48 +++++++++++++++++++++++---------------- tim_special.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 tim_special.py diff --git a/tim.py b/tim.py index 9105e02..8b9b5c4 100644 --- a/tim.py +++ b/tim.py @@ -9,6 +9,7 @@ import sys import colorama import json from colorama import Fore, Style +from tim_special import special_votes def ResistanceGame(n_players): @@ -21,7 +22,7 @@ def ResistanceGame(n_players): def AvalonGame(n_players): full_set = [("Merlin", True), ("G", True), ("G", True), ("E", False), ("E", False), ("G", True), - ("E", False), ("G", True), ("G", True), ("E", False)] + ("Mordred", False), ("G", True), ("G", True), ("E", False)] return full_set[:n_players] @@ -40,6 +41,8 @@ class DeceptionGame(object): def __init__(self, player_array): self.player_array = player_array self.all_permutations = list(set(itertools.permutations(player_array))) + self.quick_permutations = list(set(itertools.permutations( + [("", p[1]) for p in player_array]))) self.n_players = len(player_array) self.n_good = len([x for x in player_array if x[1] is True]) self.trace = None @@ -59,6 +62,7 @@ class DeceptionGame(object): self.ignorance_on_round[2] = Bernoulli(0.5) self.ignorance_on_round[3] = Bernoulli(0.3) self.ignorance_on_round[4] = Bernoulli(0.3) + self.merlin_ignorance = Bernoulli(0.2) def player_is_good(self, deal, player): return deal[player][1] @@ -180,12 +184,17 @@ class DeceptionGame(object): n_spies = len(team) - n_actually_good_people could_happen = True for player, vote in enumerate(votes): - if player in team: + role = self.player_role(deal, player) + if role in special_votes: + if special_votes[role](self, player, team, + votes, fail_req, rnd, deal): + continue + else: + return False + elif player in team: continue elif self.player_is_good(deal, player): if n_spies > fail_req - 1: - if self.player_role(deal, player) == "Merlin": - continue if vote == 1: if self.ignorance_on_round[rnd].rand(): continue @@ -246,12 +255,12 @@ class DeceptionGame(object): self.add_known_role(statement["player"], statement["role"]) - def eval(self, length=10, with_merlin=False): + def eval(self, length=10, quick=False): random.seed() - if not with_merlin: - deck = self.all_permutations[:] + if quick: + deck = self.quick_permutations[:] else: - deck = list(set(itertools.permutations(AvalonGame(self.n_players)))) + deck = self.all_permutations[:] new_deck = [] trace = {} progress = progressbar.ProgressBar( @@ -299,12 +308,8 @@ class DeceptionGame(object): out[i]["role"] = dd(float) out[i]["side"] = dd(float) - progress = progressbar.ProgressBar( - widgets=["Reticulating splines: ", - progressbar.Bar(marker="*"), - " ", progressbar.ETA()]) size = sum(self.trace.values()) * 1.0 - for deal, score in progress(self.trace.items()): + for deal, score in self.trace.items(): for i, card in enumerate(deal): role, side = card out[i]["role"][role] += (score * 1.0) / size @@ -354,10 +359,15 @@ def repl_report(report, namemap, ngood): roles = sorted([(v, k) for k, v in report[i]["role"].iteritems()], reverse=True) row = " " + has_roles = True for score, role in roles: + if role == "": + has_roles = False + break row += "%2.1f%% %s " % ( score * 100, role) - print(row) + if has_roles: + print(row) still_good += 1 @@ -392,7 +402,7 @@ def main(): if game is None: if command == "newgame": nplayers = raw_input("How many players? ") - game = DeceptionGame(ResistanceGame(int(nplayers))) + game = DeceptionGame(AvalonGame(int(nplayers))) namemap = {} elif command == "load": if len(command_list) < 2: @@ -405,7 +415,7 @@ def main(): data = observations[1:] game = DeceptionGame( - ResistanceGame(int(metadata["game_size"]))) + AvalonGame(int(metadata["game_size"]))) namemap = metadata["player_names"] game.load_save(data) else: @@ -453,12 +463,12 @@ def main(): times = 200 / (game.n_players - 4) * 2 if len(command_list) > 1: times = int(command_list[1]) - game.eval(times) - elif command == "merlineval": + game.eval(times, quick=True) + elif command == "fulleval": times = 200 / (game.n_players - 4) * 2 if len(command_list) > 1: times = int(command_list[1]) - game.eval(times, with_merlin=True) + game.eval(times) elif command == "report": repl_report(game.report(), namemap, game.n_good) elif command == "save": diff --git a/tim_special.py b/tim_special.py new file mode 100644 index 0000000..0e51f30 --- /dev/null +++ b/tim_special.py @@ -0,0 +1,72 @@ +def merlin_vote(game, player_id, team, votes, fail_req, r, deal): + n_actually_good_people = sum( + [int(game.player_is_good(deal, x)) for x in team]) + n_spies = len(team) - n_actually_good_people + roles = [game.player_role(deal, x) for x in team] + if "Mordred" in roles: + n_spies = n_spies - 1 + + if player_id in team: + return True + # At this point, this is the number of spies Merlin counts + if n_spies == 0: + if votes[player_id] == 0: + # Voted down an apparently good team. Ignore this. + return True + if votes[player_id] == 1: + if game.merlin_ignorance.rand(): + return False + return True + elif n_spies == 1: + # He'll either duck or vote it down. Either is fine + return True + elif n_spies > 1 and fail_req == 1 and r > 2: + # Even a well-played Merlin would never allow two spies on. + # Unless there's more than one fail required. Then he might + # duck. + if votes[player_id] == 0: + return True + if votes[player_id] == 1: + return False + return True + + + +def mordred_vote(game, player_id, team, votes, fail_req, r, deal): + n_actually_good_people = sum( + [int(game.player_is_good(deal, x)) for x in team]) + n_spies = len(team) - n_actually_good_people + roles = [game.player_role(deal, x) for x in team] + if "Oberon" in roles: + n_spies = n_spies - 1 + + if player_id in team: + return True + # At this point, this is the number of spies Mordred knows. + if n_spies == 0: + # The chance that merlin doesn't know Mordred is 1 - Merlin's ignorance + # Read this as "if Merlin is not ignorant of my role" + if not game.merlin_ignorance.rand(): + if votes[player_id] == 1: + return False + else: + return True + # Merlin is ignorant... + else: + # Vote it up or down, doesn't matter, both could happen + return True + elif n_spies == 1: + # He'll either duck or vote it down. Either is fine + return True + elif n_spies > 1: + # Never vote up a team of more than one spy, unless fail_req is high + if fail_req > 1: + return True + else: + return False + return True + +special_votes = { + "Merlin": merlin_vote, + "Mordred": mordred_vote +}