-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbots.py
170 lines (126 loc) · 4.79 KB
/
bots.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
import random
def play(player1, player2, num_games, verbose=False):
p1_prev_play = ""
p2_prev_play = ""
results = {"p1": 0, "p2": 0, "tie": 0}
for _ in range(num_games):
p1_play = player1(p2_prev_play,player2.__name__)
p2_play = player2(p1_prev_play,player1.__name__)
if p1_play == p2_play:
results["tie"] += 1
winner = "Tie."
elif (p1_play == "P" and p2_play == "R") or (
p1_play == "R" and p2_play == "S") or (p1_play == "S"
and p2_play == "P"):
results["p1"] += 1
winner = "Player 1 wins."
elif p2_play == "P" and p1_play == "R" or p2_play == "R" and p1_play == "S" or p2_play == "S" and p1_play == "P":
results["p2"] += 1
winner = "Player 2 wins."
if verbose:
print("Player 1:", p1_play, "| Player 2:", p2_play)
print(winner)
print()
p1_prev_play = p1_play
p2_prev_play = p2_play
games_won = results['p2'] + results['p1']
if games_won == 0:
win_rate = 0
else:
win_rate = results['p1'] / games_won * 100
# print("Final results:", results)
# print(f"Player 1 win rate: {win_rate}%")
# print()
return results, win_rate
def play_game(player1, player2, num_games, verbose=False):
p1_prev_play = ""
p2_prev_play = ""
results = {"p1": 0, "p2": 0, "tie": 0}
for _ in range(num_games):
p1_play = player1(p2_prev_play,player2.__name__)
p2_play = player2(p1_prev_play,player1.__name__)
if p1_play == p2_play:
results["tie"] += 1
winner = "Tie."
elif (p1_play == "P" and p2_play == "R") or (
p1_play == "R" and p2_play == "S") or (p1_play == "S"
and p2_play == "P"):
results["p1"] += 1
winner = "Player 1 wins."
elif p2_play == "P" and p1_play == "R" or p2_play == "R" and p1_play == "S" or p2_play == "S" and p1_play == "P":
results["p2"] += 1
winner = "Player 2 wins."
p1_prev_play = p1_play
p2_prev_play = p2_play
games_won = results['p2'] + results['p1']
if games_won == 0:
win_rate = 0
else:
win_rate = results['p1'] / games_won * 100
return win_rate
def alex_strategy(prev_opponent_play, opponent_name, counter=[0]):
counter[0] += 1
choices = ["R", "R", "P", "P", "S"]
return choices[counter[0] % len(choices)]
def jordan_strategy(prev_opponent_play, opponent_name, opponent_history=[]):
opponent_history.append(prev_opponent_play)
last_ten = opponent_history[-10:]
most_frequent = max(set(last_ten), key=last_ten.count)
if most_frequent == '':
most_frequent = "S"
ideal_response = {'P': 'S', 'R': 'P', 'S': 'R'}
return ideal_response[most_frequent]
def taylor_strategy(prev_opponent_play, opponent_name):
if prev_opponent_play == '':
prev_opponent_play = "R"
ideal_response = {'P': 'S', 'R': 'P', 'S': 'R'}
return ideal_response[prev_opponent_play]
def casey_strategy(prev_opponent_play, opponent_name,
opponent_history=[],
play_order=[{
"RR": 0,
"RP": 0,
"RS": 0,
"PR": 0,
"PP": 0,
"PS": 0,
"SR": 0,
"SP": 0,
"SS": 0,
}]):
if not prev_opponent_play:
prev_opponent_play = 'R'
opponent_history.append(prev_opponent_play)
last_two = "".join(opponent_history[-2:])
if len(last_two) == 2:
play_order[0][last_two] += 1
potential_plays = [
prev_opponent_play + "R",
prev_opponent_play + "P",
prev_opponent_play + "S",
]
sub_order = {
k: play_order[0][k]
for k in potential_plays if k in play_order[0]
}
prediction = max(sub_order, key=sub_order.get)[-1:]
ideal_response = {'P': 'S', 'R': 'P', 'S': 'R'}
return ideal_response[prediction]
def human_player(prev_opponent_play, opponent_name):
play = ""
while play not in ['R', 'P', 'S']:
play = input("[R]ock, [P]aper, [S]cissors? ")
print(play)
return play
def rock_player(prev_opponent_play, opponent_name):
return 'R'
def paper_player(prev_opponent_play, opponent_name):
return 'P'
def scissors_player(prev_opponent_play,opponent_name):
return 'S'
def mimic_strategy(prev_opponent_play, opponent_name):
if prev_opponent_play == '':
prev_opponent_play = 'R'
return prev_opponent_play
def random_strategy(prev_opponent_play, opponent_name):
return random.choice(['R', 'P', 'S'])