forked from jedisabito/coup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoup.py
More file actions
239 lines (184 loc) · 9.6 KB
/
coup.py
File metadata and controls
239 lines (184 loc) · 9.6 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#Author: Joe DiSabito
#Description: Allows automation of Coup game without cards. Requires moderator
#to operate. Python is weird about dictionaries (no list elements allowed),
#which made the Ambassador ability very difficult. Would have liked
#each person's hand to be a list of cards, but that was not possible.
#Anyways, enjoy!
#!/usr/bin/python
import sys, os, random, time
class Player(object):
def __init__(self, card1, card2):
self.coins = 2
self.cards = [card1, card2]
def aboutMe(self):
pCards = ""
for card in self.cards:
pCards += card + " "
return "Coins: " + str(self.coins) + "\nCards: " + pCards
def draw(self, newCard):
self.cards.append(newCard)
def lookAtHand(self):
for card in self.cards:
print card
class Deck(object):
def __init__(self):
self.cards = self.cards = ['Contessa', 'Contessa', 'Contessa', 'Duke', 'Duke', 'Duke', 'Captain', 'Captain', 'Captain', 'Assassin', 'Assassin', 'Assassin', 'Ambassador' ,'Ambassador' ,'Ambassador']
self.numCards = 15
def shuffle(self):
random.seed()
random.shuffle(self.cards)
def deal(self):
self.numCards -= 1
print "Dealing Card: numCards = ", self.numCards
return self.cards.pop()
def fanUp(self):
for i, card in enumerate(self.cards):
print i, " ", card
def addCard(self, card):
self.cards.append(card)
class CoupGame(object):
def __init__(self):
#self.cards = ['Contessa', 'Contessa', 'Contessa', 'Duke', 'Duke', 'Duke', 'Captain', 'Captain', 'Captain', 'Assassin', 'Assassin', 'Assassin', 'Ambassador' ,'Ambassador' ,'Ambassador']
self.deck = Deck()
#self.numCards = 15
self.destroyedCards = []
self.players = {}
self.numPlayers = input("Number of players (2-6): ")
if self.numPlayers >= 2 and self.numPlayers <= 6:
#coins dispersed
self.treasury = 50 - 2 * self.numPlayers #50 is starting amt
#deck shuffled
self.deck.shuffle()
#cards dealt
for i in range(self.numPlayers):
name = raw_input("Player name: ")
newPlayer = Player(self.deck.deal(), self.deck.deal())
#players.append((name,cards[numCards - 1], cards[numCards - 2], 2))
self.players[name] = newPlayer
print self.players[name].aboutMe()
#initialized deck of cards, numCards used as an "index pointer", tells us
#where the last card of the deck is (numCards - 1)
#after cards are lost, they are added to this list
#starting treasury
#will become a dictionary with each player's name as keys, each entry
#provides information on that player (cards held, number of coins)
print "Welcome to COUP!\n"
cg = CoupGame()
#waits for moderator's input, acts accordingly
response = 's'
while response != 'q':
response = raw_input("(s)tatus\ncoun(t)s\n(e)xchange\ns(h)uffle\n(c)oins\n(d)estroy\nta(x)\nstea(l)\n(i)ncome\n(f)oreign aid\n(q)uit:")
#Prints out raw statistics of the current game
if response == 's':
for player in cg.players.iterkeys():
print player, cg.players[player].aboutMe()
cg.deck.fanUp() #Print all cards in deck
print "Cards in deck:", cg.deck.numCards
print "Treasury: ", cg.treasury
#Allows for Ambassador ability
elif response == 'e':
name = raw_input("Player name: ")
print cg.players[name].aboutMe()
print "2 cards dealt to", name
#Same line twice, not sure if its worth consolidating
cg.players[name].draw(cg.deck.deal())
cg.players[name].draw(cg.deck.deal())
cg.players[name].lookAtHand()
#print cards[numCards - 1], cards[numCards - 2]
#This will enforce handsizes. This is the only time someone's
#hand should be bigger than 2
to_deck = raw_input("which card will you discard first? (0-3): ")
cg.deck.addCard(cg.players[name].cards[int(to_deck)])
cg.players[name].cards.remove(cg.players[name].cards[int(to_deck)])
cg.players[name].lookAtHand()
to_deck = raw_input("which card will you discard second? (0-2): ")
cg.deck.addCard(cg.players[name].cards[int(to_deck)])
cg.players[name].cards.remove(cg.players[name].cards[int(to_deck)])
# if not (players[name][1] == "GONE" or players[name][2] == "GONE"):
# exchange = input("Exchange 0 (neither)\n" +
# "Exchange 1 (first with first)\n" +
# "Exchange 2 (first with second)\n" +
# "Exchange 3 (second with first)\n" +
# "Exchange 4 (second with second)\n" +
# "Exchange 5 (both):")
# if exchange == 5:
# temp = [players[name][1], players[name][2]]
# players[name][0] = cards[numCards - 1]
# players[name][1] = cards[numCards - 2]
# cards[numCards - 1] = temp[0]
# cards[numCards - 2] = temp[1]
# elif exchange == 4:
# temp = players[name][1]
# players[name][1] = cards[numCards - 2]
# cards[numCards - 2] = temp
# elif exchange == 3:
# temp = players[name][1]
# players[name][1] = cards[numCards - 1]
# cards[numCards - 1] = temp
# elif exchange == 2:
# temp = players[name][0]
# players[name][0] = cards[numCards - 2]
# cards[numCards - 2] = temp
# elif exchange == 1:
# temp = players[name][0]
# players[name][0] = cards[numCards - 1]
# cards[numCards - 1] = temp
# else:
# exchange = input("Exchange 0 (neither)\n" +
# "Exchange 1 (with first)\n" +
# "Exchange 2 (with second):")
# card = 0
# temp = players[name][card]
# if temp == "GONE":
# card = card + 1
# temp = players[name][card]
# if exchange == 2:
# players[name][card] = cards[numCards - 2]
# cards[numCards - 2] = temp
# elif exchange == 1:
# players[name][card] = cards[numCards - 1]
# cards[numCards - 1] = temp
cg.deck.shuffle()
#Coin counts (without held cards)
elif response == 't':
for player in cg.players.iterkeys():
print player, ":", cg.players[player].coins
print "Treasury:", treasury
#Shuffle option
elif response == 'h':
cg.deck.shuffle()
#Adjust coin amount of one player
elif response == 'c':
name = raw_input("Player name:")
print name, "current coins:", cg.players[name].coins
newVal = input("New coin count:")
cg.players[name].coins = newVal
#Duke ability - Tax
elif response == 'x':
name = raw_input("Player name:")
cg.players[name].coins += 3
cg.treasury -= 3
#Income
elif response == 'i':
name = raw_input("Player name:")
cg.players[name].coins += 1
cg.treasury -= 1
#Foreign Aid
elif response == 'f':
name = raw_input("Player name:")
cg.players[name].coins += 2
cg.treasury -= 2
#Captain Ability - Steal
elif response == 'l':
name1 = raw_input("Captain claimer:")
name2 = raw_input("Target:")
cg.players[name1].coins += 2
cg.players[name2].coins -= 2
#Destruction of a card (Coup or Assassination or failed bluff/challenge)
elif response == 'd':
name = raw_input("Player name:")
print cg.players[name].aboutMe()
remove = input("Which to remove(0-1):")
cg.destroyedCards.append(cg.players[name].cards[int(remove)])
cg.players[name].cards.remove(cg.players[name].cards[int(remove)])
print cg.players[name].aboutMe()