Simulate Moves by Producing New Game States
00:00
Simulate Moves by Producing New Game States. The last property that you’ll add to your GameState
class is a fixed list of possible moves, which you can find by filling the remaining empty cells in the grid with the current player’s mark.
00:27
If the game is not over, you identify the locations of empty cells using a regular expression, and then make a move to each of those cells. Making a move creates a new Move
object, which you append to the list without mutating the game state.
00:46
If the game is over, you return an empty list of moves. This is how you construct a Move
object.
01:15
A move isn’t allowed if a target cell is already occupied by either your or your opponent’s mark, in which case you raise an InvalidMoveException
.
01:25 On the other hand, if the cell is empty, then you take a snapshot of the current player’s mark, the target cell’s index, and the current game state while synthesizing the following state.
02:08 Don’t forget to define the new exception type that you imported.
02:20 And that’s it. You’ve now got a solid domain model of the tic-tac-toe game, which you can use to build interactive games for various front ends. The model encapsulates the game’s rules and enforces its constraints.
02:36 In the next section of the course, you’ll build an abstract game engine and your first artificial player.
Become a Member to join the conversation.