Simple Tic-Tac-Toe with Python. Stage 5/5

Fight!

Report a typo

Description

Our game is almost ready! Now let's combine what we’ve learned in the previous stages to make a game of tic-tac-toe that two players can play from the beginning (with an empty grid) through to the end (until there is a draw, or one of the players wins).

The first player has to play as X and their opponent plays as O.

Objectives

In this stage, you should write a program that:

  1. Prints an empty grid at the beginning of the game.

  2. Creates a game loop where the program asks the user to enter the cell coordinates, analyzes the move for correctness and shows a grid with the changes if everything is okay.

  3. Ends the game when someone wins or there is a draw.

You need to output the final result at the end of the game. Good luck!

Note: In Python, the all() method is a built-in function that checks if all elements in an iterable (such as a list, tuple, or set) are true. It returns True if every element is true (or if the iterable is empty); otherwise, it returns False.

Example

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

Example 1.

---------
|       |
|       |
|       |
---------
> 2 2
---------
|       |
|   X   |
|       |
---------
> 2 2
This cell is occupied! Choose another one!
> two two
You should enter numbers!
> 1 4
Coordinates should be from 1 to 3!
> 1 1
---------
| O     |
|   X   |
|       |
---------
> 3 3
---------
| O     |
|   X   |
|     X |
---------
> 2 1
---------
| O     |
| O X   |
|     X |
---------
> 3 1
---------
| O     |
| O X   |
| X   X |
---------
> 2 3
---------
| O     |
| O X O |
| X   X |
---------
> 3 2
---------
| O     |
| O X O |
| X X X |
---------
X wins

Write a program
# write your code here
___

Create a free account to access the full topic