Tic Tac Toe Game In Python With Source Code
The Tic Tac Toe Game In Python is written in python programming language, This Tic Tac Toe In Python is a simple GUI based strategy board game that is very easy to understand and use. All the playing rules are the same just like we play in real time tic-tac-toe. This Tic Tac Toe Python Program is a simple multiplayer game.
A Tic Tac Toe In Python Using Tkinter game starts with a GUI board. First, the player has to click on any box to start the game. It contains the Minimax algorithm; which is a decision rule used for a two-player game. A simple Tic Tac Toe Python Code 2 Player 2D GUI is provided for easy gameplay. The gameplay design is so simple that user won’t find it difficult to use and understand.
Watch the video here to see the full running of the Tic Tac Toe game In Python with Source Code.
Anyway if you want level up your knowledge in programming especially games in python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code
This Tic Tac Toe In Python Code also includes a downloadable Tic Tac Toe Python Code for free, just find the downloadable source code below and click to start downloading.
To start creating a Tic Tac Toe Game In Python, make sure that you have PyCharm IDE installed in your computer.
Steps on How To Create Tic Tac Toe In Python
Tic Tac Toe Game In Python With Source Code
- Step 1: Create a project name.
First open Pycharm IDE and then create a “project name” after creating a project name click the “create” button.
- Step 2: Create a python file.
Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.
- Step 3: Name your python file.
Third after creating a python file, Name your python file after that click “enter“.
- Step 4: The actual code.
You are free to copy the code given below and download the full source code below.
The Code Given Below Is For Importing Modules
1 2 3 |
from tkinter import Tk,ttk,Button from tkinter import messagebox from random import randint |
The code given which is importing all modules or libraries.
The Code Given Below Is For The Module Lay Out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
def SetLayout(id,player_symbol): if id==1: b1.config(text= player_symbol) b1.state(['disabled']) elif id==2: b2.config(text= player_symbol) b2.state(['disabled']) elif id==3: b3.config(text= player_symbol) b3.state(['disabled']) elif id==4: b4.config(text= player_symbol) b4.state(['disabled']) elif id==5: b5.config(text= player_symbol) b5.state(['disabled']) elif id==6: b6.config(text= player_symbol) b6.state(['disabled']) elif id==7: b7.config(text= player_symbol) b7.state(['disabled']) elif id==8: b8.config(text= player_symbol) b8.state(['disabled']) elif id==9: b9.config(text= player_symbol) b9.state(['disabled']) |
In this module which is setting the layout of its player symbol.
The Code Given Below Is For The Check Winner Module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
def CheckWinner(): global mov winner = -1 if(1 in p1) and (2 in p1) and (3 in p1): winner = 1 if(1 in p2) and (2 in p2) and (3 in p2): winner = 2 if(4 in p1) and (5 in p1) and (6 in p1): winner = 1 if(4 in p2) and (5 in p2) and (6 in p2): winner = 2 if(7 in p1) and (8 in p1) and (9 in p1): winner = 1 if(7 in p2) and (8 in p2) and (9 in p2): winner = 2 if(1 in p1) and (4 in p1) and (7 in p1): winner = 1 if(1 in p2) and (4 in p2) and (7 in p2): winner = 2 if(2 in p1) and (5 in p1) and (8 in p1): winner = 1 if(2 in p2) and (5 in p2) and (8 in p2): winner = 2 if(3 in p1) and (6 in p1) and ( 9 in p1): winner = 1 if(3 in p2) and (6 in p2) and (9 in p2): winner = 2 if(1 in p1) and (5 in p1) and ( 9 in p1): winner = 1 if(1 in p2) and (5 in p2) and (9 in p2): winner = 2 if(3 in p1) and (5 in p1) and ( 7 in p1): winner = 1 if(3 in p2) and (5 in p2) and (7 in p2): winner = 2 if winner ==1: messagebox.showinfo(title="Congratulations.", message="Player 1 is the winner") elif winner ==2: messagebox.showinfo(title="Congratulations.", message="Player 2 is the winner") elif mov ==9: messagebox.showinfo(title="Draw", message="It's a Draw!!") |
In this module which is checking the winner of two players.
The Code Given Below Is For The Module Button Click
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def ButtonClick(id): global ActivePlayer global p1,p2 global mov if(ActivePlayer ==1): SetLayout(id,"X") p1.append(id) mov +=1 root.title("Tic Tac Toe : Player 2") ActivePlayer =2 elif(ActivePlayer==2): SetLayout(id,"O") p2.append(id) mov +=1 root.title("Tic Tac Toe : Player 1") ActivePlayer =1 CheckWinner() |
In this module which is button click of two players and its conditions.
The Code Given Below Is For The Module Restart
1 2 3 4 5 6 |
def Restart(): global p1,p2,mov,ActivePlayer p1.clear(); p2.clear() mov,ActivePlayer = 0,1 root.title("Tic Tac Toe : Player 1") EnableAll() |
In this module which is the restarting or the new game module of the game.
Complete Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
from tkinter import Tk,ttk,Button from tkinter import messagebox from random import randint ActivePlayer = 1 p1 = [] p2 = [] mov = 0 def SetLayout(id,player_symbol): if id==1: b1.config(text= player_symbol) b1.state(['disabled']) elif id==2: b2.config(text= player_symbol) b2.state(['disabled']) elif id==3: b3.config(text= player_symbol) b3.state(['disabled']) elif id==4: b4.config(text= player_symbol) b4.state(['disabled']) elif id==5: b5.config(text= player_symbol) b5.state(['disabled']) elif id==6: b6.config(text= player_symbol) b6.state(['disabled']) elif id==7: b7.config(text= player_symbol) b7.state(['disabled']) elif id==8: b8.config(text= player_symbol) b8.state(['disabled']) elif id==9: b9.config(text= player_symbol) b9.state(['disabled']) def CheckWinner(): global mov winner = -1 if(1 in p1) and (2 in p1) and (3 in p1): winner = 1 if(1 in p2) and (2 in p2) and (3 in p2): winner = 2 if(4 in p1) and (5 in p1) and (6 in p1): winner = 1 if(4 in p2) and (5 in p2) and (6 in p2): winner = 2 if(7 in p1) and (8 in p1) and (9 in p1): winner = 1 if(7 in p2) and (8 in p2) and (9 in p2): winner = 2 if(1 in p1) and (4 in p1) and (7 in p1): winner = 1 if(1 in p2) and (4 in p2) and (7 in p2): winner = 2 if(2 in p1) and (5 in p1) and (8 in p1): winner = 1 if(2 in p2) and (5 in p2) and (8 in p2): winner = 2 if(3 in p1) and (6 in p1) and ( 9 in p1): winner = 1 if(3 in p2) and (6 in p2) and (9 in p2): winner = 2 if(1 in p1) and (5 in p1) and ( 9 in p1): winner = 1 if(1 in p2) and (5 in p2) and (9 in p2): winner = 2 if(3 in p1) and (5 in p1) and ( 7 in p1): winner = 1 if(3 in p2) and (5 in p2) and (7 in p2): winner = 2 if winner ==1: messagebox.showinfo(title="Congratulations.", message="Player 1 is the winner") elif winner ==2: messagebox.showinfo(title="Congratulations.", message="Player 2 is the winner") elif mov ==9: messagebox.showinfo(title="Draw", message="It's a Draw!!") def ButtonClick(id): global ActivePlayer global p1,p2 global mov if(ActivePlayer ==1): SetLayout(id,"X") p1.append(id) mov +=1 root.title("Tic Tac Toe : Player 2") ActivePlayer =2 elif(ActivePlayer==2): SetLayout(id,"O") p2.append(id) mov +=1 root.title("Tic Tac Toe : Player 1") ActivePlayer =1 CheckWinner() def AutoPlay(): global p1; global p2 Empty = [] for cell in range(9): if(not((cell +1 in p1) or (cell +1 in p2))): Empty.append(cell+1) try: RandIndex = randint(0,len(Empty) -1) ButtonClick(Empty[RandIndex]) except: pass def EnableAll(): b1.config(text= " ") b1.state(['!disabled']) b2.config(text= " ") b2.state(['!disabled']) b3.config(text= " ") b3.state(['!disabled']) b4.config(text= " ") b4.state(['!disabled']) b5.config(text= " ") b5.state(['!disabled']) b6.config(text= " ") b6.state(['!disabled']) b7.config(text= " ") b7.state(['!disabled']) b8.config(text= " ") b8.state(['!disabled']) b9.config(text= " ") b9.state(['!disabled']) def Restart(): global p1,p2,mov,ActivePlayer p1.clear(); p2.clear() mov,ActivePlayer = 0,1 root.title("Tic Tac Toe : Player 1") EnableAll() root = Tk() root.title("Tic Tac toe : Player 1") st = ttk.Style() st.configure("my.TButton", font=('Chiller',24,'bold')) b1 = ttk.Button(root, text=" ", style="my.TButton") b1.grid(row=1, column=0, sticky="nwse", ipadx=50,ipady=50) b1.config(command = lambda : ButtonClick(1)) b2 = ttk.Button(root, text=" ",style ="my.TButton") b2.grid(row=1, column=1, sticky="snew", ipadx=50, ipady=50) b2.config(command = lambda : ButtonClick(2)) b3= ttk.Button(root, text=" ",style="my.TButton") b3.grid(row=1, column=2, sticky="snew", ipadx=50, ipady=50) b3.config(command = lambda : ButtonClick(3)) b4 = ttk.Button(root, text=" ",style="my.TButton") b4.grid(row=2, column=0, sticky="snew", ipadx=50, ipady=50) b4.config(command = lambda : ButtonClick(4)) b5 = ttk.Button(root, text=" ",style="my.TButton") b5.grid(row=2, column=1, sticky="snew", ipadx=50, ipady=50) b5.config(command = lambda : ButtonClick(5)) b6 = ttk.Button(root, text=" ",style="my.TButton") b6.grid(row=2, column=2, sticky="snew", ipadx=50, ipady=50) b6.config(command = lambda : ButtonClick(6)) b7 = ttk.Button(root, text=" ",style="my.TButton") b7.grid(row=3, column=0, sticky="snew", ipadx=50, ipady=50) b7.config(command = lambda : ButtonClick(7)) b8 = ttk.Button(root, text=" ",style="my.TButton") b8.grid(row=3, column=1, sticky="snew", ipadx=50, ipady=50) b8.config(command = lambda : ButtonClick(8)) b9 = ttk.Button(root, text=" ",style="my.TButton") b9.grid(row=3, column=2, sticky="snew", ipadx=50, ipady=50) b9.config(command = lambda : ButtonClick(9)) Button(text="New Game..", font=('Courier new', 22, 'bold'), bg='blue', fg='white', border=5, width=4,command = lambda :Restart()).grid(row=0, column=1, sticky="we") root.resizable(0,0) root.mainloop() |
Output

Run Quick Virus Scan for secure Download
Run Quick Scan for secure DownloadDownloadable Source Code
I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.
Summary
The Tic Tac Toe Game In Python is written in Python programming language, Python is very smooth to research the syntax emphasizes readability and it is able to reduces time ingesting in developing.
This is a simple GUI Based system, specially written for the beginners. Tic Tac Toe in Python project with source code is free to download. Use for education purpose only!
Also in this tutorial is the simplest way for the beginners or the student to enhance their logical skills in programming. and also in this game project is the way for the students or beginners in designing and developing games.
Related Articles
- Puzzle Game In Python With Source Code
- Speed Typing Test Python Project With Source Code
- Snakes and Ladders Game in Python with Source Code
- Code For Game in Python: Python Game Projects With Source Code
- Stickman Game in Python with Source Code
- Tank Game Python with Source Code
- Tetris In Python Code
- Mario Game In Python With Source Code
- Hangman Game In Python With Source Code
- Aircraft War Game in Python with Source Code
- Snake Game In Python Code
- How to Make Bouncing Ball Game in Python with Source Code
- How to Create Rock-Paper-Scissor Game in Python
Inquiries
If you have any questions or suggestions about Tic Tac Toe Game In Python , please feel free to leave a comment below.