Python如何访问类函数变量

Python如何访问类函数变量,python,class,variables,Python,Class,Variables,这是一个石头剪刀布游戏代码,它是一个人类玩家对一个反射玩家Reflectplayer谁复制上次humanplayer移动并显示移动。我试图从play\u round到ReflectPlayer访问move2。但是怎么做呢?游戏1。玩一轮。移动2不是工作吗 import random moves = ['rock', 'paper', 'scissors'] class Player: def move(self): return 'rock' def lea

这是一个石头剪刀布游戏代码,它是一个
人类玩家
对一个
反射玩家
Reflectplayer
谁复制上次
humanplayer
移动并显示移动。我试图从
play\u round
ReflectPlayer
访问
move2
。但是怎么做呢?游戏1。玩一轮。移动2不是工作吗

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()

考虑更改实现以将最后播放的移动传递给move()方法。如果您的玩家需要使用之前的移动,那么您可以轻松访问它们。如果不是,就丢弃它们。我假设你会有其他类型的球员可以从中受益

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
可能对游戏类进行的调整:

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)
一些注意事项:

  • 您在开始时将rsp定义为全局,因此可以使用该列表而不是创建新列表
  • 你实施检查的方式,看看这是否是第一个发挥将使它,使我每次都是1。(我换了一下,看看你是否收到了最后一个动作)
  • 在play\u round方法中检查缩进

希望这有帮助

如果您使用正确的IDE,添加类型提示将对您有所帮助。
game1.play\u game()
不会返回任何内容。因此它返回
None
None
没有
move2
属性。在
t[random.randint(0,2)]
t
是什么RSP@Tom哦,那应该是什么?要获得
move2
Cascais,非常感谢!