0

This was a Rock scissors paper game code ,its a humanplayer versus a reflectplayer. Reflectplayer who copy the last time humanplayer move and show the move. I tried to access the move2 from the play_round to ReflectPlayer . But How? Isn't game1.play_round.move2 work?

import random

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

class Player:
    def move(self):
        return 'rock'

    def learn(self, my_move, their_move):
        pass

class RandomPlayer:
    def move(self):
        all = ["rock","scissors","paper"]
        ranint = random.randint(0,2)
        return all[ranint]

    def learn(self, my_move, their_move):
        pass

class HumanPlayer:
    def move(self):
        ans = input("Rock Paper or Scissors?").lower()
        return ans

    def learn(self,my_move,their_move):
        pass

class ReflectPlayer:
    def move(self):
        i = 1
        RSP = ["rock","paper","scissors"]

        print(self.move2)   
        if i == 1:
            i += 1
            return RSP[random.randint(0,2)]
        elif game1.play_round.move2 == RSP[0]:
            return RSP[0]
        elif game1.play_round.move2 == RSP[1]:
            return RSP[1]
        elif game1.play_round.move2 == RSP[2]:
            return RSP[2]
        else:
            print("Wrong input !")
            pass
    def learn(self,my_move,their_move):
        pass



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


class Game:
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2

    def play_round(self):
        move1 = self.p1.move()
        move2 = self.p2.move()
        print(f"Player 1: {move1}  Player 2: {move2}")
        if beats(move1 , move2) == True:
            print(f"This Round  : Player 1 WIN")
        elif beats(move2 , move1) == True:
               print(f"This Round  : Player 2 WIN")
        elif move1 == move2:
               print("This Round : TIE")    
        else:
            print("Wrong Input !")
        self.p1.learn(move1, move2)
        self.p2.learn(move2, move1)

    def play_game(self):
        print("Game start!")
        for round in range(3):
            print(f"Round {round}:")
            self.play_round()
        print("Game over!")

if __name__ == '__main__':
    game1 = Game(ReflectPlayer(),HumanPlayer())
    game1.play_game()
user126004
  • 3
  • 1
  • 5

1 Answers1

0

Consider changing your implementation to pass the last moves played to the move() method. If your player needs to use the previous moves then you can access them easily. If not just discard them. I am assuming that you will have other types of players that can benefit from this.

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

class ReflectPlayer:

    def move(self, lastOpponentMove):
        if lastOpponentMove == None:
            return rsp[random.randint(0,2)]
        elif lastOpponentMove == rsp[0]:
            return rsp[0]
        elif lastOpponentMove == rsp[1]:
            return rsp[1]
        elif lastOpponentMove == rsp[2]:
            return rsp[2]
        else:
            print("Wrong input !")
            pass
    def learn(self,my_move,their_move):
        pass

Possible adaption on the Game class:

class Game:
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2
        self.lastP1Move = None
        self.lastP2Move = None

    def play_round(self):
        move1 = self.p1.move(lastP2Move)
        move2 = self.p2.move(lastP1Move)

        lastP1Move = move1
        lastP2Move = move2

        print(f"Player 1: {move1}  Player 2: {move2}")
        if beats(move1 , move2) == True:
            print(f"This Round  : Player 1 WIN")
        elif beats(move2 , move1) == True:
            print(f"This Round  : Player 2 WIN")
        elif move1 == move2:
            print("This Round : TIE")    
        else:
            print("Wrong Input !")
        self.p1.learn(move1, move2)
        self.p2.learn(move2, move1)

Some notes:

  • You defined rsp as global in the beginning so you can use that list instead of creating new ones.
  • The way you implemented the check to see if it was the first play would make it so that i would be 1 everytime. (I changed to see if you are receiving a None last move).
  • Check your indentation in the play_round method

Hope this helps