-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
143 lines (139 loc) · 4.87 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Copyright 2010 Vincent Verhoeven
#
# This file is part of bra-ket-wolf.
#
# bra-ket-wolf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# bra-ket-wolf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bra-ket-wolf. If not, see <http://www.gnu.org/licenses/>.
try:
import cPickle as pickle
except:
import pickle
from cmd import Cmd
from multiverse import Multiverse
class Main(Cmd):
def __init__(self):
Cmd.__init__(self)
self.players = []
self.roles = []
self.keep = 1.0
self.game = None
def update_roles(self):
seer = 1
wolf = len(self.players) / 3
villager = len(self.players) - wolf - seer
self.roles = [("Seer",seer),("Wolf",wolf),("Villager",villager)]
def do_start(self,s):
"""Start a new game! Requires the players and roles to be set."""
if self.players == []:
print "Unable to do start because players is empty"
return
if self.roles == []:
print "Unable to do start because roles is empty"
return
self.game = Multiverse(self.players,self.roles,self.keep)
def do_players(self,s):
"""Enter a comma-separated list of players. This will also reset
the roles list to a sensible default."""
if s == "":
print self.players
else:
players = s.split(",")
self.players = players
self.update_roles()
def do_roles(self,s):
"""Enter the roles to be used, in a format like
roles Villager 3, Wolf 2, Seer 1"""
if s == "":
print self.roles
else:
roles = [role.strip().split(" ") for role in s.split(",")]
roles = [(role,int(count)) for [role,count] in roles]
self.roles = roles
def do_keepfraction(self,s):
"""Specify which fraction of the generated universes to keep. Defaults to 1.0."""
if s == "":
print self.keep
else:
self.keep = float(s)
def do_state(self,s):
"""Returns the current state of the game."""
print self.game
def do_table(self,s):
"""Returns an overview table of the distribution of good and evil."""
print self.game.getGoodEvilDeadTable(False)
def do_namedtable(self,s):
"""Returns an overview table of the distribution of good and evil, with player names."""
print self.game.getGoodEvilDeadTable(True)
def do_next(self,s):
"""Starts the next phase of the game."""
self.game.nextPhase()
print "It is now %s%s" % self.game.time
def do_kill(self,s):
"""kill <player>: kills a player, fixing their role."""
if self.game is None:
print "Unable to do kill because game is None"
return
if s in self.players:
self.game.killPlayer(s)
else:
print "Player {0:s} not found, did you mean 'attack {0:s}'?".format(s)
def do_attack(self,s):
"""attack <player> <target>: wolf attack during night."""
if self.game is None:
print "Unable to do attack because game is None"
return
(player,target) = s.split(" ")
if player in self.players and target in self.players:
self.game.wolfAttack(player,target)
else:
print "Error: player {0:s} or {1:s} not found!".format(player,target)
def do_see(self,s):
"""see <player> <target>: seer target during night.
Note: these are executed immediately, so according to the rules
you should input all the wolf attacks first."""
if self.game is None:
print "Unable to do see because game is None"
return
(player,target) = s.split(" ")
if player in self.players and target in self.players:
result = self.game.seerAlignmentVision(player,target)
print result
else:
print "Error: player {0:s} or {1:s} not found!".format(player,target)
def do_save(self,s):
"""Saves the game, by default to 'current.bra-ket-wolf' but you
can give another file name as parameter."""
filename = s
if filename == "":
filename = "current.bra-ket-wolf"
f = open(filename,"w")
pickle.dump(self.game,f)
f.close()
def do_load(self,s):
"""Loads the game, by default from 'current.bra-ket-wolf' but you
can give another file name as parameter.
This uses pickle so standard security warning apply, do not open untrusted files."""
filename = s
if filename == "":
filename = "current.bra-ket-wolf"
f = open(filename,"r")
self.game = pickle.load(f)
self.players = self.game.players
self.roles = self.game.rolelist
f.close()
def do_exit(self,s):
"""Exits the program."""
return True
if __name__ == "__main__":
m = Main()
m.cmdloop()