简体   繁体   中英

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

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. 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.

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. Or perhaps that Game class is the next thing you plan to write.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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