From a64734005df3b638f9ffca6e654d6174fdc3645c Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Mon, 29 Jul 2013 18:41:54 -0400 Subject: [PATCH] repl --- tim.py | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/tim.py b/tim.py index 2e19924..cb6aa81 100644 --- a/tim.py +++ b/tim.py @@ -4,6 +4,9 @@ import itertools from collections import defaultdict as dd import pprint import progressbar +import readline +import sys +from clint.textui import colored, puts def ResistanceGame(n_players): @@ -17,8 +20,10 @@ class DeceptionGame(object): self.player_array = player_array self.all_permutations = list(itertools.permutations(player_array)) self.n_players = len(player_array) + self.n_good = len([x for x in player_array if x[1] == True]) self.trace = None self.observations = [] + self.seen = [] self.tid = 0 self.lady_will_duck = mc.Bernoulli("lady_will_duck", 0.5) self.mission_ducks_on_round = [None] * 5 @@ -49,6 +54,7 @@ class DeceptionGame(object): return None transaction.append(obs) self.observations.append(transaction) + self.seen.append(["Known role ", player_id, "Good" if is_good else "Evil"]) self.tid += 1 def add_known_role(self, player_id, role_str): @@ -60,6 +66,7 @@ class DeceptionGame(object): return None transaction.append(obs) self.observations.append(transaction) + self.seen.append(["Known role ", player_id, role_str]) self.tid += 1 def player_sees_player_and_claims(self, p1, p2, claim): @@ -83,6 +90,7 @@ class DeceptionGame(object): return True transaction.append(obs) self.observations.append(transaction) + self.seen.append(["Lady:", p1, "says", p2 , "is", "Good" if claim else "Evil"]) self.tid += 1 def do_mission(self, team, fails, must_fail, r): @@ -111,6 +119,7 @@ class DeceptionGame(object): transaction.append(obs) self.observations.append(transaction) + self.seen.append(["Mission:"] + team + ["with %d fails on round %d" % (fails, r)]) self.tid += 1 def do_vote(self, team, votes, r): @@ -155,6 +164,7 @@ class DeceptionGame(object): transaction.append(obs) self.observations.append(transaction) + self.seen.append(["Vote:"] + team + ["voted %s round %d" % (votes, r)]) self.tid += 1 @@ -193,7 +203,7 @@ class DeceptionGame(object): self.trace = trace def report(self): - if self.trace is None: + if self.trace == []: self.eval() return self.get_player_data() @@ -223,12 +233,128 @@ class DeceptionGame(object): out[deal[i]] += 1 / size return dict(out) + def __str__(self): + return "%d Player Game (%d constraints)" % (self.n_players, len(self.seen)) + + def disbelieve(self, i): + self.observations = self.observations[:i] + self.observations[i + 1:] + self.seen = self.seen[:i] + self.seen[i + 1:] + self.trace = [] def print_report(self): pp = pprint.PrettyPrinter(indent = 4) pp.pprint(self.report()) +def repl_report(report, namemap, ngood): + sort_order = sorted([(report[i]["side"].get(True, 0.0), i) for i in range(len(report))], reverse=True) + still_good = 0 + for goodness, i in sort_order: + row = "%s: %2f%% Good %2f%% Evil" % (namemap.get(i, "") + "(%s)" % str(i), goodness * 100, (1.0 - goodness) * 100) + if still_good < ngood: + puts(colored.cyan(row)) + else: + puts(colored.red(row)) + still_good += 1 + + +def main(): + puts(colored.green("Welcome to Tim the Enchanter v1.0")) + + game = None + namemap = {} + + while True: + command_str = raw_input("%s> " % game) + command_list = command_str.strip().split(" ") + command = command_list[0] + if command == "quit" or command == "q" or command == "exit": + sys.exit(0) + if (command != "newgame" and command != "testgame") and game is None: + puts(colored.red("Need to create a game")) + continue + elif command == "newgame": + nplayers = raw_input("How many players? ") + game = DeceptionGame(ResistanceGame(int(nplayers))) + namemap = {} + continue + elif command == "testgame": + game = DeceptionGame(ResistanceGame(5)) + game.do_vote([1,2], [0,1,1,0,1], 1) + game.do_mission([1,2], 0, False, 1) + game.do_vote([0, 1, 2], [1,1,1,0,1], 2) + game.do_mission([0, 1, 2], 1, False, 2) + game.do_vote([3, 4], [0,0,1,1,1], 3) + game.do_mission([3, 4], 0, False, 3) + game.do_vote([3, 4], [0,0,1,1,1], 4) + game.do_mission([0, 3, 4], 1, False, 4) + namemap = {} + continue + elif command == "ls": + for i, statement in enumerate(game.seen): + name = " ".join([namemap.get(x, str(x)) for x in statement]) + print "%d: %s" % (i, name) + continue + elif command == "vote": + team = [int(x) for x in raw_input("Team? ").strip().split(" ")] + votes = [int(x) for x in raw_input("Votes? ").strip().split(" ")] + round = int(raw_input("Round? ").strip()) + game.do_vote(team, votes, round) + game.trace = [] + continue + + elif command == "mission": + team = [int(x) for x in raw_input("Team? ").strip().split(" ")] + fails = int(raw_input("# of Fails? ").strip()) + must = int(raw_input("Spys must fail? ").strip()) == 1 + round = int(raw_input("Round? ").strip()) + game.do_mission(team, fails, must, round) + game.trace = [] + continue + + elif command == "lady" or command == "lol": + p1 = int(raw_input("ID For Lady? ").strip()) + p2 = int(raw_input("ID For Target? ").strip()) + claim = int(raw_input("Claim? ").strip()) == 1 + game.player_sees_player_and_claims(p1, p2, claim) + game.trace = [] + continue + + elif command == "side": + p1 = int(raw_input("ID For Assertion? ").strip()) + claim = int(raw_input("Good? ").strip()) == 1 + game.add_known_alliance(p1, claim) + game.trace = [] + continue + + elif command == "eval": + times = 200 / (game.n_players - 4) * 2 + if len(command_list) > 1: + times = int(command_list[1]) + game.eval(times) + elif command == "report": + repl_report(game.report(), namemap, game.n_good) + elif command == "name": + if len(command_list) < 3: + puts(colored.red("No args?")) + continue + namemap[int(command_list[1])] = command_list[2] + elif command == "disb" or command == "disbelieve": + if len(command_list) < 2: + puts(colored.red("No args?")) + continue + game.disbelieve(int(command_list[1])) + else: + puts(colored.red("Unknown command: %s" % command)) + continue + + + + + + +if __name__ == '__main__': + main() base_game = DeceptionGame(ResistanceGame(5)) #base_game.add_known_role(0, "G1") #base_game.add_known_alliance(1, False)