Tic Tac Toe Game in Python for Beginners
Published in
6 min readAug 6, 2022
In programming, the best way to learn is to practice. I will give you a chance of practicing through a little exercise: a Tic Tac Toe game.
Not a Medium subscriber? Read for free here!
Prerequisites
To complete this exercise, you need to know Python basics such as lists, conditions, loops, and functions.
Statement
- We’re building a console Tic Tac Toe game playable against a computer
- The board is a 3x3 grid numbered from 1 to 9 so that we can ask the player a cell to play by number.
- There is no A.I., the computer is playing random.
Tips
- We can modelize the board with a list of characters which can be either “X”, “O” or an empty string.
- To generate random numbers, we import Python’s
random
package and we use therandint(a, b)
function which generates an int between a and b.
import randomrandom.randint(1, 10)
# 3
- The game should be played this way :
- Board drawing.
- The player is asked which square he wants to play.
- Update the board according to the chosen square.