Discard Incorrect Game States
Discard Incorrect Game States. In order to reject invalid game states, you’ll implement a familiar post-initialization hook in your
GameState class that delegates the processing to another function.
01:17 Your new helper function serves as an entry point to the game state validation by calling a few subsequent functions that you’ll define presently. To prevent instantiating a game state with an incorrect number of a player’s marks in the grid, such as the one that you stumbled on earlier on, you must take the proportion of naughts to crosses into account.
02:09 Initially, there are no marks, so the number of Xs and Os is equal to zero. When the first player makes a move, they’ll have one more mark than their opponent, but as soon as the other player makes their move, the proportion evens out again, and so on.
It’s customary to have empty classes extend the built-in
Exception type in Python without specifying any methods or attributes in them. Such classes exist solely for their names, which convey enough information about the error that occurred at runtime.
The player who left more marks on the grid is guaranteed to be the starting player. If not, then you know something must have gone wrong. Because of the way
Mark was defined, you can directly compare the starting player’s mark to a string literal.
04:28 A starting player has an advantage, so when they win, they’ll have left more marks than their opponent. Conversely, the second player is at a disadvantage so they can only win the game by making an equal number of moves as the starting player.
05:20 You are almost done with encapsulating the tic-tac-toe game’s rules in Python code, but there’s still one more important piece missing. In the next video, you’ll write code to systematically produce new game states by simulating players’ moves.
Become a Member to join the conversation.