Repository navigation
Expand file tree
/
Copy pathoh_hell.py
More file actions
254 lines (206 loc) · 7.66 KB
/
Copy pathoh_hell.py
File metadata and controls
254 lines (206 loc) · 7.66 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import random
from itertools import islice, cycle, compress
import csv
suit_names = ["Spades", "Clubs", "Hearts", "Diamonds"]
card_vals = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
class randAI:
def __init__(self, *args, **kwargs):
self.name = "randAI"
def pick_bid(self, player, trump_card):
return
def pick_card(self, player, trump_card):
return
class trickAI(randAI):
def __init__(self, *args, **kwargs):
self.name = "trickAI"
class countAI(randAI):
def __init__(self, *args, **kwargs):
self.name = "countAI"
def cardEval(cardList, trumpCard):
trumpList = [0]*len(cardList)
followSuitList = [0]*len(cardList)
hasTrump = False
for i, card in enumerate(cardList):
if (card.suit == trumpCard.suit):
hasTrump = True
trumpList[i] = 1
if (card.suit == cardList[0].suit):
followSuitList[i] = 1
if(hasTrump):
evalList = list(compress(cardList, trumpList))
else:
evalList = list(compress(cardList, followSuitList))
if (not len(evalList)): #if there's only one card left, return it
return evalList[0]
tempCard = evalList[0] #find highest value card among remaining
for card in evalList[1:]:
if (card.val > tempCard.val):
tempCard = card
return tempCard
class Card:
def __init__(self, val, suit):
self.suit = suit
self.val = val
def getName(self):
return card_vals[self.val] + " of " + suit_names[self.suit]
def getNameSh(self):
if self.val < 9: #Not a face card
return str(self.val + 2) + suit_names[self.suit][0]
else:
return card_vals[self.val][0] + suit_names[self.suit][0]
def isLegal(self, lead, trump):
return (self.suit == lead.suit) or (self.suit == trump.suit)
class Deck:
def __init__(self):
self.order = [Card(val, suit) for val in range(13) for suit in range(4)]
def shuffle(self):
random.shuffle(self.order)
def printOrder(self):
for it in range(52):
print(self.order[it].getName())
''' # Needs to be re-made for non-full deck, do if needed...
def printOrderSh(self):
print("----")
for it1 in range(4):
line = ""
for it2 in range(13):
line += self.order[it1*13 + it2].getNameSh()
line += " "
print(line)
print("----")
'''
class Player:
def __init__(self, *args, **kwargs):
if (len(args) == 0):
self.name = "Player"
else:
self.name = args[0]
self.gameScore = 0
self.trickScore = 0
self.bid = 0
self.hand = []
def printPlayerInfo(self):
print('Info for player %s' % self.name)
print('Score: %d' % self.gameScore)
print('Hand contents:')
for card in self.hand:
print(card.getName())
class Game:
def __init__(self, pNames, nHands, hookBool, verbose):
self.maxHands = nHands
self.hook = hookBool
self.deck = Deck()
self.deck.shuffle()
self.verbose = verbose
self.players = []
for name in pNames:
self.players.append(Player(name))
def printGameInfo(self):
verboseprint = print if self.verbose else lambda *a, **k: None
verboseprint('Game Info: ')
#verboseprint('Number of players: %d' % len(self.playerNames))
#verboseprint('Players:')
#for pName in self.playerNames:
# verboseprint(pName)
verboseprint('Maximum starting cards: %d' % self.maxHands)
#verboseprint('Maximum possible starting cards: %d' % int(52 / len(self.playerNames)))
verboseprint('Player Scores: ')
for player in self.players:
verboseprint('%s: %d' % (player.name, player.gameScore))
#verboseprint('Deck contents: ')
#self.deck.printOrderSh()
def play(self):
verboseprint = print if self.verbose else lambda *a, **k: None
handNums = list(range(self.maxHands, 0 , -1)) + list(range(2, self.maxHands + 1))
dealers = list(islice(cycle(self.players), 0, len(handNums))) # make list of all dealers for all hands
for handIndex, handLength in enumerate(handNums): #hand loop; handIndex is the nth hand and handLength is the tricks in the hand
self.deck = Deck() #re-fill deck
self.deck.shuffle() #shuffle
for i in range(handLength): #deal cards
for j, player in enumerate(self.players):
if (i == 0): #reset bids and trick scores
self.players[j].bid = 0
self.players[j].trickScore = 0
self.players[j].hand.append(self.deck.order.pop())
trumpCard = self.deck.order.pop() #turn trump card face-up
#bids = [0] * len(self.players) # make bids TODO: make this controlled by a changeable decision function
sumbids = 0
for player in self.players:
player.bid = random.randint(0, 2) # just give a random bid for now TODO: make generalized
sumbids += player.bid
if (sumbids == handLength and self.hook): #enforce the hook, this should be handled in the method above eventually
self.players[-1].bid += 1
dealer = dealers[handIndex]
dealerIndex = self.players.index(dealer)
trickOrder = list(islice(cycle(self.players), dealerIndex, dealerIndex + len(self.players))) #set up the initial trick order
for trick in range(handLength): #loop through all tricks in a hand
verboseprint("Trick %d:" % (trick + 1))
playedCards = []
for player in trickOrder:
#Play lead card
if player == trickOrder[0]:
playedCards.append(player.hand.pop(random.randint(0, len(player.hand) - 1)))
else:
# must play only legal cards
# play the first legal card
played = False
for card in player.hand:
if card.isLegal(playedCards[0], trumpCard):
playedCards.append(card)
player.hand.remove(card)
played = True
break
if(not played): #if there's no legal card, play a random card
playedCards.append(player.hand.pop(random.randrange(len(player.hand))))
verboseprint("{} played {}".format(player.name, playedCards[-1].getName()))
# evaluate winner, increment trick score
wonCard = cardEval(playedCards, trumpCard)
wonPlayer = trickOrder[playedCards.index(wonCard)]
verboseprint("{} won the trick with the {}".format(wonPlayer.name, wonCard.getName()))
wonPlayer.trickScore += 1
#re-make the trick order based on winner of this trick
playersIndex = self.players.index(wonPlayer)
trickOrder = list(islice(cycle(self.players), playersIndex, playersIndex + len(self.players)))
#evaluate bids and increment game scores
verboseprint("Evaluating bids for hand {}:".format(handIndex))
for player in self.players:
if player.trickScore == player.bid:
player.gameScore += 10 + player.trickScore
verboseprint("{} took {} tricks and bid {}. +{} points".format(player.name, player.trickScore, player.bid, 10 + player.trickScore))
else:
player.gameScore += player.trickScore
verboseprint("{} took {} tricks and bid {}. +{} points".format(player.name, player.trickScore, player.bid, player.trickScore))
verboseprint("Final Scores:")
for player in self.players:
verboseprint("{} - {}".format(player.name, player.gameScore))
def runTest(numGames, pNames, nHands, hook, verbose):
verboseprint = print if verbose else lambda *a, **k: None
with open('gameData.csv', mode='w', newline='') as dataFile:
dataWriter = csv.writer(dataFile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
titleRow = ['Game #'] + pNames
dataWriter.writerow(titleRow)
for i in range(numGames):
if (i%100 == 0):
print('Processing trial {}...'.format(i))
verboseprint('Processing trial {}...'.format(i))
g = Game(pNames, nHands, hook, verbose)
g.play()
g.printGameInfo()
dataRow = [i]
for player in g.players:
dataRow.append(player.gameScore)
dataWriter.writerow(dataRow)
'''
c = Card(8,1)
c2 = Card(11,3)
d = Deck()
p = Player("testPlayer")
p.hand = [c, c2]
p.printPlayerInfo()
'''
pN = ['STR', 'ETR', 'Mom', '3P', 'PJ', 'CJ']
#g = Game(pN, 7, True)
#g.printGameInfo()
#g.play()
#g.printGameInfo()
runTest(10000, pN, 7, True, False)