0% found this document useful (0 votes)
35 views8 pages

Tic Tac Toe Game in Python: Updated On Feb 17, 2023 16:56 IST

Uploaded by

mrishu133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Tic Tac Toe Game in Python: Updated On Feb 17, 2023 16:56 IST

Uploaded by

mrishu133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tic Tac Toe Game in Python

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.

So, without further ado, let’s start!

Tic Tac Toe Python Program


Here’s a basic implementation of a two-player Tic Tac Toe in Python. To
understand the program below, you should have a basic idea about the following
Python concepts:
Python Functions

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

board = [" " f or x in range(9)]

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

def print _board():


row1 = "| {} | {} | {} |".f ormat (board[0], board[1], board[2])
row2 = "| {} | {} | {} |".f ormat (board[3], board[4], board[5])
row3 = "| {} | {} | {} |".f ormat (board[6], board[7], board[8])

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

def is_vict ory(icon):


if (board[0] == icon and board[1] == icon and board[2] == icon) or
(board[3] == icon and board[4] == icon and board[5] == icon) or
(board[6] == icon and board[7] == icon and board[8] == icon) or
(board[0] == icon and board[3] == icon and board[6] == icon) or
(board[1] == icon and board[4] == icon and board[7] == icon) or
(board[2] == icon and board[5] == icon and board[8] == icon) or
(board[0] == icon and board[4] == icon and board[8] == icon) or
(board[2] == icon and board[4] == icon and board[6] == icon):
ret urn T rue
else:
ret urn False

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.

Contributed by – Prerna Singh

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy