-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpssl.py
388 lines (308 loc) · 11.9 KB
/
rpssl.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
###############################################################################
## Filename : rpssl.py
## Author : Andrew Laing ([email protected])
## Source : Python 3.5
## Description : The gameplay methods for the RPSSL application.
## History : Work started 29/09/2015
###############################################################################
from random import randint
from random import choice as rndchoice
from os import path
for filename in ["utils.py","conf/gameRules.py","classifier.py"]:
if not path.isfile(filename):
print("Error: Configuration file '%s' not found."% filename)
print("Exiting program.")
exit()
from utils import saveToPickle, loadFromPickle
import conf.gameRules as gameRules
import classifier as rpsslClassifier
class RPSSL(object):
def __init__(self, loadPk=0):
"""
Init method for the RPSSL Class.
Loads the configuration file or creates one.
"""
loaded = 0
# Autoload the configuration data stored in rpssl.pk
if loadPk:
self.__dict__ = loadFromPickle("conf/rpssl.pk")
self.classifier = rpsslClassifier.naivebayes(loadPk=1)
loaded = 1
## If rpssl.pk doesn't exist yet run the basic configuration
if not loaded:
self.maxRounds = 5
self.choices = gameRules.choices
self.validGuesses = gameRules.validGuesses
self.winningChoices = gameRules.winningChoices
self.rules = gameRules.rules
# For implementing strategy 2. ####################################
# if using probablilty is losing, add the possiblity of a random
# choice being made by the bot
self.strategy2percentage = 0.75
self.sampleSize = 0 # total rounds played
self.botWins = 0 # total bot wins
self.humanWins = 0 # total human wins
###################################################################
## Initialise round variables
self.initCurrentGameVars()
## Initialise Intelligence variables
self.initIntelligence()
## Create configuration file
saveToPickle("conf/rpssl.pk", self.__dict__)
self.classifier = rpsslClassifier.naivebayes()
self.configChanged = 0 # Flag for configuation changes
self.roundsPlayed = 0
def initPlayerOneVars(self):
"""
Initialising Player One variables.
Called by self.__init__() and self.playNewGame()
"""
self.playerOneGuess = 0
self.playerOneScore = 0
def initPlayerTwoVars(self):
"""
Re/Initialise Player One variables.
Called by self.__init__() and self.playNewGame()
"""
self.playerTwoGuess = 0
self.playerTwoScore = 0
def initCurrentGameVars(self):
"""
Re/Initialise Current Game variables.
Called by self.__init__() and self.playNewGame()
"""
self.initPlayerOneVars()
self.initPlayerTwoVars()
self.roundsPlayed = 0
self.drawsScore = 0
self.winner = 0
self.winString = "Draw, Nobody wins!"
def initIntelligence(self):
"""
Initialise Intelligence variables.
Called by self.__init__()
"""
self.initHumanStats()
self.initBotStats()
def initHumanStats(self):
"""
Initialise Human Statistic variables.
Called by self.initIntelligence()
"""
self.lastChoice = None
self.choiceBeforeLast = None
self.choice2BeforeLast = None
def initBotStats(self):
"""
Initialise Bot Statistic variables.
Called by self.initIntelligence()
"""
self.botLastChoice = None
self.botChoiceBeforeLast = None
self.botChoice2BeforeLast = None
def saveOnQuit(self):
"""
Save the configuration and classifier when quitting the app.
"""
saveToPickle("conf/rpssl.pk", self.__dict__)
self.classifier.saveCounts()
## INTELLIGENCE METHODS ###################################################
def updateHumanStatsAndGetFeatures(self):
"""
Update the stats stored for the human then create
and return a list of features.
Called by self.trainClassifier()
"""
features = []
# update stats kept on human
prevLast = self.lastChoice
prevBefore = self.choiceBeforeLast
prev2Before = self.choice2BeforeLast
if self.choiceBeforeLast != None:
self.choice2BeforeLast = self.choiceBeforeLast
if self.lastChoice != None:
self.choiceBeforeLast = self.lastChoice
self.lastChoice = self.validGuesses[self.playerOneGuess]
# Update features list with previous human choices
if prevLast != None:
features.append(prevLast)
if prevBefore != None:
temp = prevBefore+prevLast
features.append(temp)
if prev2Before != None:
temp = prev2Before+prevBefore+prevLast
features.append(temp)
return features
def updateBotStatsAndGetFeatures(self):
"""
Update the stats stored for the bot then create
and return a list of features.
Called by self.trainClassifier()
"""
features = []
# update stats kept on bot
prevLast = self.botLastChoice
prevBefore = self.botChoiceBeforeLast
prev2Before = self.botChoice2BeforeLast
if self.botChoiceBeforeLast != None:
self.botChoice2BeforeLast = self.botChoiceBeforeLast
if self.botLastChoice != None:
self.botChoiceBeforeLast = self.botLastChoice
self.botLastChoice = self.validGuesses[self.playerTwoGuess]
# Update features list with previous bot choices
if prevLast != None:
temp = "b"+prevLast
features.append(temp)
if prevBefore != None:
temp = "b"+prevBefore+prevLast
features.append(temp)
if prev2Before != None:
temp = "b"+prev2Before+prevBefore+prevLast
features.append(temp)
return features
def trainClassifier(self):
"""
Send features and category to the classifier.
Called by self.playGame()
"""
features = self.updateHumanStatsAndGetFeatures()
features.extend( self.updateBotStatsAndGetFeatures() )
category = self.validGuesses[self.playerOneGuess]
self.classifier.train(features, category)
def getChoiceToDefeatHuman(self, guess):
"""
Return an option which will defeat what the classifier
has calculated that the human player will choose.
Called by self.getBotChoice()
"""
return rndchoice(self.winningChoices[guess])
def getProbableHumanChoice(self):
"""
Creates a list of features and sends them to the classifier
to get a probable category.
Called by self.getBotChoice()
"""
features = []
onemb = self.lastChoice
bOnemb = self.botLastChoice
if onemb!=None:
features.append(onemb)
if self.choiceBeforeLast!=None:
twomb = self.choiceBeforeLast + onemb
features.append(twomb)
if self.choice2BeforeLast!=None:
threemb = self.choice2BeforeLast + twomb
features.append(threemb)
if bOnemb!=None:
features.append("b"+bOnemb)
if self.botChoiceBeforeLast!=None:
bTwomb = self.botChoiceBeforeLast + bOnemb
features.append("b"+bTwomb)
if self.botChoice2BeforeLast!=None:
bThreemb = self.botChoice2BeforeLast + bTwomb
features.append("b"+bThreemb)
guess = randint(0,len(self.validGuesses)-1)
default = self.validGuesses[guess]
best = self.convertGuessToInt(self.classifier.classify(features,default))
return best
def getBotChoice(self, player):
"""
Get and return the bot's choice.
Called by self.playARound()
"""
## Get a number that is an index to the validGuesses list
## using the naivebayesian classifier
if self.humanWins > (self.sampleSize * self.strategy2percentage):
baseline = ((self.humanWins/float(self.sampleSize)) * 40) # if rand < this
if randint(0,100) < baseline:
guess = randint(0,len(self.validGuesses)-1)
else:
guess = self.getProbableHumanChoice()
choice = self.getChoiceToDefeatHuman(guess)
else:
guess = self.getProbableHumanChoice()
choice = self.getChoiceToDefeatHuman(guess)
return choice
## GAMEPLAY METHODS #######################################################
def adjustScores(self):
"""
Adjust the stored scores for the current game.
Called by self.playGame()
"""
## Augment the winner's score
if self.winner==1:
self.playerOneScore+=1
self.humanWins+=1
elif self.winner==2:
self.playerTwoScore+=1
self.botWins+=1
elif self.winner==0: self.drawsScore+=1
self.sampleSize+=1
def calculateWinner(self):
"""
Calculate winner of round and update round winner data.
Called by self.playGame()
"""
## Create a tuple representing the player's choices
rulesKey = (self.playerOneGuess,self.playerTwoGuess)
## Find the winner from the rules dictionary
self.winner = self.rules[rulesKey]["winner"]
self.winString = self.rules[rulesKey]["winStr"]
if self.winner==1:
self.winString += "\nPlayer wins!"
elif self.winner==2:
self.winString += "\nBot wins!"
def convertGuessToInt(self, guess):
"""
Convert the player's guess string to an integer.
Called by self.getHumanChoice() and self.getProbableHumanChoice()
"""
return self.validGuesses.index(guess)
def playARound(self, guess):
"""
Play a whole round.
Called by self.playGame()
"""
self.playerOneGuess=self.convertGuessToInt(guess)
self.playerTwoGuess=self.getBotChoice(2)
def getGameResult(self):
"""
Return the result of the Game as a string.
"""
## Create a string containing the final score
score = "%d:%d" % (self.playerTwoScore, self.playerOneScore)
## Find out who won the game using conditional operator comparisons
## And return the result
result = ""
if self.playerOneScore > self.playerTwoScore:
result += "Player has won the game!"
elif self.playerTwoScore > self.playerOneScore:
result += "Bot has won the game!"
else: result += "The game was drawn!"
result += "\nThe final score was " + score
return result
def playGame(self, choice):
"""
Play a game.
"""
self.playARound(choice)
self.calculateWinner()
self.adjustScores()
## update the stats and train the classifier
self.trainClassifier()
self.roundsPlayed += 1
if self.roundsPlayed == self.maxRounds:
self.classifier.saveCounts()
def startGame(self):
"""
Clear screen and start a new game.
"""
## Reset the gameplay variables to their initial state
self.initCurrentGameVars()
saveToPickle("conf/rpssl.pk", self.__dict__)
def changeMaxRounds(self, n):
"""
Change number of rounds per game.
"""
self.configChanged = 1
self.maxRounds = n