new ui tools, prepare to save

This commit is contained in:
Barak Michener 2013-07-30 15:30:12 -04:00
parent d0794949ad
commit 32c040f2c2
2 changed files with 68 additions and 25 deletions

View file

@ -1,4 +1,4 @@
argparse==1.2.1 argparse==1.2.1
clint==0.3.1 colorama==0.2.5
progressbar==2.3 progressbar==2.3
wsgiref==0.1.2 wsgiref==0.1.2

87
tim.py
View file

@ -5,7 +5,8 @@ import progressbar
import readline import readline
import random import random
import sys import sys
from clint.textui import colored, puts import colorama
from colorama import Fore, Style
def ResistanceGame(n_players): def ResistanceGame(n_players):
@ -15,6 +16,13 @@ def ResistanceGame(n_players):
return full_set[:n_players] return full_set[:n_players]
def AvalonGame(n_players):
full_set = [("Merlin", True), ("G2", True), ("G3", True),
("E1", False), ("E2", False), ("G4", True),
("E3", False), ("G5", True), ("G6", True), ("E4", False)]
return full_set[:n_players]
class Bernoulli(object): class Bernoulli(object):
def __init__(self, percentage): def __init__(self, percentage):
self.percentage = percentage self.percentage = percentage
@ -66,8 +74,11 @@ class DeceptionGame(object):
return None return None
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append( self.seen.append({"type": "known_side",
["Known role ", player_id, "Good" if is_good else "Evil"]) "player": player_id,
"is good": is_good,
"print_order": ["player",
"is good"]})
self.tid += 1 self.tid += 1
def add_known_role(self, player_id, role_str): def add_known_role(self, player_id, role_str):
@ -80,7 +91,11 @@ class DeceptionGame(object):
return None return None
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append(["Known role ", player_id, role_str]) self.seen.append({"type": "known_role",
"player": player_id,
"role": role_str,
"print_order": ["player",
"role"]})
self.tid += 1 self.tid += 1
def player_sees_player_and_claims(self, p1, p2, claim): def player_sees_player_and_claims(self, p1, p2, claim):
@ -107,6 +122,13 @@ class DeceptionGame(object):
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append( self.seen.append(
["Lady:", p1, "says", p2, "is", "Good" if claim else "Evil"]) ["Lady:", p1, "says", p2, "is", "Good" if claim else "Evil"])
self.seen.append({"type": "lady",
"p1": p1,
"p2": p2,
"is good": claim,
"print_order": ["p1",
"p2",
"is good"]})
self.tid += 1 self.tid += 1
def do_mission(self, team, fails, must_fail, r): def do_mission(self, team, fails, must_fail, r):
@ -137,8 +159,15 @@ class DeceptionGame(object):
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append( self.seen.append({"type": "mission",
["Mission:"] + team + ["with %d fails on round %d" % (fails, r)]) "team": team,
"fails": fails,
"round": r,
"must fail": must_fail,
"print_order": ["team",
"fails",
"must fail",
"round"]})
self.tid += 1 self.tid += 1
def do_vote(self, team, votes, r): def do_vote(self, team, votes, r):
@ -151,10 +180,10 @@ class DeceptionGame(object):
n_spies = len(team) - n_actually_good_people n_spies = len(team) - n_actually_good_people
could_happen = True could_happen = True
for player, vote in enumerate(votes): for player, vote in enumerate(votes):
if self.player_is_good(deal, player):
if player in team: if player in team:
continue continue
elif n_spies > 0: elif self.player_is_good(deal, player):
if n_spies > 0:
if vote == 1: if vote == 1:
if self.ignorance_on_round[rnd].rand(): if self.ignorance_on_round[rnd].rand():
continue continue
@ -167,9 +196,7 @@ class DeceptionGame(object):
else: else:
return False return False
else: else:
if player in team: if n_spies == 0:
continue
elif n_spies == 0:
if vote == 1: if vote == 1:
if self.ignorance_on_round[rnd].rand(): if self.ignorance_on_round[rnd].rand():
continue continue
@ -185,7 +212,11 @@ class DeceptionGame(object):
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append(["Vote:"] + team + ["voted %s round %d" % (votes, r)]) self.seen.append({"type": "vote",
"team": team,
"votes": votes,
"round": r,
"print_order": ["team", "votes", "round"]})
self.tid += 1 self.tid += 1
def eval(self, length=10): def eval(self, length=10):
@ -287,14 +318,28 @@ def repl_report(report, namemap, ngood):
goodness * 100, goodness * 100,
(1.0 - goodness) * 100) (1.0 - goodness) * 100)
if still_good < ngood: if still_good < ngood:
puts(colored.cyan(row)) print(Fore.CYAN + Style.BRIGHT + row)
else: else:
puts(colored.red(row)) print(Fore.RED + Style.BRIGHT + row)
still_good += 1 still_good += 1
def display_statement(statement, namemap):
out = ""
out += Fore.MAGENTA + Style.BRIGHT
out += statement["type"].title()
out += Style.RESET_ALL + " -- "
for key in statement["print_order"]:
out += Fore.YELLOW
out += key.title() + ": "
out += Style.RESET_ALL
out += str(statement[key]).title() + " "
return out
def main(): def main():
puts(colored.green("Welcome to Tim the Enchanter v1.0")) colorama.init(autoreset=True)
print(Fore.GREEN + Style.BRIGHT + "Welcome to Tim the Enchanter v1.0")
game = None game = None
namemap = {} namemap = {}
@ -308,7 +353,7 @@ def main():
sys.exit(0) sys.exit(0)
if (command != "newgame" and command != "testgame") \ if (command != "newgame" and command != "testgame") \
and game is None: and game is None:
puts(colored.red("Need to create a game")) print(Fore.RED + "Need to create a game")
continue continue
elif command == "newgame": elif command == "newgame":
nplayers = raw_input("How many players? ") nplayers = raw_input("How many players? ")
@ -329,9 +374,7 @@ def main():
continue continue
elif command == "ls": elif command == "ls":
for i, statement in enumerate(game.seen): for i, statement in enumerate(game.seen):
name = " ".join( print "%d: %s" % (i, display_statement(statement, namemap))
[namemap.get(x, str(x)) for x in statement])
print "%d: %s" % (i, name)
continue continue
elif command == "vote": elif command == "vote":
input = raw_input("Team? ").strip() input = raw_input("Team? ").strip()
@ -375,16 +418,16 @@ def main():
repl_report(game.report(), namemap, game.n_good) repl_report(game.report(), namemap, game.n_good)
elif command == "name": elif command == "name":
if len(command_list) < 3: if len(command_list) < 3:
puts(colored.red("No args?")) print(Fore.RED + "No args?")
continue continue
namemap[int(command_list[1])] = command_list[2] namemap[int(command_list[1])] = command_list[2]
elif command == "disb" or command == "disbelieve": elif command == "disb" or command == "disbelieve":
if len(command_list) < 2: if len(command_list) < 2:
puts(colored.red("No args?")) print(Fore.RED + "No args?")
continue continue
game.disbelieve(int(command_list[1])) game.disbelieve(int(command_list[1]))
else: else:
puts(colored.red("Unknown command: %s" % command)) print(Fore.RED + "Unknown command: %s" % command)
continue continue
except Exception: except Exception:
print "\n" print "\n"