-
Notifications
You must be signed in to change notification settings - Fork 0
/
opponents.py
83 lines (69 loc) · 2.19 KB
/
opponents.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
move_that_beats = {
"r":"p",
"p":"s",
"s":"r"
}
import random
def opponent_random(my_history, their_history):
# if no history, play random
return "rps"[random.randint(0,2)]
def opponent_beat_last(my_history, their_history):
# beat opponents last move
if len(their_history) > 0:
return move_that_beats[their_history[-1]]
# if no history, play random
return "rps"[random.randint(0,2)]
def opponent_repeat_rs(my_history, their_history):
# repeat rock, scissors
if len(my_history) % 2 == 0:
return "r"
return "s"
def opponent_repeat_pr(my_history, their_history):
# repeat paper, rock
if len(my_history) % 2 == 0:
return "p"
return "r"
def opponent_repeat_sp(my_history, their_history):
# repeat scissors, paper
if len(my_history) % 2 == 0:
return "s"
return "p"
def opponent_repeat_rps(my_history, their_history):
# repeat rock, paper, scissors
if len(my_history) % 3 == 0:
return "r"
elif len(my_history) % 3 == 1:
return "p"
return "s"
def opponent_repeat_prs(my_history, their_history):
# repeat paper, rock, scissors
if len(my_history) % 3 == 0:
return "p"
elif len(my_history) % 3 == 1:
return "r"
return "s"
def opponent_repeat_srp(my_history, their_history):
# repeat scissors, rock, paper
if len(my_history) % 3 == 0:
return "s"
elif len(my_history) % 3 == 1:
return "r"
return "p"
def opponent_copy_last(my_history, their_history):
# copy opponents last move
if len(their_history) > 0:
return their_history[-1]
# if no history, play random
return "rps"[random.randint(0,2)]
move_that_loses = {
"p" : "r",
"s" : "p",
"r" : "s"
}
def opponent_antibeat_last(my_history, their_history):
# copy opponents last move
if len(their_history) > 0:
return move_that_loses[their_history[-1]]
# if no history, play random
return "rps"[random.randint(0,2)]
opponents = [opponent_random, opponent_beat_last, opponent_repeat_rs, opponent_repeat_pr, opponent_repeat_sp, opponent_repeat_rps, opponent_repeat_prs, opponent_repeat_srp, opponent_copy_last, opponent_antibeat_last]