-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeLink 15.py
More file actions
352 lines (276 loc) · 9.99 KB
/
CodeLink 15.py
File metadata and controls
352 lines (276 loc) · 9.99 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
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
# THE COMPUTER ALWAYS WINS
# MIT PRESS
#
# CodeLink 15
# Rock, Paper, Paper
# last revised 12/25/24
# This is a full implementation of the code we wrote in the first
# half of Chapter Ten. Once you test it out, see if you can gain
# further advantage by adding additional functions like
# isBoring() isSoreLoser().
import random
# set number of games to play
maxGames = 300
# define constants, for readable code
RELUCTANT = 1
BORING = 2
RANDOM = 3
## FUNCTIONS THAT SIMULATE THE VARIOUS TYPES OF HUMAN PLAYERS
def playRandomly():
# output: a random move
choice = random.randint(1,3)
if choice == 1:
return ("ROCK")
if choice == 2:
return ("PAPER")
else:
return("SCISSORS")
def playReluctantly(priorPlayerMove):
# output: a move that is unlikely to be the same as the most prior move
# choose a random number from 1 to 13
# 6/13 odds for each of two new moves
# 1/13 odds for repeating this move
currentMove = random.randint(1, 13)
if priorPlayerMove == "ROCK":
if currentMove in [1]:
return "ROCK"
if currentMove in [2, 3, 4, 5, 6, 7]:
return "PAPER"
if currentMove in [8, 9, 10, 11, 12, 13]:
return "SCISSORS"
if priorPlayerMove == "PAPER":
if currentMove in [1, 2, 3, 4, 5, 6]:
return "ROCK"
if currentMove in [7]:
return "PAPER"
if currentMove in [8, 9, 10, 11, 12, 13]:
return "SCISSORS"
if priorPlayerMove == "SCISSORS":
if currentMove in [1, 2, 3, 4, 5, 6]:
return "ROCK"
if currentMove in [7, 8, 9, 10, 11, 12]:
return "PAPER"
if currentMove in [13]:
return "SCISSORS"
def playBoringly(playerHistory):
# output is 4/6 chance of being the player's most common prior choice, 1/6 each other choice
numGames = len(playerHistory)
choices = [0,0,0]
maxCount = 0
# find the most-used choice
for ctr in range(numGames):
if playerHistory[ctr] == "ROCK":
choices[0] +=1
if choices[0] > maxCount:
maxCount = choices[0]
mainChoice = "ROCK"
if playerHistory[ctr] == "PAPER":
choices[1] +=1
if choices[1] > maxCount:
maxCount = choices[1]
mainChoice = "PAPER"
if playerHistory[ctr] == "SCISSORS":
choices[2] +=1
if choices[2] > maxCount:
maxCount = choices[2]
mainChoice = "SCISSORS"
currentMove = random.randint(1,6)
if mainChoice == "ROCK":
if currentMove in [1, 2, 3, 4]:
return "ROCK"
if currentMove in [5]:
return "PAPER"
if currentMove in [6]:
return "SCISSORS"
if mainChoice == "PAPER":
if currentMove in [1]:
return "ROCK"
if currentMove in [2, 3, 4, 5]:
return "PAPER"
if currentMove in [6]:
return "SCISSORS"
if mainChoice == "SCISSORS":
if currentMove in [1]:
return "ROCK"
if currentMove in [2]:
return "PAPER"
if currentMove in [3, 4, 5, 6]:
return "SCISSORS"
## FUNCTIONS THAT RESPOND TO VARIOUS HUMAN QUIRKS
def respondReluctant(playerMove):
# input: the player's most recent move
# output: the computer's best response if the player will not repeat
if playerMove == "ROCK":
return ("SCISSORS")
if playerMove == "SCISSORS":
return ("PAPER")
else:
return("ROCK")
def respondBoring(playerHistory):
# input: the player's full move history
# output: the computer's best response if the player plays the most common prior move
numGames = len(playerHistory)
choices = [0,0,0]
maxCount = 0
# find the most-used choice
for ctr in range(numGames):
if playerHistory[ctr] == "ROCK":
choices[0] +=1
if choices[0] > maxCount:
maxCount = choices[0]
mostCommon = "ROCK"
if playerHistory[ctr] == "PAPER":
choices[1] +=1
if choices[1] > maxCount:
maxCount = choices[1]
mostCommon = "PAPER"
if playerHistory[ctr] == "SCISSORS":
choices[2] +=1
if choices[2] > maxCount:
maxCount = choices[2]
mostCommon = "SCISSORS"
if mostCommon == "ROCK":
return ("PAPER")
if mostCommon == "PAPER":
return ("SCISSORS")
if mostCommon == "SCISSORS":
return ("ROCK")
## FUNCTIONS THAT DETECT VARIOUS HUMAN QUIRKS
def isReluctant (playerHistory):
# input: the player's full move history
# output: game results if the computer assumes this player is reluctant to repeat
outcome = 0
numGames = len(playerHistory)
for ctr in range(1, numGames):
playerChoice = playerHistory[ctr]
computerChoice = respondReluctant(playerHistory[ctr-1])
outcome = outcome + calculateOutcome(playerChoice, computerChoice)
return(outcome)
def isBoring (playerHistory):
# input: the player's full move history
# output: game results if computer assumes this player mainly plays the same move
outcome = 0
numGames = len(playerHistory)
partialPlayerHistory = []
partialPlayerHistory.append(playerHistory[0])
for ctr in range(1, numGames):
playerChoice = playerHistory[ctr]
computerChoice = respondBoring(partialPlayerHistory)
outcome = outcome + calculateOutcome(playerChoice, computerChoice)
partialPlayerHistory.append(playerChoice)
return(outcome)
def isRandom ():
# output: one third the number of games played, which is the random outcome
numGames = len(playerHistory)
return (numGames/3)
## THIS FUNCTION LETS THE COMPUTER CHOOSE ITS BEST MOVE
def chooseBestResponseStrategy (playerHistory, computerHistory):
# input is the full history so far between these players
# output is the computer's best next move based on that history
numGames = len(playerHistory)
# computer evaluates some possible options
reluctantScore = isReluctant(playerHistory)
boringScore = isBoring(playerHistory)
randomScore = isRandom()
# computer finds the best strategy
computerChoice = RELUCTANT
bestScore = reluctantScore
if boringScore > bestScore:
computerChoice = BORING
bestScore = boringScore
if randomScore > bestScore:
computerChoice = RANDOM
bestScore = randomScore
return computerChoice
## REPORT BACK WHO WON, OR IF IT WAS A TIE
def calculateOutcome (playerMove, computerMove):
# input: the moves this round
# output: 1 if the computer wins, -1 if the computer loses, 0 if tie
outcome = 0
if playerMove == "ROCK":
if computerMove == "SCISSORS":
outcome = -1
elif computerMove == "PAPER":
outcome = 1
else:
outcome = 0
if playerMove == "SCISSORS":
if computerMove == "PAPER":
outcome = -1
elif computerMove == "ROCK":
outcome = 1
else:
outcome = 0
if playerMove == "PAPER":
if computerMove == "ROCK":
outcome = -1
elif computerMove == "SCISSORS":
outcome = 1
else:
outcome = 0
return (outcome)
# main program
# ############
while True:
# define variables
playerHistory = []
computerHistory = []
computerWins = 0
playerWins = 0
ties = 0
# allow user to pick a player type
print ("")
print ("Choose a type of player.")
print ("")
print ("1 = A player who is reluctant to repeat back-to-back.")
print ("2 = A boring player who disproportionately plays one move.")
print ("3 = A truly random player.")
print ("")
humanStrategy = int(input("=> "))
# play the game
for counter in range(maxGames):
if counter == 0:
# the first move for both computer and player is random
computerMove = playRandomly()
playerMove = playRandomly()
else:
if humanStrategy == RELUCTANT:
playerMove = playReluctantly(playerHistory[counter-1])
if humanStrategy == BORING:
playerMove = playBoringly(playerHistory)
if humanStrategy == RANDOM:
playerMove = playRandomly()
if counter == 1:
# the second move for the computer is also random
computerMove = playRandomly()
else:
# starting with the 3rd move, however, the computer can learn
computerStrategy = chooseBestResponseStrategy(playerHistory, computerHistory)
if computerStrategy == RELUCTANT:
computerMove = respondReluctant(playerHistory[counter-1])
if computerStrategy == BORING:
computerMove= respondBoring(playerHistory)
if computerStrategy == RANDOM:
computerMove = playRandomly()
result = calculateOutcome (playerMove, computerMove)
if result == 1:
computerWins = computerWins + 1
elif result == -1:
playerWins = playerWins + 1
else:
ties = ties + 1
playerHistory.append(playerMove)
computerHistory.append(computerMove)
# report final statistics
if computerStrategy == RELUCTANT:
description = "RELUCTANT"
if computerStrategy == BORING:
description = "BORING"
if computerStrategy == RANDOM:
description = "RANDOM"
print ("")
print ("I just played %d games against this player." % (counter+1))
print ("In the end, I decided that this is a %s player." %description)
print ("As I figured that out, I won %2.0f percent of the games." % (100*computerWins/maxGames))
print ("")
print ("Let's play again.")