Description
In this stage, we're going to analyze the game state to determine if either player has already won the game or it is still ongoing, if the game is a draw, or if the user has entered an impossible game state (two winners, or with one player having made too many moves).
Objectives
In this stage, your program should:
Take a string entered by the user and print the game grid as in the previous stage.
Analyze the game state and print the result. Possible states:
Game not finishedwhen neither side has three in a row but the grid still has empty cells.Drawwhen no side has a three in a row and the grid has no empty cells.X winswhen the grid has three X's in a row (including diagonals).O winswhen the grid has three O's in a row (including diagonals).Impossiblewhen the grid has three X's in a row as well as three O's in a row, or there are a lot more X's than O's or vice versa (the difference should be 1 or 0; if the difference is 2 or more, then the game state is impossible).
In this stage, we will assume that either X or O can start the game.
You can choose whether to use a space or underscore _ to print empty cells.
Note: List comprehensions in Python offer a concise way to create lists by iterating over sequences and applying conditions or transformations in a single line of code. They improve readability and efficiency compared to using traditional loops, making your code more Pythonic. For example, [x ** 2 for x in range(10) if x % 2 == 0] generates a list of squares for even numbers between 0 and 9.
Examples
The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.
Example 1:
> XXXOO__O_
---------
| X X X |
| O O _ |
| _ O _ |
---------
X winsExample 2:
> XOXOXOXXO
---------
| X O X |
| O X O |
| X X O |
---------
X winsExample 3:
> XOOOXOXXO
---------
| X O O |
| O X O |
| X X O |
---------
O winsExample 4:
> XOXOOXXXO
---------
| X O X |
| O O X |
| X X O |
---------
DrawExample 5:
> XO_OOX_X_
---------
| X O |
| O O X |
| X |
---------
Game not finishedExample 6:
> XO_XO_XOX
---------
| X O _ |
| X O _ |
| X O X |
---------
ImpossibleExample 7:
> _O_X__X_X
---------
| O |
| X |
| X X |
---------
ImpossibleExample 8:
> _OOOO_X_X
---------
| O O |
| O O |
| X X |
---------
Impossible