Tic Tac Toe Game in Python: Updated On Feb 17, 2023 16:56 IST
Tic Tac Toe Game in Python: Updated On Feb 17, 2023 16:56 IST
Shiksha Online
Updated on Feb 17, 2023 16:56 IST
Learn how to easily create the classic Tic Tac Toe game in Python.
In this article, we will build the classic tic-tac-toe game using Python. Tic Tac Toe is a
simple two-player game where players mark spaces on a 3×3 grid with X and O. The
goal is to get three of your marks in a row, either horizontally, vertically, or
diagonally.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Python Loops
St ep 1
The following line initializes a list called board with 9 spaces, representing the 9
spaces on the Tic Tac Toe board.
Copy code
St ep 2
The following function print_board() will print the Tic Tac Toe board. It creates 3
strings, row1, row2, and row3, to represent each row of the board. The spaces in
the board are filled with the values from the board list. The function then prints each
row with separators to create the appearance of a 3×3 grid.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
print ()
print (row1)
print (row2)
print (row3)
print ()
St ep 3
The following function then prints each row with separators to create the
appearance of a 3×3 grid. The player_move() function allows a player to make a
move by marking a space on the board with their icon (X or O). It takes the player’s
icon as an argument and asks the player to enter their move (1-9) in the terminal. If
the space the player chooses is blank, the function will place the player’s icon in that
space. Otherwise, it will inform the player that the space is full.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
def player_move(icon):
if icon == "X":
number = 1
elif icon == "O":
number = 2
print ("Your t urn player {}".f ormat (number))
choice = int (input ("Ent er your move (1-9): ").st rip())
if board[choice - 1] == " ":
board[choice - 1] = icon
else:
print ()
print ("T hat space is already t aken!")
St ep 4
This function is_victory() checks if a player has won the game. It takes the player’s
icon as an argument and checks if there are three of that icon in a row on the board.
It returns True if a player has won, otherwise it returns False.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
St ep 5
This function is_draw() checks if the game is a draw by checking if all spaces on the
board are filled. When all spaces are filled and no player has won, it returns True,
otherwise it returns False.
Copy code
def is_draw():
if " " not in board:
ret urn T rue
else:
ret urn False
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
St ep 6
The while loop continues the game until a player has won or the game is a draw.
Within the loop, the board is printed. Then, the player_move() function calls and
allows the first player to make a move. The board prints again to show the updated
state. Then, calling the is_victory() function will check if the first player has won.
When they win, the game ends and a victory message shows If not, the is_draw()
function is called to check if the game is a draw.
If the game is a draw, a message is displayed and the game ends. If neither the first
player nor the game has won or ended in a draw, the second player makes a move
and the process repeats.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
while T rue:
print _board()
player_move("X")
print _board()
if is_vict ory("X"):
print ("X wins! Congrat ulat ions!")
break
elif is_draw():
print ("It 's a draw!")
break
player_move("O")
if is_vict ory("O"):
print _board()
print ("O wins! Congrat ulat ions!")
break
elif is_draw():
print ("It 's a draw!")
break
Out put
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
In summary, this basic Tic Tac Toe game implementation in Python can help
beginners who are just starting with the language to apply their knowledge and
improve their logic skills while enjoying the game.
Endnotes
Hope this article proved helpful and fun in teaching you how to build a tic-tac-toe
game in Python.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.