-
Notifications
You must be signed in to change notification settings - Fork 0
/
spoons.py
234 lines (150 loc) · 4.87 KB
/
spoons.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
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
from random import *
import copy
#Define a deck of cards
class Deck:
def __init__(self, deckSizeI = 52, cardRangeI = 13, cardCountI = 4):
self.cards = []
self.deckSize = deckSizeI
self.cardRange = cardRangeI
self.cardCount = cardCountI
cardsUsed = []
for i in range(0, self.cardRange):
cardsUsed.append(0)
i = 0
self.cards = [-1] * self.deckSize
while i < self.deckSize:
card = randint(0, self.cardRange - 1)
if(cardsUsed[card] < self.cardCount):
self.cards[i] = card
i += 1
cardsUsed[card] += 1
return None
def shuffle(self):
cardsUsed = []
for i in range(0, self.cardRange):
cardsUsed.append(0)
i = 0
while i < self.deckSize:
card = randint(0, self.cardRange - 1)
if(cardsUsed[card] < self.cardCount):
self.cards[i] = card
i += 1
cardsUsed[card] += 1
def draw(self):
self.deckSize -= 1
return self.cards.pop(0)
def discard(self, card):
self.deckSize += 1
self.cards.append(card)
return
def deal(self, cardCount, hands):
for i in range(0, cardCount):
for hand in hands:
hand.add(self.draw())
#Define a hand of cards
class Hand:
def __init__(self, cards = []):
self.cards = copy.copy(cards)
self.handSize = 0
return None
def add(self, card):
self.handSize += 1
self.cards.append(card)
def remove(self, cardNum):
self.handSize -= 1
return self.cards.pop(cardNum)
def sort(self):
self.cards.sort()
def getCards(self):
return self.cards
def getDistrib(self):
distrib = Distrib(self.cards)
return distrib
def index(self, card):
return self.cards.index(card)
def getSize(self):
return self.handSize
#Define an object that can be used to perform statistical analysis on a hand of cards
class Distrib:
def __init__(self, cards):
self.cards = []
self.count = []
for card in cards:
self.append(card)
return None
def append(self, card):
if card in self.cards:
self.count[self.cards.index(card)] += 1
else:
self.cards.append(card)
self.count.append(1)
def getCount(self, card):
return self.count[self.cards.index(card)]
def getMax(self):
return self.cards[self.count.index(max(self.count))]
def getMin(self):
return self.cards[self.count.index(min(self.count))]
#Define the rules and process for a game of spoons
class SpoonsGame:
def __init__(self, playersI = 2, handSizeI = 4):
self.hands = []
self.players = playersI
self.handSize = handSizeI
self.roundCount = 0
self.deckSize = 52
self.handSize = 4
self.roundCount = 0
self.deck = Deck(self.deckSize)
self.hands = [Hand() for i in range(0, self.players)]
self.deck.deal(self.handSize, self.hands)
return None
def playTurn(self, hand, card):
discard = card
if card in hand.getCards():
if hand.getDistrib().getCount(hand.getDistrib().getMax()) <= self.handSize / 2 or hand.getDistrib().getMax() == card:
tempHand = Hand(copy.copy(hand.getCards()))
while card in tempHand.getCards():
tempHand.remove(tempHand.index(card))
discard = hand.remove(hand.index(tempHand.getDistrib().getMin()))
hand.add(card)
return discard
def playRound(self):
self.roundCount += 1
nextCard = self.deck.draw()
handCards = 0
for hand in self.hands:
handCards += hand.getSize()
nextCard = self.playTurn(hand, nextCard)
if hand.getDistrib().getCount(hand.getDistrib().getMax()) == self.handSize:
return self.hands.index(hand)
self.deck.discard(nextCard)
if self.roundCount % (self.deckSize - handCards) == 0:
self.deck.shuffle()
return -1
def playGame(self):
winner = self.playRound()
while winner == -1:
winner = self.playRound()
return winner
def getRoundCount(self):
return self.roundCount
#Set simulation variables
players = int(input("Players: "))
rounds = int(input("Rounds: "))
wins = []
#Initialize win counter
for i in range(0, players):
wins.append(0)
#Run simulation
for x in range(0, rounds):
game = SpoonsGame(players)
wins[game.playGame()] += 1
#Output block
print("\n")
print("Statistics from " + str(rounds) + " rounds of spoons with " + str(players) + " players:")
print("")
print(" Player | Win Rate")
print(" -------+---------")
for p in range(0, players):
print(" " + str(p + 1).rjust(2, " ") + " | " + ("{0:." + str(len(str(rounds)) - 3) + "f}").format(((float(wins[p])/float(rounds))*100)).rjust(len(str(rounds)), " ") + "%")
print("\n")