Skip to content

Commit 146a4ae

Browse files
authored
Add files via upload
1 parent 7e07bbb commit 146a4ae

File tree

2 files changed

+688
-0
lines changed

2 files changed

+688
-0
lines changed

extra/aiplayer.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import rpstournament
2+
import random
3+
4+
#Your class must be called GamePlayer, and it must have these functions
5+
class GamePlayer:
6+
#Give your AI a name. You can put your name in it or not, depending on whether you want to be anonymous.
7+
#You can submit multiple AI's with different names for the tournament
8+
name = "Barrett"
9+
last3 = []
10+
allchoices = ["rock", "paper", "scissors"]
11+
losecount = 0
12+
prev_user = ""
13+
14+
def get_win_choice(choice):
15+
if choice == "rock" : return "paper"
16+
if choice == "paper" : return "scissors"
17+
else : return "rock"
18+
19+
def make_choice(self):
20+
##check patterns
21+
if len(self.last3) == 3:
22+
choice = self.check_for_patterns()
23+
if choice != "no pattern":
24+
self.prev_user = choice
25+
return choice
26+
27+
##if no patterns, go back to basic tactics
28+
if self.losecount >= 3:
29+
if self.losecount > 3:
30+
self.losecount = 0
31+
32+
else:
33+
return self.predictive_guess()
34+
35+
return self.probability_guess()
36+
37+
def view_opponent_choice(self, opponent_choice):
38+
##count loses
39+
if self.prev_user != "" and opponent_choice != self.prev_user:
40+
if opponent_choice == "rock" and self.prev_user != "paper":
41+
self.losecount += 1
42+
elif opponent_choice == "paper" and self.prev_user != "scissors":
43+
self.losecount += 1
44+
elif opponent_choice == "scissors" and self.prev_user != "rock":
45+
self.losecount += 1
46+
47+
##track opponents moves
48+
self.allchoices.append(opponent_choice)
49+
self.last3.append(opponent_choice)
50+
51+
##update opponents moves as
52+
##more moves come in
53+
if len(self.last3) > 3:
54+
del self.last3[0]
55+
56+
def check_for_patterns(self):
57+
##check all common patterns
58+
if ["scissors", "scissors", "scissors"] == self.last3:
59+
return "rock"
60+
elif ["rock", "rock", "rock"] == self.last3:
61+
return "paper"
62+
elif ["paper", "paper", "paper"] == self.last3:
63+
return "scissors"
64+
65+
else:
66+
##if not common, guess next move
67+
##in pattern cycle and find win move
68+
if "rock" in self.last3 and "paper" in self.last3 and "scissors" in self.last3:
69+
return GamePlayer.get_win_choice(self.last3[0])
70+
71+
return "no pattern"
72+
73+
def predictive_guess(self):
74+
##pick move to force win or lose
75+
new_choices = [i for i in self.allchoices if i != self.last3[0]]
76+
self.prev_user = random.choice(new_choices)
77+
78+
return self.prev_user
79+
80+
def probability_guess(self):
81+
##pick random move from all of opp. choices
82+
self.prev_user = random.choice(self.allchoices)
83+
84+
return self.prev_user
85+
86+
def new_player(self):
87+
self.losecount = 0
88+
self.prev_user = ""
89+
self.allchoices = ["rock", "paper", "scissors"]
90+
self.last3.clear()
91+
return
92+
93+
##run against other bots
94+
tournament_manager = rpstournament.TournamentManager()
95+
example_game_player = GamePlayer()
96+
tournament_manager.run_tournament(example_game_player)
97+

0 commit comments

Comments
 (0)