-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.py
178 lines (133 loc) · 4.4 KB
/
RPS.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
import random
moves = ['rock', 'paper', 'scissors']
class Player():
def __init__(self):
self.score = 0
def move(self):
return moves[0]
def learn(self, learn_move):
pass
class RandomPlayer(Player):
def move(self):
throw = random.choice(moves)
return (throw)
class ReflectPlayer(Player):
def __init__(self):
Player.__init__(self)
self.learn_move = None
def move(self):
if self.learn_move is None:
throw = moves[0]
else:
throw = self.learn_move
return (throw)
def learn(self, learn_move):
self.learn_move = learn_move
class Cycles(Player):
def __init__(self):
Player.__init__(self)
self.step = 0
def move(self):
throw = None
if self.step == 0:
throw = moves[0]
self.step = self.step + 1
elif self.step == 1:
throw = moves[1]
self.step = self.step + 1
else:
throw = moves[2]
self.step = self.step + 1
return throw
class HumanPlayer(Player):
def move(self):
throw = input('rock, paper, scissors? >')
while throw != 'rock'and throw != 'paper'and throw != 'scissors':
print('Sorry try again')
throw = input('rock, paper, scissors? >')
return (throw)
class Game():
def __init__(self, p2):
self.p1 = HumanPlayer()
self.p2 = p2
def play_game(self):
print("Rock Paper Scissors, Go!")
for round in range(3):
print (f"Round {round}:")
self.play_round()
if self.p1.score > self.p2.score:
print('Player 1 won!')
elif self.p1.score < self.p2.score:
print('Player 2 won!')
else:
print('The game was a tie!')
print('The final score ' + str(self.p1.score) + ' TO ' +
str(self.p2.score))
def play_single(self):
print("Rock Paper Scissors, Go!")
print (f"Round 1 of 1:")
self.play_round()
if self.p1.score > self.p2.score:
print('Player 1 won!')
elif self.p1.score < self.p2.score:
print('Player 2 won!')
else:
print('The game was a tie!')
print('The final score ' + str(self.p1.score) + ' TO ' +
str(self.p2.score))
def play_round(self):
move1 = self.p1.move()
move2 = self.p2.move()
result = Game.play(move1, move2)
self.p1.learn(move2)
self.p2.learn(move1)
def play(self, move1, move2):
print(f"You played {move1}")
print(f"Opponent played {move2}")
if beats(move1, move2):
print ("** PLAYER ONE WINS **")
print(f"Score: Player 1: {move1} Player 2: {move2}\n\n")
self.p1.score += 1
return 1
elif beats(move2, move1):
print ("** PLAYER TWO WINS **")
print(f"Score: Player 1: {move1} Player 2: {move2}\n\n")
self.p2.score += 1
return 2
else:
print ("** It's A TIE **")
print(f"Score: Player 1: {move1} Player 2: {move2}\n\n")
return 0
def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
(one == 'scissors' and two == 'paper') or
(one == 'paper' and two == 'rock'))
if __name__ == '__main__':
answer = [Player(), RandomPlayer(), Cycles(), ReflectPlayer()]
p2 = input('Select the RPS game you would like to play or just hit any\
key and enter for random game: [1]Rock, [2]Random,\
[3]Reflective, or [4]Cycles: >')
while p2 != 1 or p2 != 2 or p2 != 3 or p2 != 4:
p2 = random.choice(answer)
break
if p2 == '1':
p2 = Player()
elif p2 == '2':
p2 = RandomPlayer()
elif p2 == '3':
p2 = Cycles()
elif p2 == '4':
p2 = ReflectPlayer()
rounds = input('Enter for [s]ingle game or [f]ull game: >')
Game = Game(p2)
while True:
if rounds == 's':
Game.play_single()
break
elif rounds == 'f':
Game.play_game()
break
else:
print('Sorry try again')
rounds = input('Enter 1 for a single\
game and 2 for a full game: >')