-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.py
209 lines (161 loc) · 5.37 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
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
import random
MAX_PLAYERS = 3
ROCK = "\U0000270A"
PAPER = "\U0000270B"
SCISSORS = "\U0000270C"
N_GAMES = 3
def main():
print_welcome()
players = []
players.append(Player("human"))
ai_opponents = input("How many AI opponents do you want to play against? ")
for i in range(int(ai_opponents)):
players.append(Player("ai"))
print()
print("Let's begin! Our players are: ")
for p in players:
print(f" > \33[1m{p.name}\33[0m")
print()
for round in range(N_GAMES):
hround = round + 1
print(f"\33[1mRound {hround}!\33[0m")
print()
pmoves = []
# check for player moves
for p in players:
print(f"{p.name}'s turn!")
move = p.rps()
print(f"{p.name} plays {move}!")
pmoves.append([p, move])
print()
print()
ties = []
for cp in pmoves:
current_player = cp[0]
current_move = cp[1]
for op in pmoves:
opponent_player = op[0]
opponent_move = op[1]
if current_player != opponent_player:
if current_player.beats(current_move, opponent_move):
print(f"{current_player.name} beats {opponent_player.name}! One point to {current_player.name}!")
current_player.award(1)
elif current_move == opponent_move:
ties.append(set([current_player, opponent_player]))
while ties:
t = ties.pop()
tied_string = ", ".join([x.name for x in t])
for t2 in ties:
if t == t2:
ties.remove(t2)
print(f"We had a tie between {tied_string}!")
print()
# we'll need to sort the list by high score to low score
players.sort(key=lambda x: x.score, reverse=True)
#add rankings based on scores
prev = None
for i, p in enumerate(players):
if p.score!=prev:
place,prev = i+1,p.score
p.rank = place
# resort by rank numbers
players.sort(key=lambda x: x.rank)
print()
print("\33[1mGame over!\33[0m")
print()
for p in players:
print(f"Rank #{p.rank}: {p.name} (score: {p.score})")
class Player:
def __init__(self, ai_or_human):
self._name = ""
self._score = 0
self._rank = 0
if ai_or_human == "human":
self._classification = "human"
elif ai_or_human == "ai":
self._classification = "ai"
else:
# assume human with no params
self._classification = "human"
self.prompt_name()
def __str__(self):
return f"{self._name} (score: {self._score})"
def __repr__(self):
return f"{self._name} (score: {self._score})"
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def classification(self):
return self._classification
@classification.setter
def classification(self, classification):
self._classification = classification
@property
def score(self):
return self._score
@score.setter
def score(self, score):
self._score + score
@property
def rank(self):
return self._rank
@rank.setter
def rank(self, rank):
self._rank = rank
def award(self, points):
self._score = self._score + points
return self._score
def beats(self, m1, m2):
if m1 == ROCK and m2 == SCISSORS:
return True
elif m1 == PAPER and m2 == ROCK:
return True
elif m1 == SCISSORS and m2 == PAPER:
return True
elif m1 == m2:
return False
else:
return False
def prompt_name(self):
ai_names = ["T1000", "T800", "HAL9000", "Colossus", "R2D2", "Skynet",
"C-3PO", "Mother", "Cyberman", "Dalek", "Chappie", "MCP", "VIKI",
"Ava", "Ultron", "Gunslinger"]
if self.classification == "ai":
self.name = random.choice(ai_names)
else:
self.name = input("Please enter your name: ")
def rps(self):
rps_string = {
"r": ROCK,
"p": PAPER,
"s": SCISSORS
}
if self.classification == "ai":
rand = random.random()
if rand >= 0 and rand <= 0.333:
return ROCK
elif rand > 0.333 and rand <= 0.666:
return PAPER
elif rand > 0.666 and rand <= 1:
return SCISSORS
elif self.classification == "human":
rps_input = input("Enter your choice - [p]aper, [r]ock, [s]cissors: ")
while rps_input != "r" and rps_input != "p" and rps_input != "s":
print("Invalid choice, enter r, p, or s!")
rps_input = input("Enter your choice - [p]aper, [r]ock, [s]cissors: ")
return rps_string[rps_input]
def print_welcome():
"""
prints a welcome message
"""
print('\33[1mWelcome to Rock Paper Scissors!\33[0m')
print(f"You will play {N_GAMES} games against the AI.")
print(f"{ROCK} beats {SCISSORS}, {SCISSORS} beats {PAPER}, {PAPER} beats {ROCK}")
print('')
print('')
if __name__ == '__main__':
main()