1
votes

I am developing a rock paper scissors on python and I am stuck. I made a class that cycles between (rock, paper, and scissors ), I want the computer to know it's previous play. for instance ( first round computer played rock, in the next round it should play paper)
but I don't know how to call the learn function to make it work

class Player:
def __init__(self):
    self.score = 0

def move(self):
    return 'rock'

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

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 = input("Pick something!\n")
    move2 = self.p2.move()
    print(f"Player 1: {move1}  Player 2: {move2}")
    self.p1.learn(move1, move2)
    self.p2.learn(move2, move1)
    if beats(move1, move2):
        self.p1.score += 1
        print ("You win")
        print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score) ) 
    elif beats(move2,move1):
        self.p2.score += 1
        print ("Computer wins")
        print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score) ) 
    else:
        print ("Draw")
        print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score) )

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

class human_player(Player):
def move(self):
   return input("Pick something!\n")

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

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

class cycleplayer(Player):
def move(self):
    # calling my_move from the Player class
    if self.learn.my_move == "rock" or "paper" or "scissors" :
        return 'rock'

    elif  self.their_move == 'rock':
        return "paper"   

    elif self.their_move == 'paper':
        return "scissors"  

    elif self.their_move == 'scissors':
        return "rock"

if HumanPlayer_choice == "cycle" :
game = Game(Player(), cycleplayer())
game.play_game()

This is the error I am getting.

Exception has occurred: AttributeError 'function' object has no attribute 'my_move'

I know that I need to utilize the init function with the learn function to make it work but I not sure how.

2
Please fix the indentation first, the code you posted is not syntactically valid. - Martin Frodl
Fix the indentation please - Ayxan Haqverdili
Correct indentations of the code otherwise it is just useless. - Rarblack
On which line does this error occur? Please add the entire traceback - Ayxan Haqverdili
This is the cause of the AttributeError: self.learn.my_move. self.learn is class method and you have nowhere a class method my_move. - stovfl

2 Answers

3
votes

The problem is on this line:

if self.learn.my_move == "rock" or "paper" or "scissors" :

learn function does not have an attribute named my_move. What you meant to do is probably

if self.my_move == "rock" or self.my_move == "paper" or self.my_move == "scissors" :

Note that you have to add self.my_move == before "paper" and "scissors"; otherwise it is evaluated like:

if (self.my_move == "rock") or ("paper") or ("scissors"):

and since non-empty strings are always evaluated as True, this if case is rendered useless.


As @DmitryErohin mentioned, there is a better way of achieving that without repeating yourself:

if (self.my_move in ("rock", "paper", "scissors")):

This is much less verbose and a lot more readable

0
votes

There were a few problems in your code as far as I could follow. Take a look at the example below based on your code. There's some logic I could not follow so it may not behave exactly as you want. Suggestions:

  • as you just want to store the previous move for the cycle_player move, store it in the cycle object (instance) not on the Player
  • as the move method varies make it's implementation specific to a child class (e.g. human_player, random_player, cycle_player)
  • some methods are better as static rather than recreated in each instance methods

    import random
    
    class Player:
        def __init__(self):
            self.score = 0
    
    class Game:
        def __init__(self, p1, p2):
            self.p1 = p1
            self.p2 = p2
    
        @staticmethod
        def check_result(a, b): #Return win, draw, loose for a over b
            if (a == b):
                return 'draw'
            if (a == 'rock' and b == 'scissors') or (a == 'scissors' and b == 'paper') or (a == 'paper' and b == 'rock'):
                return 'win'
            return 'loose'
    
        def play_round(self):
            #move1 = input("Pick something!\n")
            move1 = self.p1.move()
            move2 = self.p2.move()
            print(f"Player 1: {move1}  Player 2: {move2}")
            result = Game.check_result(move1, move2) # a over b
            if result == 'win':
                self.p1.score += 1
                print ("You win")
            elif result == 'loose':
                self.p2.score += 1
                print ("Computer wins")
            else:
                print ("Draw")
            print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score))
    
        def play_game(self):
            print("Game start!")
            for round in range(3):
                print(f"Round {round + 1}:")
                self.play_round()
            print("Game over!")
    
    class human_player(Player):
        def move(self):
            return input("Pick something!\n") #TODO validate input
    
    class random_player(Player): #Option in game for computer to make random moves
        def move(self):
            return random.choice(["scissors","paper","rock"])
    
    class repeat_player(Player): #Option
        def move(self):
            return 'rock'
    
    class cycle_player(Player):
        def __init__(self):
            self.previous_move = None
            super().__init__()
    
        def move(self):
            # calling my_move from the Player class
            if self.previous_move is None :
                next_move = 'rock'
            elif  self.previous_move == 'rock':
                next_move = "paper"
            elif self.previous_move == 'paper':
                next_move = "scissors"  
            elif self.previous_move == 'scissors':
                next_move = "rock"
            self.previous_move = next_move
            return next_move
    
    #game = Game(human_player(), random_player())
    #game = Game(human_player(), repeat_player())
    game = Game(human_player(), cycle_player())
    game.play_game()