Let the Computer Pick a Random Move
An abstract class is one that you can’t instantiate because its objects wouldn’t stand on their own. Its only purpose is to provide the skeleton for concrete subclasses. You can mark a class as abstract in Python by setting its metaclass to
abc.ABCMeta, or extending the
This is less important in languages like Python, which support multiple inheritance, but as a rule of thumb, you should favor composition over inheritance wherever possible. The player gets assigned a
Mark instance that they’ll be using during the game.
Notice how the public
.make_move() method defines a universal algorithm for making a move, but the individual step of getting the move is delegated to an abstract method, which you must implement in concrete subclasses.
Making a move entails checking if it’s the given player’s turn and whether the move exists. The
.get_move() method returns
None to indicate that no more moves are possible, and the abstract
Player class uses the walrus operator (
:=) to simplify the calling code.
02:46 To make the game feel more natural, you can introduce a short delay for the computer player to wait before choosing their move. Otherwise, the computer would make its move instantly, unlike a human player. You can define another, slightly more specific abstract base class to represent computer players.
04:04 Having an abstract computer player data type enforces a uniform interface, which you can conveniently satisfy with a few lines of code. For example, you can implement a computer player picking moves at random in the way seen on-screen.
random.choice() to pick a random element from a list of possible moves. If there are no more moves in the given game state, then you’ll get an
IndexError because of an empty list, so you catch it and return
None instead. You now have two abstract base classes,
ComputerPlayer, as well as one concrete
RandomComputerPlayer, which you’ll be able to use in your games.
05:10 The only remaining element of the equation before you can put those classes into action is the abstract renderer, which you’ll define next. Giving the tic-tac-toe grid a visual form is entirely up to the front end, so you’ll only define an abstract interface in your library.
Become a Member to join the conversation.