首页 文章

我喜欢在python中创建子类,一个用户先前输入,另一个循环一系列选择

提问于
浏览
0

我试图用Python3编写一个石头剪刀游戏 . 我提供了一些入门代码,并试图用代码完成一些额外的事情 . 比如提供不同的玩家类别,宣布胜利者,保持得分等等 . 我似乎找到了以这种方式编写的代码的任何好例子 . 除此之外我基本上都把它全部放下了,除了我需要创建其他玩家类类型的两件事 . 该游戏进行了3轮比赛并在最后宣布了胜利者 .
1)我正在尝试在一个玩家类中编码,该玩家类记住用户在上一轮中输入的内容并将其作为他们选择的摇滚,纸张或剪刀进行播放 .
2)记住上一轮比赛的动作,并循环不同的动作 . 就像它玩摇滚一样,然后会玩纸,然后是剪刀 .
3)(这个试图弄清楚如何继续提示玩家正确拼写他们的选择,因为此时它只是提示你一次,如果你再次错误拼写,它只需要输入"none"

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

    import random

    class Player:
        score = 0
        def move(self):
            return 'rock'

        def learn(self, my_move, their_move):
            pass

    #    def move(self):
    #        return 'paper'

    #    def learn(self, my_move, their_move):
    #        pass

    #    def move(self):
    #        return 'scissors'

    #    def learn(self, my_move, their_move):
    #        pass

    class RandomPlayer(Player):
        def __init__(self):
            Player.__init__(self)

        def move (self):
            return random.choice(moves)

    class HumanPlayer(Player):
        def __init__(self):
            Player.__init__(self)

        def move (self):
            x = input("choose rock, paper or scissors:")
            if x not in (moves):
                x = input("check your spelling and try again:")
                return(x)
            else:
                return(x)

    #class ReflectPlayer(Player):
    #    def __init__(self):
    #        Player.__init__(self)
    #       
    #    def move (self): 

    #class CyclePlayer(Player):
    #    def __init__(self):
    #        Player.__init__(self)
    #       
    #    def move (self): 

    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}")
            self.p1.learn(move1, move2)
            self.p2.learn(move2, move1)
            if beats(move1, move2):
                print("player 1 wins this round")
                self.p1.score += 1
            elif beats(move2, move1):
                print("player 2 wins this round")
                self.p2.score += 1   
            else:
                print("A Tie!")
            print(f"Scores, Player 1: {self.p1.score} Player 2:      
    {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!")

            if self.p1.score > self.p2.score:
                print ("Player 1 Wins the Game!")
            elif self.p2.score > self.p1.score:
                print ("Player 2 Wins the Game!")
            else:
            print("a TIE!? it must be settled with a FIGHT TO THE DEATH!")          



    if __name__ == '__main__':
        game = Game(HumanPlayer(), RandomPlayer())
        game.play_game()

1 回答

  • 0

    您可以存储人类玩家的移动频率,并相应地移动 . 一个简单的方法是根据人类移动频率的 max 进行移动 .

    class RandomPlayer(Player):
        def __init__(self):
            Player.__init__(self)
            self.human_player_history = {}  # stores the frequency of human player moves
            for move in moves:
                self.human_player_history[move] = 0
    
        def move(self):
            max_move = max(self.human_player_history.items(), key=operator.itemgetter(1))[0]
            if max_move == 'rock':
                return 'paper'
            if max_move == 'scissors':
                return 'rock'
            if max_move == 'paper':
                return 'rock'
    
        def learn(self, my_move, their_move):
            self.human_player_history[their_move] += 1
    

相关问题