Scaffold Your Game
00:00 Scaffold a Generic Tic-Tac-Toe Game Engine. At this point, you should have all the domain models defined for your tic-tac-toe library. Now it’s time to build a game engine that will take advantage of these model classes.
engine module is the centerpiece of the virtual gameplay, where you’ll implement the game’s main loop. You’ll define abstract interfaces that the game engine uses, along with a sample computer player, in the
00:41 By the end of this step, you’ll be set to write a tangible front end for the tic-tac-toe library. At the very minimum, to play a tic-tac-toe game, you need to have two players, something to draw on, and a set of rules to follow.
01:32 They’ll eventually get replaced with concrete classes, some of which may come from an externally defined front end. The player will know what move to make, and the renderer will be responsible for visualizing the grid. To play the game, you must decide which player should make the first move, or you can assume the default one, which is the player with crosses.
These steps are repeated in an infinite loop until the game is over.
GameState only knows about the current player’s mark, which can be either X or O, but it doesn’t know about the specific player objects that were assigned those marks.
Here you compare enum members by their identities using Python’s
is operator. If the current player’s mark, determined by the game state, is the same as the mark assigned to the first player, then that’s the player who should be making the next move.
Both players supplied to the
TicTacToe object should have opposite marks. Otherwise, you wouldn’t be able to play the game without violating the rules, so it’s reasonable to validate the player’s marks when instantiating the
04:32 You use the identity comparison again to check both players’ marks and prevent the game from starting when both players use the same mark. There’s one more thing that can go wrong. Because it’s up to the players, including human players, to decide what move they make, their choice could be invalid.
05:58 Having implemented an abstract tic-tac-toe game engine, you can proceed to code an artificial player. In the next section of the course, you’ll define a generic player interface and implement it with a sample computer player that makes moves at random.
Become a Member to join the conversation.