-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpsls.py
173 lines (143 loc) · 5.42 KB
/
rpsls.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
import random
#These are the rules of the game. I use this as a database of possible options and a way of determining who wins.
#
rules = [['paper','covers','rock'],
['paper','disproves','spock'],
['scissors','cuts', 'paper'],
['scissors','decapitates','lizard'],
['spock','smashes','scissors'],
['spock','vaporizes','rock'],
['lizard','eats','paper'],
['lizard','poisions','spock'],
['rock','crushes','lizard'],
['rock','crushes','scissors']]
validpicks = ['rock','paper','scissors','lizard','spock']
#This is the class where the hand and the win status are stored
class player():
win = False
lastwin = False
hand = ""
win_count = 0
lose_count = 0
tie_count = 0
total_games = 0
win_percentage = 0.00
history = []
def getpercentage(self): #calculates your win percentage
self.total_games = self.win_count+self.lose_count+self.tie_count
if self.total_games !=0:
self.win_percentage = (self.win_count/(self.total_games))*100
else:
self.win_percentage = 0.00
def makechoice(self,choice):
self.hand = choice
def youwin(self):
self.win = True
self.win_count +=1
def youlose(self):
self.win = False
self.lose_count +=1
def youtie(self):
self.win = False
self.tie_count+=1
def reset(self):
self.hand = ""
if self.win == False:
self.lastwin = False
else:
self.lastwin = True
self.win = False
def showrecord(self):
print("Your record: %d-%d-%d \nWin Percentage: %.2f"%(self.win_count,self.lose_count,self.tie_count,self.win_percentage))
human = player()
computer = player()
difficulty = input("Welcome to RSPLS! Do you want to play against: \n\n1.) A random guesser\n2.) A mildly intelligent AI\n3.) An experimental expert AI\nEnter 1, 2, or 3: ")
while difficulty != '1' and difficulty != '2' and difficulty != '3':
difficulty= input("Not a valid input! Enter difficulty level (1, 2, or 3): ")
def gameloop():
def most_common(lst):
max = 0
maxitemlist = []
for x in set(lst):
count = lst.count(x)
if count >= max:
max = count
maxitemlist.append(x)
return maxitemlist
favoritepicks = most_common(human.history)
counterpicks = []
def findopposites(list_of_options): #Input list of picks, returns a list of picks that will beat them.
opposite_list = []
for option in list_of_options:
for rule in rules:
if rule[2] == option:
opposite_list.append(rule[0])
return(opposite_list)
counterpicks = findopposites(favoritepicks)
randompick = validpicks[random.randrange(len(validpicks))] #This will return either RPSLS
if len(counterpicks)!=0:
learnedpick = counterpicks[random.randrange(len(counterpicks))]
else:
learnedpick = randompick
if len(human.history) != 0:
if computer.lastwin == True:
smartpick = human.history[-1]
if computer.lastwin == False:
smartpick = findopposites([human.history[-1]])[random.randrange(2)]
else:
smartpick = randompick
if difficulty == "1":
computer.makechoice(randompick)
computer.history.append(randompick)
if difficulty == "2":
computer.makechoice(learnedpick)
computer.history.append(learnedpick)
if difficulty == "3":
computer.makechoice(smartpick)
computer.history.append(smartpick)
while human.hand == "": #This will loop until the player puts in a valid input
humanpick = input("3,2,1... Rock! Paper! Scissors! Lizard! Spock! Pick one: ").lower()
if humanpick in validpicks:
human.makechoice(humanpick)
human.history.append(humanpick)
else:
print('Not a valid input! Try again...')
print("You picked: %s \nComputer picked: %s" %(human.hand,computer.hand))
if human.hand == computer.hand:
print("You tie!")
human.youtie()
computer.youtie()
else:
def findwinner(player1,player2):
for x in range(len(rules)):
if rules[x][0] == player1.hand and rules[x][2] == player2.hand:
player1.youwin()
player2.youlose()
return 0
player2.youwin()
player1.youlose()
return 0
findwinner(human,computer)
if human.win:
for x in range(len(rules)):
if rules[x][0] == human.hand and rules[x][2] == computer.hand:
print(" ".join(rules[x]))
print("You win!")
if computer.win:
for x in range(len(rules)):
if rules[x][2] == human.hand and rules[x][0] == computer.hand:
print(" ".join(rules[x]))
print("You lose!")
human.getpercentage()
human.showrecord()
human.reset()
computer.reset()
playagain = input("Do you want to play again? (Y/N): ").lower()
if playagain == 'y':
gameloop()
else:
print(("Games Played: %d "%human.total_games))
human.showrecord()
print("Goodbye!\n")
quit()
gameloop()