From 55c5f4a0c81a52c76a213f44736175a8a86b23b6 Mon Sep 17 00:00:00 2001 From: Leonard Buckley Date: Thu, 28 Sep 2017 18:22:24 +1000 Subject: [PATCH 1/2] Monty v2 improved --- rock_paper_scissors/league/monty2.py | 42 +++++++++++++++++----------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/rock_paper_scissors/league/monty2.py b/rock_paper_scissors/league/monty2.py index cc85a0b..71f5f08 100644 --- a/rock_paper_scissors/league/monty2.py +++ b/rock_paper_scissors/league/monty2.py @@ -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 = [] 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.my_history.append(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] From caa5d16ae358f84e61e8b648c55101edd6e4e1d3 Mon Sep 17 00:00:00 2001 From: Leonard Buckley Date: Thu, 28 Sep 2017 18:31:08 +1000 Subject: [PATCH 2/2] Monty v2 improved --- rock_paper_scissors/league/monty2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rock_paper_scissors/league/monty2.py b/rock_paper_scissors/league/monty2.py index 71f5f08..531bf7c 100644 --- a/rock_paper_scissors/league/monty2.py +++ b/rock_paper_scissors/league/monty2.py @@ -16,6 +16,7 @@ def __init__(self): self.is_reflected = False self.last_choice = None self.my_history = [] + self.current_choice = None def play(self): """ @@ -27,7 +28,6 @@ def play(self): else: if self.is_reflected: self.current_choice = self.beat(self.current_choice) - self.my_history.append(self.current_choice) self.last_choice = self.current_choice return self.current_choice random_number = random.randint(1, self.round_number)