Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions rock_paper_scissors/league/monty2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,52 @@

class Player(BasePlayer):

player_name = 'Monty2'
player_name = 'Monty 2'
author = 'Leonard B'
x_beats_y = {ROCK: SCISSORS, PAPER: ROCK, SCISSORS: PAPER}
x_beaten_by_y = {SCISSORS: ROCK, ROCK: PAPER, PAPER: SCISSORS}

def __init__(self):
self.history = []
self.current_choice = None
self.counts = {ROCK: 0, PAPER: 0, SCISSORS: 0}
self.round = 1

def is_win(self, my_play, other_play):
for key, value in self.x_beats_y.items():
if key == my_play and value == other_play:
return True
return False
self.round_number = 1
self.reflect_count = 0
self.is_reflected = False
self.last_choice = None
self.my_history = []
self.current_choice = None

def play(self):
"""
Must return a single var for the decided play:
either 'ROCK', 'PAPER', 'SCISSORS'
"""
if self.round == 1:
self.current_choice = random.choice([ROCK, PAPER, SCISSORS])
elif not self.is_win(self.current_choice, self.history[-1]):
if self.round_number == 1:
self.current_choice = random.choice([ROCK, PAPER, SCISSORS])
else:
random_number = random.randint(1, self.round)
if self.is_reflected:
self.current_choice = self.beat(self.current_choice)
self.last_choice = self.current_choice
return self.current_choice
random_number = random.randint(1, self.round_number)
if random_number <= self.counts[ROCK]:
self.current_choice = PAPER
elif random_number <= self.counts[ROCK] + self.counts[PAPER]:
self.current_choice = SCISSORS
else:
self.current_choice = ROCK
self.last_choice = self.current_choice
return self.current_choice

def result(self, their_play):
self.history.append((self.current_choice, their_play))
if self.round_number == 6:
if self.reflect_count >= 4:
self.is_reflected = True
if self.round_number > 1 and their_play == self.my_history[-1]:
self.reflect_count += 1
self.my_history.append(self.current_choice)
self.history.append(their_play)
self.counts[their_play] += 1
self.round += 1
self.round_number += 1

def beat(self, play):
return self.x_beaten_by_y[play]