-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
341 lines (290 loc) · 13 KB
/
player.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/python
#-*- coding: utf-8 -*-
import colorama
import copy
import hanabi
import numpy as np
from bcolor import *
from card import *
from random import randint
class Player(object):
"""
The basic human player.
"""
def __init__(self, handSize):
self.handCapacity = handSize
self.hand = []
self.knownHand = []
def drawFrom(self, deck):
missingCards = self.handCapacity - len(self.hand)
while (missingCards > 0 and not deck.empty()):
self.hand.append(deck.draw())
self.knownHand.append(Card())
missingCards -= 1
def giveSuitHint(self, s):
for card in self.hand:
if card.getSuit() == s:
self.knownHand[self.hand.index(card)].setSuit(s)
def giveValueHint(self, value):
for card in self.hand:
if card.getValue() == value:
self.knownHand[self.hand.index(card)].setValue(value)
def play(self, c):
self.knownHand.remove(self.knownHand[self.hand.index(c)])
self.hand.remove(c)
hanabi.Hanabi.table.place(c)
def discard(self, c):
self.knownHand.remove(self.knownHand[self.hand.index(c)])
self.hand.remove(c)
hanabi.Hanabi.table.discard(c)
hanabi.Hanabi.table.rechargeHint()
def promptAction(self, knowledgeBase=None, nTurnsLeft=0, players=None):
""" This method prompts the player for action and carries said action out
"""
# validAction is a boolean that loops on
# the menu while the action is invalid
validAction = False
while not validAction:
validAction = True
print("--------------")
print("What do you want to do? ", end='')
answer = input()
if answer == "play":
print("which card? ", end='')
answerCardId = input() # the index of the card answered by the player
answerCard = self.hand[int(answerCardId)]
self.play(answerCard)
self.drawFrom(hanabi.Hanabi.deck)
elif answer == "hint":
print("which player? ", end='')
answerPlayer = input()
target = players[int(answerPlayer)]
if hanabi.Hanabi.table.hintsLeft() and target != self:
hanabi.Hanabi.table.useHint()
print("what type? (suit/value) ", end='')
answerHintType = input()
if answerHintType == "suit":
print("Which suit? ", end='')
answerSuit = input()
target.giveSuitHint(Suit.valueOf(answerSuit))
elif answerHintType == "value":
print("Which value? ", end='')
answerValue = input()
target.giveValueHint(int(answerValue))
else:
validAction = False
elif answer == "discard":
print("Which card? ", end='')
answerCard = input()
self.discard(self.hand[int(answerCard)])
self.drawFrom(hanabi.Hanabi.deck)
else:
validAction = False
def displayHand(self):
for card in self.hand:
print(card.toString(), end=' ')
print()
def displayKnownHand(self):
print("> ", end='')
self.displayHand() # playing with complete information at the moment
# for card in self.knownHand:
# print(card.toString(), end=' ')
# print()
def getHandCapacity(self):
return self.handCapacity
class PlayerRandom(Player):
"""
Player that play randomly.
"""
def __init__(self, handSize):
Player.__init__(self, handSize)
def promptAction(self, knowledgeBase=None, nTurnsLeft=0, players=None):
# validAction is a boolean that loops on
# the menu while the action is invalid
validAction = False
randAction = randint(0, 1)
while not validAction:
validAction = True
print("--------------")
print("Choosing random play ")
if randAction == 0:
print(colorama.Fore.CYAN + "Playing..." + Bcolor.END)
randCard = randint(0, self.handCapacity - 1)
self.play(self.hand[randCard])
self.drawFrom(hanabi.Hanabi.deck)
elif randAction == 1:
print(colorama.Fore.LIGHTMAGENTA_EX + "Discarding... " + Bcolor.END)
randDiscard = randint(0, self.handCapacity - 1)
self.discard(self.hand[randDiscard])
self.drawFrom(hanabi.Hanabi.deck)
else:
validAction = False
class PlayerRandomPlus(Player):
"""
Player that try to play playable card.
"""
def __init__(self, handSize):
Player.__init__(self, handSize)
def promptAction(self, knowledgeBase=None, nTurnsLeft=0, players=None):
nbcard = 0 # index of current card
print("--------------")
print("Trying to play... ")
for card in self.hand:
if hanabi.Hanabi.table.cardPlayable(self.hand[nbcard]):
self.play(self.hand[nbcard])
self.drawFrom(hanabi.Hanabi.deck)
print(colorama.Fore.CYAN + "Playing card: " + str(nbcard + 1) + Bcolor.END)
return # finish if played a card
print(colorama.Fore.CYAN + "Could not play card: " + str(nbcard + 1) + Bcolor.END)
nbcard += 1
randDiscard = randint(0, self.handCapacity - 1)
print(colorama.Fore.LIGHTMAGENTA_EX + "Discarding random card: " + str(randDiscard + 1) + Bcolor.END)
self.discard(self.hand[randDiscard])
self.drawFrom(hanabi.Hanabi.deck)
class PlayerRandomPlusPlus(Player):
"""
Player that try to play playable card and dicard correctly.
"""
def __init__(self, handSize):
Player.__init__(self, handSize)
def promptAction(self, knowledgeBase=None, nTurnsLeft=0, players=None):
nbcard = 0 # index of current card
# print("--------------")
# print("Trying to play... ")
for card in self.hand:
if knowledgeBase is not None:
example = list(hanabi.Hanabi.table.field)
example.append(Suit.toInt(self.hand[nbcard].getSuit()))
example.append(self.hand[nbcard].getValue())
if hanabi.Hanabi.table.cardPlayable(self.hand[nbcard]):
if knowledgeBase is not None:
knowledgeBase.append((example, [1]))
self.play(self.hand[nbcard])
self.drawFrom(hanabi.Hanabi.deck)
# print(colorama.Fore.CYAN + "Playing card: " + str(nbcard + 1) + Bcolor.END)
return # finish if played a card
else:
if knowledgeBase is not None:
knowledgeBase.append((example, [0]))
# print(colorama.Fore.CYAN + "Could not play card: " + str(nbcard + 1) + Bcolor.END)
nbcard += 1
nbcard = 0
for card in self.hand:
if hanabi.Hanabi.table.cardDiscardable(self.hand[nbcard]):
# print(colorama.Fore.LIGHTMAGENTA_EX + "Discarding card: " + str(nbcard + 1) + Bcolor.END)
self.discard(self.hand[nbcard])
self.drawFrom(hanabi.Hanabi.deck)
return # finish if discarded a card
# print(colorama.Fore.LIGHTMAGENTA_EX + "Could not discard card: " + str(nbcard + 1) + Bcolor.END)
nbcard += 1
else: # discard a random card if cant find a discardable card
randDiscard = randint(0, self.handCapacity - 1)
# print(colorama.Fore.LIGHTMAGENTA_EX + "Could not find discardable card, random card: " + str(randDiscard) + Bcolor.END)
self.discard(self.hand[randDiscard])
self.drawFrom(hanabi.Hanabi.deck)
class PlayerNet(Player):
"""
Player that play with the help of the neural network.
"""
def __init__(self, handSize, neuralNet=None, model=None):
Player.__init__(self, handSize)
self.net = neuralNet
self.log = []
self.drawFrom(hanabi.Hanabi.deck)
self.model = model
def promptAction(self, knowledgeBase=None, nTurnsLeft=5):
states = []
for i in range(len(self.hand)):
states.append(State(hanabi.Hanabi.table.field, hanabi.Hanabi.table.discarded, len(hanabi.Hanabi.deck), hanabi.Hanabi.table.strikes, self.hand, nTurnsLeft))
states[-1].play(i)
states[-1].hand.append(copy.deepcopy(hanabi.Hanabi.deck[0]))
for i in range(len(self.hand)):
states.append(State(hanabi.Hanabi.table.field, hanabi.Hanabi.table.discarded, len(hanabi.Hanabi.deck), hanabi.Hanabi.table.strikes, self.hand, nTurnsLeft))
states[-1].discard(i)
states[-1].hand.append(copy.deepcopy(hanabi.Hanabi.deck[0]))
stateValues = []
for s in states:
if self.model is None:
stateValues.append(self.net.compute(s.toInputs()))
else:
v = self.model.predict(np.array(s.toInputs()).reshape(-1, 93))
stateValues.append(v[0][0])
if all(x == stateValues[0] for x in stateValues):
indexOfBestState = stateValues.index(random.choice(stateValues))
else:
indexOfBestState = stateValues.index(max(stateValues))
if int(indexOfBestState / len(self.hand)) == 0:
self.play(self.hand[indexOfBestState % len(self.hand)])
else:
self.discard(self.hand[indexOfBestState % len(self.hand)])
self.drawFrom(hanabi.Hanabi.deck)
self.log.append(states[indexOfBestState]) # logging the states to learn later
class State():
"""
Copy the state of the game to try all the moves so we don't touch the real game.
"""
def __init__(self, field, graveyard, cardsLeft, strikes, hand, nTurnsLeft):
self.field = copy.deepcopy(field)
self.graveyard = copy.deepcopy(graveyard)
self.cardsLeft = copy.deepcopy(cardsLeft)
self.strikes = strikes
self.hand = copy.deepcopy(hand)
self.nTurns = nTurnsLeft
def play(self, i):
c = self.hand[i]
if self.field[Suit.toInt(c.getSuit()) - 1] == c.getValue() - 1:
self.field[Suit.toInt(c.getSuit()) - 1] += 1
del self.hand[i]
else:
self.strikes -= 1
self.discard(i)
def discard(self, i):
self.graveyard.append(self.hand[i])
del self.hand[i]
def getScore(self):
return sum(self.field)
def toInputs(self):
inputs = []
for card in self.field: # adding the field cards in binary to inputs
inputs += pad([int(i) for i in str(bin(card))[2:]], 3)
# adding a logical value for each card in the game saying whether it is discardable or not
discarded = []
for card in self.graveyard:
discarded.append(int(card)) # now we have all the discarded cards in int form ([1, 25])
discardable = [0 for _ in range(25)]
for card in discarded:
discardable[card - 1] += 1 # now we have the number of discarded cards of each type # the -1 is to avoid out of bounds
# discardable is turned into a boolean array
# this doesn't take into account dead cards
for i in range(25):
if Card.getValueFromInt(i + 1) == 1:
discardable[i] = 1 if discardable[i] <= 1 else 0
elif Card.getValueFromInt(i + 1) == 5:
discardable[i] = 0
else:
discardable[i] = 1 if discardable[i] == 0 else 0
inputs += discardable
# length of deck (binary)
inputs += pad([int(i) for i in str(bin(self.cardsLeft))[2:]], 6)
# number of turns left in binary
inputs += pad([int(i) for i in str(bin(self.nTurns))[2:]], 3)
# current score in binary
inputs += pad([int(i) for i in str(bin(self.getScore()))[2:]], 6)
# number of strikes in binary
inputs += pad([int(i) for i in str(bin(self.strikes))[2:]], 2)
# cards in hand
for card in self.hand:
currentCardInfoBinary = card.toBinary()
currentCardInfoBinary.append(int(hanabi.Hanabi.table.cardPlayable(card)))
currentCardInfoBinary.append(int(hanabi.Hanabi.table.cardDead(card)))
currentCardInfoBinary.append(int(hanabi.Hanabi.table.cardDiscardable(card)))
inputs += currentCardInfoBinary
return inputs
def cardListToStr(self, cards):
s = ''
for i in cards:
s += str(i)
s += '\n'
return s
def __str__(self):
return "field : " + str(self.field) + "\ngraveyard : " + self.cardListToStr(self.graveyard) + "\ncards left : " + str(self.cardsLeft) + "\nstrikes : " + str(self.strikes) + "\nhand : " + self.cardListToStr(self.hand) + "\nturns left : " + str(self.nTurns) + "\n"