简体   繁体   English

如何让这个输入在 Python 中继承的 class 内部工作?

[英]How do I get this input to work inside of an inherited class in Python?

So, to preface this I'm self learning python and I'm trying to build a Tic-Tac-Toe game using the command line as an interface.因此,作为序言,我正在自学 python 并且我正在尝试使用命令行作为界面构建井字游戏。 The issue that I have is that I can't get the input inside one of the inherited class for the player to work (so the game itself doesn't work aside from the 3x3 board showing up on the command line)我遇到的问题是我无法在继承的 class 之一中获取输入以供玩家工作(因此除了命令行上显示的 3x3 板外,游戏本身无法工作)

The section of code that I'm having issues with goes as such:我遇到问题的代码部分如下:

class HumanPlayer(Player):
    def __init__(self, letter):
        super().__init__(letter)

    def get_move(self, game):
        valid_square = False
        val = None
        while not valid_square:
            square = input(self.letter + ' s turn. Input move (0-9):')
            # we are going to check that this is a correct value by trying to cast
            # it into an integer, and if it's not, then we will say its invalid
            # if that spot is not available on the board, then we also say it's invalid
            try:
                val = int(square)
                if val not in game.available_moves:
                     raise ValueError
                valid_square = True # if these are successful, then oh yeah!
            except ValueError:
                print ('Invalid Square. Try Again. ')
                return val

I've tried to make sure that my spacing is correct within this class, but now I'm not sure what else to do.我试图确保我的间距在这个 class 中是正确的,但现在我不确定还能做什么。 Any help, suggestions, or the like would be appreciated since I'm learning to program in general任何帮助、建议等将不胜感激,因为我正在学习编程

Thanks!谢谢!

Although there is nothing wrong with an object-oriented approach, and it can be the (or at least a ) right approach for many problems, it looks like your program has "classes because of classes".尽管面向对象的方法没有任何问题,并且它可以是(或至少)解决许多问题的正确方法,但看起来您的程序具有“由于类而产生的类”。 It's probably easier if you don't bother with the object-orientation too much at this stage and focus on the main gameplay loop.如果您在此阶段不过多关注面向对象并专注于主要的游戏循环,这可能会更容易。

Try to imagine how the game should progress: you start the game, you make a move, another player makes a move, this continues until the game decides either play has won and then perhaps you can start a new game.试着想象游戏应该如何进行:你开始游戏,你移动,另一个玩家移动,一直持续到游戏决定任何一方获胜,然后也许你可以开始新的游戏。 And the other player might be an "AI" (tic tac toe doesn't require much intelligence) or another live player.另一个玩家可能是“AI”(井字游戏不需要太多智能)或另一个现场玩家。

Your code covers what needs to happen for a single player to enter a valid square and it appears you have another class somewhere that's a Game , an instance of which has an attribute available_moves that contains all the currently valid moves.您的代码涵盖了单个玩家进入有效方格所需发生的事情,并且您似乎在某处有另一个 class ,它是一个Game ,它的一个实例具有一个包含所有当前有效动作的属性available_moves Or perhaps that Game class is the next thing you plan to write.或者也许Game class 是您计划编写的下一个东西。

The main game loop would be to ask players for a move in alternating fashion, update the game board, decide if someone has won yet, and keep doing that.主要的游戏循环是以交替方式询问玩家移动,更新游戏板,决定是否有人赢了,然后继续这样做。 And you'll need to get the whole thing started, some core routine that sets up the game and the players and gets the ball rolling.而且你需要让整个事情开始,一些核心程序来设置比赛和球员并让球滚动。

If you have a more specific problem getting all that to work, you should post a question about that - but without a more specific problem, it's hard to provide a better answer.如果您有一个更具体的问题来解决所有问题,您应该发布一个关于该问题的问题 - 但如果没有更具体的问题,很难提供更好的答案。

Where is the code for th th的代码在哪里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM