citizenfive
citizenfive

Reputation: 9

Python - Reoccurring Bug in my Rock, Paper, Scissors code

I am currently working on developing a python program for school regarding a paper, scissors, rock game. I have written the program and can run it, but I am noticing that when playing different hands, I'm still getting Tie result on some.

The mentors have given me feedback regarding this: "For your project to be complete, you still need to implement the "learn()" method in the ReflectPlayer and CyclePlayer classes.

Note: According to the specification, you must implement this method in the classes where it is necessary to remember the last move (my_move in CyclePlayer and their_move in ReflectPlayer).

Specification:

Some computer players don't need to remember anything, so their remembering method should do nothing. In the Player class, you must leave only the method signature.

def learn(self, my_move, their_move):
pass

enter image description here

import random
import colorama
from colorama import Fore 

colorama.init()
print(Fore.GREEN)

"""This program plays a game of Rock, Paper, Scissors between two Players,
and reports both Player's scores each round."""

moves = ['rock', 'paper', 'scissors']

"""The Player class is the parent class for all of the Players
in this game"""

class Player:
    moves = ['rock', 'paper', 'scissors']
    def __init__(self, name):
        self.score = 0
        self.name = name
        self.my_move = 'init'
        self.ai = random.choice(self.moves)
        self.human = self.moves

    def learn(self, my_move, their_move):
        self.human = my_move
        self.ai = their_move

    def move(self):
        return 'rock' 

def beats(one, two):
    return ((one == 'rock' and two == 'scissors') or
            (one == 'scissors' and two == 'paper') or
            (one == 'paper' and two == 'rock'))

class HumanPlayer(Player):
    def move(self):
        valid_input = False
        while not valid_input:
            humanmove = input('Rock, Paper or Scissors? -- ')
            if humanmove.lower() not in moves:
                print(">>>Try again!<<<\n")
            else:
                return humanmove
        return valid_input

class RandomPlayer(Player):
    def move(self):
        return random.choice(moves)

class CyclePlayer(Player):
    def move(self):
        if self.human == self.moves[0]:
            return self.moves[1]
        elif self.human == self.moves[1]:
            return self.moves[2]
        else:
            return self.moves[0]

class ReflectPlayer(Player):
    def move(self):
        return self.ai

class Game:
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2
        self.winner = 'init'
    def play_round(self):
        self.p1.human = self.p1.move()
        self.p2.human = self.p2.move()
        print(f"Agent Smith: {self.p1.human} - Neo: {self.p2.human}")
        self.p1.learn(self.p1.human, self.p2.human)
        self.p2.learn(self.p2.human, self.p1.human)
        self.conclude_round_winner()

    def conclude_round_winner(self):
        if (beats(self.p1.human, self.p2.human)):
            self.p1.score += 1
            print(f'{self.p1.name} is the Winner!')
        elif (beats(self.p2.human, self.p1.human)):
            self.p2.score += 1
            print(f'{self.p2.name} is the Winner!')
        else:
            print("Tie!")
            
    def conclude_game_winner(self):
        if (self.p1.score > self.p2.score):
            self.winner = self.p1
        elif (self.p2.score > self.p1.score):
            self.winner = self.p2

    def play_game(self):
        print("Hello Neo... want to enter The Matrix? All you need to do is beat Agent Smith in Rock, Paper, Scissors...")
        for round in range(1, 5):
            print(f'Score: Neo: {self.p2.score} | Agent Smith: {self.p1.score} \nRound {round} Begin:')
            self.play_round()
        self.conclude_game_winner()
        if (self.winner != 'init'):
            win_text = f'The Simulation is Over, {self.winner.name} is your Winner!\n'
            print(win_text)
        else:
            win_text = 'Draw!'
            print('Draw!')

if __name__ == '__main__':
    game = Game(CyclePlayer('Agent Smith'), HumanPlayer('Neo'))
    game.play_game()

enter image description here

Upvotes: 0

Views: 100

Answers (1)

furas
furas

Reputation: 142651

In HumanPlayer.move() you return humanmove but you should return humanmove.lower().

OR you should run humanmove = input(...).lower()

Upvotes: 1

Related Questions