Tic-Tac-Toe in Python
In this article, we will learn how to build Tic-Tac-Toe in Python. Where two players take turns and enter their input(X or O), and the winner is displayed based on the rules in a Tic-Tac-Toe game.
You can install python from the link given below for running the codes
https://www.python.org/downloads/
Tic-Tac-Toe in Python

Tic-tac-toe is played on a three-by-three grid by two players. Who alternately place the marks X and O in one of the nine spaces in the grid.
There is no universally-agreed rule as to who plays first, but the convention that X plays first is used.
If you are not sure about the game, you can check it out here
Once you are done, let’s jump right in
THE BOARD for Tic-Tac-Toe in Python
board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
winner = None
full = False
moves = []
game = True
def display():
for i in board:
print("-"*7)
for j in i:
print("|"+j, end= "")
print("|")
print("-"*7)
Line 1: Let’s start off by defining a variable named board that has a list in it which acts like the tic-tac-toe board
Line 3,4,5: Now we define 4 variables
- winner stores the value of the player who wins the game(X or O)
- full that checks if the board is full or not so that we can exit the game when the board is full
- moves to keep track of all the moves that are made during the game
- game that stores the Boolean value that keeps the game running
Lines 8 to 14: We define a function called display() that loops through the variable board
and prints it out, and therefore when we run the display function, we get . . .
>>> display()
-------
| | | |
-------
| | | |
-------
| | | |
-------
CONDITIONS FOR WIN
# board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
# winner = None
# full = False
# moves = []
# game = True
# def display():
# for i in board:
# print("-"*7)
# for j in i:
# print("|"+j, end= "")
# print("|")
# print("-"*7)
def win(player):
global winner
b = board
hori = b[0][0]==b[0][1]==b[0][2]!= ' ' or b[1][0]==b[1][1]==b[1][2]!=' ' or b[2][0]==b[2][1]==b[2][2]!= ' '
verti = b[0][0]==b[1][0]==b[2][0]!= ' ' or b[0][1]==b[1][1]==b[2][1]!= ' ' or b[0][2]==b[1][2]==b[2][2]!= ' '
dia = b[0][0]==b[1][1]==b[2][2]!= ' ' or b[2][0]==b[1][1]==b[0][2]!= ' '
if hori or verti or dia:
winner = player
for i in board:
if " " in i:
full = False
break
else:
full = True
if full and not winner:
winner = 'draw'
Line 16: We define a function called win that takes in 1 parameter called player.
Lines 17, 18: Now we declare the variable winner to be global, and then define a variable called b that is equal to the variable board, just to make our lives a little bit more simpler
Line 19: Variable hori checks if the player wins the game horizontally
Line 20: Variable verti checks if the player wins the game vertically
Line 21: Variable dia checks if the player wins the game diagonally
Lines 22, 23: If any of these variables, hori, verti or dia returns True, this naturally means the player has won. Therefore the winner will be equal to player
Lines 24 to 29: Here, we check if there are any empty spaces left for the players, if we come through atleast 1 empty space inside the list, this would mean that the board is not full, and therefore we can assign the value of False to full and come out of the loop. If there isn’t any empty space in the list, this means that the board is full, hence the value of full becomes True
Lines 30, 31: We include another condition that says, if the board is full and there is no winner, go ahead and call the match a draw
THE GAME
# board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
# winner = None
# full = False
# moves = []
# game = True
# def display():
# for i in board:
# print("-"*7)
# for j in i:
# print("|"+j, end= "")
# print("|")
# print("-"*7)
# def win(player):
# global winner
# b = board
# hori = b[0][0]==b[0][1]==b[0][2]!= ' ' or b[1][0]==b[1][1]==b[1][2]!=' ' or b[2][0]==b[2][1]==b[2][2]!= ' '
# verti = b[0][0]==b[1][0]==b[2][0]!= ' ' or b[0][1]==b[1][1]==b[2][1]!= ' ' or b[0][2]==b[1][2]==b[2][2]!= ' '
# dia = b[0][0]==b[1][1]==b[2][2]!= ' ' or b[2][0]==b[1][1]==b[0][2]!= ' '
# if hori or verti or dia:
# winner = player
# for i in board:
# if " " in i:
# full = False
# break
# else:
# full = True
# if full and not winner:
# winner = 'draw'
player = 'X'
while game:
move = input(f"Player ({player}) (Eg:12) : ")
if move not in moves:
moves.append(move)
else:
print("Already Occupied!")
continue
try:
x,y = int(move[0]), int(move[1])
except:
print("Enter the Coordinates properly")
continue
board[2-y][x] = f"{player}"
display()
win(player)
if winner == player:
print(f"{player} wins!")
game = False
elif winner == "draw":
print("It's a Tie")
game = False
if player=='X':
player = 'O'
else:
player = 'X'
Line 35: Let’s now create a variable named player, that stores either ‘X’ or ‘O’ depending upon who’s turn it is. Since X is supposed to start the game, player stores the value ‘X’ outside the loop
Line 36: We create a while loop that initiates the game. The loop runs, as long as game is equal to True
Line 37: We declare a variable that stores all the moves. The player will be prompted to enter the coordinates of the board that they wanted to be marked as X or O, depending on their turn. The coordinates we use for the game are based on the image below

Lines 38 to 42: With the coordinates in our hands, the first thing we have to check is whether the coordinates the player wants mark is already occupied or not. We can do this by storing all the moves inside the variable moves. If it is occupied, we print “Already Occupied!” and continue, if not, we append the coordinates to moves
Lines 43 to 47: Here, we try to store the x-coordinate and y-coordinate, inside the variables x and y. If the coordinates entered by the player was not valid, we print "Enter the Coordinates properly"
Line 48: Here we find the index of the list board with respect to the coordinates entered by the player. And mark it as X or O depending upon who’s playing
Line 50: After the coordinates are verified and stored in the variables, now we display the board by calling the function display()
Line 51: And then we check if the player has won the game or not by calling the function win()
Lines 52 to 57: We check if the value of winner, the global variable we assigned a while ago, and if it is equal to player, we announce the winner and exit the game, by assigning the value of False to game
Lines 58 to 61: Using the if condition, we switch the players.
And with this, we have completed our code for Tic-Tac-Toe in Python. Let’s now play the game
PLAYING THE GAME
board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
winner = None
full = False
moves = []
game = True
def display():
for i in board:
print("-"*7)
for j in i:
print("|"+j, end= "")
print("|")
print("-"*7)
def win(player):
global winner
b = board
hori = b[0][0]==b[0][1]==b[0][2]!= ' ' or b[1][0]==b[1][1]==b[1][2]!=' ' or b[2][0]==b[2][1]==b[2][2]!= ' '
verti = b[0][0]==b[1][0]==b[2][0]!= ' ' or b[0][1]==b[1][1]==b[2][1]!= ' ' or b[0][2]==b[1][2]==b[2][2]!= ' '
dia = b[0][0]==b[1][1]==b[2][2]!= ' ' or b[2][0]==b[1][1]==b[0][2]!= ' '
if hori or verti or dia:
winner = player
for i in board:
if " " in i:
full = False
break
else:
full = True
if full and not winner:
winner = 'draw'
player = 'X'
while game:
move = input(f"Player ({player}) (Eg:12) : ")
if move not in moves:
moves.append(move)
else:
print("Already Occupied!")
continue
try:
x,y = int(move[0]), int(move[1])
except:
print("Enter the Coordinates properly")
continue
board[2-y][x] = f"{player}"
display()
win(player)
if winner == player:
print(f"{player} wins!")
game = False
elif winner == "draw":
print("It's a Tie")
game = False
if player=='X':
player = 'O'
else:
player = 'X'
>>> python3 ttt.py
Player (X) (Eg:12) : 11
-------
| | | |
-------
| |X| |
-------
| | | |
-------
Player (O) (Eg:10) : 01
-------
| | | |
-------
|O|X| |
-------
| | | |
-------
Player (X) (Eg:12) : 00
-------
| | | |
-------
|O|X| |
-------
|X| | |
-------
Player (O) (Eg:10) : 22
-------
| | |O|
-------
|O|X| |
-------
|X| | |
-------
Player (X) (Eg:12) : 20
-------
| | |O|
-------
|O|X| |
-------
|X| |X|
-------
Player (O) (Eg:10) : 10
-------
| | |O|
-------
|O|X| |
-------
|X|O|X|
-------
Player (X) (Eg:12) : 02
-------
|X| |O|
-------
|O|X| |
-------
|X|O|X|
-------
X wins!
TO SUM UP . . .
Today we have seen how to build Tic-Tac-Toe in Python. If you have any questions or feedback on this, feel free to post in the comments section below.
If you are on your way to becoming a Python expert, you might find our site really helpful. Hit this link for more python related posts
For more python projects like this, check out our Projects Posts
Check it out, it’s Simply the Best Code.
Thank you, really glad you found our site helpful! It means alot!