In this exercise, you're going to implement a program that determines the state of a tic-tac-toe game. (You may also know the game as "noughts and crosses" or "Xs and Os".)
The game is played on a 3×3 grid.
Players take turns to place Xs and Os on the grid.
The game ends when one player has won by placing three of marks in a row, column, or along a diagonal of the grid, or when the entire grid is filled up.
In this exercise, we will assume that X starts.
It's your job to determine which state a given game is in.
There are 3 potential game states:
If the given board is invalid, throw an appropriate error.
If a board meets the following conditions, it is invalid:
X starts). | |
X | |
___|___|___
| |
| X | O
___|___|___
| |
O | X |
| |
| |
X | O | X
___|___|___
| |
X | X | O
___|___|___
| |
O | X | O
| |
| |
X | X | X
___|___|___
| |
| O | O
___|___|___
| |
| |
| |
| |
O | O | X
___|___|___
| |
| |
___|___|___
| |
| |
| |
| |
X | X | X
___|___|___
| |
O | O | O
___|___|___
| |
| |
| |
Sometimes it is necessary to raise an exception. When you do this, you should always include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the built in error types, but should still include a meaningful message.
A valid game of Tic Tac Toe ends when one player wins.
If both players are in a winning state, this exercise expects you to use the raise statement and "throw" a ValueError in your solution.
Your code is also expected to throw a ValueError if one player is assessed as playing two turns in a row, or playing the first move when they were assigned to play the second move.
To raise a ValueError with a message, write the message as an argument to the exception type:
# Example when player O goes before player X.
raise ValueError("Wrong turn order: O started")
# Example when player X goes twice.
raise ValueError("Wrong turn order: X went twice")
# Example when player X wins AND player O wins.
raise ValueError("Impossible board: game should have ended after the game was won")
The tests will check for an exact error message match, so please read the expected results carefully.
Sign up to Exercism to learn and master Python with 17 concepts146 exercises, and real human mentoring, all for free.