-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (117 loc) · 4.33 KB
/
main.py
File metadata and controls
144 lines (117 loc) · 4.33 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
import random
class Player:
def __init__(self, score=0):
self.score = score
self.moves = ['rock', 'paper', 'scissors']
def move(self):
return 'rock'
def learn(self, my_move, their_move):
pass
class RockPlayer(Player):
def move(self):
return 'rock'
def learn(self, my_move, their_move):
pass
class RandomPlayer(Player):
def move(self):
return random.choice(self.moves)
def learn(self, my_move, their_move):
pass
class HumanPlayer(Player):
def move(self):
choice = input("Choose your move, R for rock,"
" P for paper or S for scissors?\n")
if choice == 'S' or choice == 's' or choice == 'scissors':
return 'scissors'
elif choice == 'P' or choice == 'p' or choice == 'paper':
return 'paper'
if choice == 'R' or choice == 'r' or choice == 'rock':
return 'rock'
else:
print("This is an invalid input")
return self.move()
def learn(self, my_move, their_move):
pass
class ReflectPlayer(Player):
def move(self):
# check if my_move is not yet added (round-01)
# to the dictionary class (self object) beside score & moves
if len(self.__dict__.keys()) == 2:
self.my_move = random.choice(self.moves)
return self.my_move
# check if my_move is added (round-02,03)
# to the dictionary class (self object) beside score & moves
elif len(self.__dict__.keys()) > 2:
print(self.my_move)
return self.my_move
def learn(self, my_move, their_move):
self.my_move = their_move
return self.my_move
class CyclePlayer(Player):
def move(self):
if len(self.__dict__.keys()) == 2:
return random.choice(self.moves)
else:
if self.my_move in self.moves:
# get the index of the item
indx = self.moves.index(self.my_move)
# create a repeatted list to avoid out of rane error
cycle_list = [x for i in range(2) for x in self.moves]
return cycle_list[indx + 1]
def learn(self, my_move, their_move):
self.my_move = my_move
return my_move
def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
(one == 'scissors' and two == 'paper') or
(one == 'paper' and two == 'rock'))
class Game:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def play_round(self):
move1 = self.p1.move()
move2 = self.p2.move()
print(f"Player 1: {move1} Player 2: {move2}")
self.p1.learn(move1, move2)
self.p2.learn(move2, move1)
if beats(move1, move2):
print("Player 1 wins the round!")
self.p1.score = + 1
elif beats(move2, move1):
print("Player 2 wins the round!")
self.p2.score = + 1
else:
print("That was a tie!")
def play_game(self):
print("Game start!")
print('Hello, you are playing the traditional'
' Rock Paper Scissors as Player 1')
print("The game will run in 3 rounds")
for round in range(3):
print(f"Round {round+1}:")
self.play_round()
print(f"Player 1 score is: {self.p1.score}")
print(f"Player 2 score is: {self.p2.score}")
if self.p1.score > self.p2.score:
print(f"Final Game Score: {self.p1.score}/{self.p2.score}")
print("__Player 1 wins the game!__")
else:
print(f"Final Game Score: {self.p1.score}/{self.p2.score}")
print("__Player 2 wins the game!__")
print("Game over!")
if __name__ == '__main__':
opponent = input("Choose your opponent, R for Random Player, "
"Ref for Reflected Player or C for Cycle Player\n")
if opponent == 'R' or opponent == 'r':
game = Game(HumanPlayer(), RandomPlayer())
game.play_game()
elif opponent == 'Ref' or opponent == 'ref':
game = Game(HumanPlayer(), ReflectPlayer())
game.play_game()
elif opponent == 'C' or opponent == 'c':
game = Game(HumanPlayer(), CyclePlayer())
game.play_game()
else:
game = Game(HumanPlayer(), RandomPlayer())
game.play_game()