-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrazy_eights_game.py
316 lines (272 loc) · 10.3 KB
/
crazy_eights_game.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import random, collections
import game_rules
from game_rules import Suit,Actions
import util
import copy
class Card:
def __init__(self, rank, suit):
self.suit = suit
self.rank = rank
def __repr__(self):
return '%s of %ss' % (self.rank,self.suit)
def __hash__(self):
return hash((self.suit, self.rank))
def __eq__(self, card2):
if type(card2) == str:
return False
return self.suit == card2.suit and self.rank == card2.rank
class CardPile:
def __init__(self):
self.pile = collections.Counter()
def __add__(self, other):
new = CardPile()
new.pile = copy.copy(self.pile)
new.pile += other.pile
return new
def look(self, card):
return self.pile[card]
def add_n(self, card, n):
self.pile[card] += n
def add(self, card):
self.pile[card] += 1
def remove(self, card):
strcard = str(card)
self.pile[card] -= 1
assert self.pile[card] >= 0, 'card to remove was not in pile'
if self.pile[card] <= 0:
del self.pile[card]
def takeRandomly(self):
card = random.choice(self.pile.keys())
self.remove(card)
return card
def isEmpty(self):
return not bool(self.pile)
def size(self):
size = 0
for k in self.pile.keys():
size += self.pile[k]
return size
class GameState:
'''
GameState for Crazy Eights Card Game
'''
def __init__(self, startingPlayer,
numStartingCards,
suits,
ranks,
multiplicity,
numPlayers
):
self.numPlayers = numPlayers
self.numStartingCards = numStartingCards
self.suits = suits
self.ranks = ranks
self.multiplicity = multiplicity
self.deck = self.initializeDeck(suits, ranks, multiplicity)
self.hands = self.initializeHands()
self.player = startingPlayer
self.cardOnTable = self.initializeCardOnTable()
self.numsTaken = [0,] * self.numPlayers
def initializeDeck(self, suits, ranks, multiplicity):
deck = CardPile()
for suit in suits:
for rank in ranks:
card = Card(rank, suit)
deck.add_n(card, multiplicity)
return deck
def initializeHands(self):
hands = []
for p in range(self.numPlayers):
hand = CardPile()
for i in range(self.numStartingCards):
card = self.deck.takeRandomly()
hand.add(card)
hands.append(hand)
return hands
def initializeCardOnTable(self):
card = self.deck.takeRandomly()
return card
def isEnd(self):
for hand in self.hands:
if hand.isEmpty():
return True
return False
def getLegalActions(self):
"""
:param gameState: current game state
:return: a list of actions that can be taken from the given game state
each action will be in the form (action, cards) where:
action - in {'take','play','pass'}
cards - list of cards the player will play, in the correct order
"""
cardOnTable = self.cardOnTable
hand = self.hands[self.player]
numsTaken = self.numsTaken[self.player]
deckSize = self.deck.size()
return getLegalActions(cardOnTable, hand, numsTaken, deckSize)
def getSuccessor(self, newAction):
newGameState = copy.deepcopy(self)
action, cards = newAction
if action == Actions.TAKE:
newGameState.numsTaken[self.player] += 1
card = newGameState.deck.takeRandomly()
newGameState.hands[self.player].add(card)
if newGameState.deck.isEmpty():
newGameState.deck = self.reshuffleDeck()
newGameState.deck.remove(card)
else:
newGameState.numsTaken[self.player] = 0
if action == Actions.PLAY:
for card in cards:
newGameState.hands[self.player].remove(card)
newGameState.cardOnTable = cards[-1]
if newGameState.deck.isEmpty():
newGameState.deck = self.reshuffleDeck()
newGameState.player = (newGameState.player + 1) % self.numPlayers
return newGameState
def reshuffleDeck(self):
deck = self.initializeDeck(self.suits, self.ranks, self.multiplicity)
for hand in self.hands:
for card in hand.pile:
for i in range(hand.look(card)):
deck.remove(card)
deck.remove(self.cardOnTable)
return deck
def Utility(self):
for i,hand in self.hands:
if hand.isEmpty():
return float('inf') if i == 0 else -float('inf')
class Observation:
'''
Observation will serve as a 'fake' version of GameState, with the same
functions, but without the real values of the deck and opponents hand
Objects of this class will be used by agents when deciding on the best
action to make
'''
def __init__(self, agentIndex, gameState):
self.observer = agentIndex
self.player = agentIndex
self.suits = gameState.suits
self.ranks = gameState.ranks
self.multiplicity = gameState.multiplicity
self.hand = gameState.hands[self.observer]
self.handsizes = [hand.size() for hand in gameState.hands]
self.cardOnTable = gameState.cardOnTable
self.numsTaken = copy.copy(gameState.numsTaken)
self.legalActions = gameState.getLegalActions()
self.deckSize = gameState.deck.size()
self.unknowns = gameState.deck
self.numPlayers = gameState.numPlayers
for i in range(len(gameState.hands)):
if i != self.observer:
self.unknowns += gameState.hands[i]
def getSuits(self):
return self.suits
def getHandSize(self):
return self.handsizes[self.player]
def getRanks(self):
return self.ranks
def getMultiplicity(self):
return self.multiplicity
def getHand(self):
return self.hand
def getUnknowns(self):
return self.unknowns
def getCardOnTable(self):
return self.cardOnTable
def getNumsTaken(self):
return self.numsTaken[self.player]
def isEmptyDeck(self):
return self.getDeckSize() == 0
def getDeckSize(self):
return self.deckSize
def getLegalActions(self):
cardOnTable = self.getCardOnTable()
hand = self.getHand() if self.player == self.observer else self.unknowns
numsTaken = self.getNumsTaken()
deckSize = self.getDeckSize()
return getLegalActions(cardOnTable, hand, numsTaken, deckSize)
def getSuccessor(self,newAction):
newObservation = copy.deepcopy(self)
action, cards = newAction
if action == Actions.TAKE:
newObservation.numsTaken[self.player] += 1
newObservation.handsizes[self.player] += 1
newObservation.deckSize -= 1
else:
newObservation.numsTaken[self.player] = 0
if action == Actions.PLAY:
for card in cards:
newObservation.handsizes[self.player] -= 1
if self.observer != self.player:
newObservation.unknowns.remove(card)
newObservation.cardOnTable = cards[-1]
newObservation.player = (newObservation.player + 1) % len(self.handsizes) # Number of players
if newObservation.isEmptyDeck():
self.reshuffleDeck()
return newObservation
def reshuffleDeck(self):
self.deckSize = len(self.getSuits())*len(self.getRanks())*self.getMultiplicity() \
- sum(handSize for handSize in self.handsizes) - 1
def isEnd(self):
for size in self.handsizes:
if size == 0:
return True
return False
def Utility(self):
for i,hand in enumerate(self.handsizes):
if hand == 0:
return float('inf') if i == self.observer else -float('inf')
return 0
def getLegalActions(cardOnTable, hand, numsTaken, deckSize):
actions = []
if numsTaken < 3 and deckSize > 0:
actions.append((Actions.TAKE, None))
for card in hand.pile.keys():
if cardOnTable.suit == card.suit or \
cardOnTable.rank == card.rank or \
card.rank == 8:
actions.append((Actions.PLAY, [card]))
if game_rules.CHAIN_RULE:
cardsWithSameRank = [c for c in hand.pile.keys()
if c.rank == card.rank and c != card]
allCombinations = util.getCombinations(cardsWithSameRank)
for c in allCombinations:
actions.append((Actions.PLAY, [card] + c))
if len(actions) == 0:
actions.append((Actions.PASS, None))
return actions
class CrazyEightsGame:
def __init__(self, numStartingCards = 6,
suits = [Suit.HEART,Suit.DIAMOND,Suit.CLUB,Suit.SPADE],
ranks = [1,2,3,4,5,6,7,8,9,10,11,12,13],
multiplicity = 1,
startingPlayer = 0,
numPlayers = 2):
"""
startingPlayer: index of player starting the game
numStartingCards: integer representing number of cards each player will
start with
suits: subset of [HEART, DIAMOND, SPADE, CLUB], representing the suits
used in the deck
ranks: subset of [1,2,3,4,5,6,7,8,9,10,11,12,13], representing the ranks
used in the deck. 8 will always be in game.
multiplicity: integer representing the number of decks used
"""
self.suits = suits
self.ranks = set(ranks)
self.ranks.add(8)
self.multiplicity = multiplicity
self.player = startingPlayer
self.numStartingCards = numStartingCards
self.startingPlayer = startingPlayer
self.numPlayers = numPlayers
def startState(self):
return GameState(self.startingPlayer,
self.numStartingCards,
self.suits,
self.ranks,
self.multiplicity,
self.numPlayers)
def Player(self, gameState):
return gameState.player