{"id":8915,"date":"2022-03-04T17:22:07","date_gmt":"2022-03-04T11:52:07","guid":{"rendered":"https:\/\/copyassignment.com\/?p=8915"},"modified":"2022-11-18T16:13:27","modified_gmt":"2022-11-18T10:43:27","slug":"tic-tac-toe-using-python-project-with-source-code","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/tic-tac-toe-using-python-project-with-source-code\/","title":{"rendered":"Tic Tac Toe in Python"},"content":{"rendered":"\n<p>Hey folks, as we all are now aware that Python supports thousands of modules and packages to crack any logic behind the idea! And here also Python will help you play and create a very renowned and interesting game which we have all gotten our hands on since our childhood. Yes, you read it correctly game using Python and that&#8217;s <span style=\"text-decoration: underline\"><a href=\"https:\/\/playtictactoe.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Tic-Tac-Toe<\/a><\/span>!, so let&#8217;s start our journey for Tic Tac Toe in Python Project with source code.<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">What is the Tic Tac Toe game?<\/h2>\n\n\n\n<p><strong>Tic Tac To<\/strong>e is a 2 player game where each player has a symbol (either X or O) and plays alternately to mark their symbol on a 3&#215;3 grid.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"225\" height=\"224\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-image-1.png\" alt=\"what is Tic Tac Toe\" class=\"wp-image-8919 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-image-1.png 225w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-image-1-150x150.png 150w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-image-1-100x100.png 100w\" data-sizes=\"(max-width: 225px) 100vw, 225px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 225px; --smush-placeholder-aspect-ratio: 225\/224;\" \/><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>If any player gets their(X or 0) symbol consecutively 3 times in a row, column or diagonal then that player is the winner. So, in total, we have 8 winning conditions: 3 for the rows, 3 for the columns, and 2 for the diagonals. Isn&#8217;t it interesting!<\/p>\n\n\n\n<p>So, we will be coding it in Python using Tkinter for the interface.<\/p>\n\n\n\n<p>&#8216;<strong>Tkinter<\/strong>&#8216; is one of the known in-built libraries of Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applications. All you need to do is perform the following steps:<\/p>\n\n\n\n<p>We first need to import the tkinter module.<\/p>\n\n\n\n<p>Since we also have to display the results, we should use messagebox widget. As MessageBox Widget is used to display the message boxes in the python applications. Hence to use this, we have to import messagebox:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from tkinter import *\nimport tkinter.messagebox <\/code><\/pre>\n\n\n\n<p>Using * after import means we have imported all the methods and variables of the tkinter library.<\/p>\n\n\n\n<p>Now, let&#8217;s create the GUI application&#8217;s main window i.e. game window.<\/p>\n\n\n\n<p>Here, we will create a Tk widget and assign it to a variable root. And title() is used to set the title of the window. <\/p>\n\n\n\n<p>Let&#8217;s assign two variables: click and count as well. Initialize &#8216;click&#8217; to &#8216;True&#8217; and &#8216;count&#8217; to &#8216;0&#8217;. According to our logic, if click=True that means &#8216;X&#8217; is clicking and if it is False that means it&#8217;s time for &#8216;0&#8217; to click, while the count variable will check how many turns have been played, and after every turn, the count variable will be incremented by 1. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Coding Board for Tic Tac Toe in Python<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>root = Tk()\n\nroot.iconbitmap('tic tac toe.ico')\n\nroot.title('Tic-Tac-Toe')\n\nroot.resizable(False,False)\n\nclick = True\n\ncount = 0\n\nbtn1 = StringVar()\nbtn2 = StringVar()\nbtn3 = StringVar()\nbtn4 = StringVar()\nbtn5 = StringVar()\nbtn6 = StringVar()\nbtn7 = StringVar()\nbtn8 = StringVar()\nbtn9 = StringVar()\n\nxPhoto = PhotoImage(file = 'cross.png')\noPhoto = PhotoImage(file = 'happy.png')\n\n#grid buttons\n\ndef start():\n    button1 = Button(root,height=9,width=19,bd=.5,relief = 'sunken',bg = '#ccfff7',textvariable = btn1) \n    button1.grid(row=0,column=0)\n\n    button2 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#ccfff7',textvariable = btn2)\n    button2.grid(row=0,column=1)\n\n    button3 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#ccfff7',textvariable = btn3)\n    button3.grid(row=0,column=2)\n\n    button4 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn4)\n    button4.grid(row=1,column=0)\n\n    button5 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn5)\n    button5.grid(row=1,column=1)\n\n    button6 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn6)\n    button6.grid(row=1,column=2)\n\n    button7 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn7)\n    button7.grid(row=2,column=0)\n\n    button8 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn8)\n    button8.grid(row=2,column=1)\n\n    button9 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn9)\n    button9.grid(row=2,column=2)\n\nstart()\n\nroot.mainloop()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><span style=\"text-decoration: underline\">Output:<\/span><\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"525\" height=\"574\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-window.jpg\" alt=\"Tic Tac Toe board\" class=\"wp-image-8940 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-window.jpg 525w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-window-274x300.jpg 274w\" data-sizes=\"(max-width: 525px) 100vw, 525px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 525px; --smush-placeholder-aspect-ratio: 525\/574;\" \/><figcaption class=\"wp-element-caption\">And this is what the graphical interface for our game looks like!<\/figcaption><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>As you can see now, we have 9 buttons starting from button1 to button9 using the Button method of Tkinter in our Tic Tac Toe game board, and the window is in the grid format. As the first parameter, we have given the value root as the button to be inside the parent window. <\/p>\n\n\n\n<p>Further, we have set the values of buttons with height=9 and width=19 and background color. And the start( ) function is used to directly start the window. You can play with those values for a better understanding. <\/p>\n\n\n\n<p>Wasn&#8217;t that interesting?? And the rest of the code is as interesting as this!! <\/p>\n\n\n\n<p>Now, whenever the button is clicked we are calling the &#8216;press function&#8217;. We have defined press() as a lambda function i.e. lambda() function is used to send specific data to the callback function. The statement for that is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command=lambda: press()<\/code><\/pre>\n\n\n\n<p>Now, we will add this command to the button attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def start():\n    button1 = Button(root,height=9,width=19,bd=.5,relief = 'sunken',bg = '#ccfff7',textvariable = btn1,\n command=lambda: press(1,0,0)) \n    button1.grid(row=0,column=0)\n\n    button2 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#ccfff7',textvariable = btn2,\n command=lambda: press(2,0,1))\n    button2.grid(row=0,column=1)\n\n    button3 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#ccfff7',textvariable = btn3,\n command=lambda: press(3,0,2))\n    button3.grid(row=0,column=2)\n\n    button4 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn4,\n command=lambda: press(4,1,0))\n    button4.grid(row=1,column=0)\n\n    button5 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn5,\n command=lambda: press(5,1,1))\n    button5.grid(row=1,column=1)\n\n    button6 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn6,\n command=lambda: press(6,1,2))\n    button6.grid(row=1,column=2)\n\n    button7 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn7, command=lambda: press(7,2,0))\n    button7.grid(row=2,column=0)\n\n    button8 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn8,\n command=lambda: press(8,2,1))\n    button8.grid(row=2,column=1)\n\n    button9 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn9,\n command=lambda: press(9,2,2))\n    button9.grid(row=2,column=2)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Algorithm or Logic behind Tic Tac Toe<\/h2>\n\n\n\n<p>This is how it works. Whenever a player clicks on the button, we need to add symbols over it. The press() function does exactly the same. The parameters passed are num, r, and c. Let&#8217;s try to understand:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def press(num,r,c):\n    global click,count\n    if click == True:\n        labelPhoto = Label(root,image = xPhoto)\n        labelPhoto.grid(row=r,column=c)\n        if num == 1:\n            btn1.set('X')\n        elif num == 2:\n            btn2.set('X')\n        elif num == 3:\n            btn3.set('X')\n        elif num == 4:\n            btn4.set('X')\n        elif num == 5:\n            btn5.set('X')\n        elif num == 6:\n            btn6.set('X')\n        elif num == 7:\n            btn7.set('X')\n        elif num == 8:\n            btn8.set('X')\n        else:\n            btn9.set('X')\n        count += 1\n        click = False\n        checkWin()\n        \n    else:\n        labelPhoto = Label(root,image = oPhoto)\n        labelPhoto.grid(row=r,column=c)\n        if num == 1:\n            btn1.set('O')\n        elif num == 2:\n            btn2.set('O')\n        elif num == 3:\n            btn3.set('O')\n        elif num == 4:\n            btn4.set('O')\n        elif num == 5:\n            btn5.set('O')\n        elif num == 6:\n            btn6.set('O')\n        elif num == 7:\n            btn7.set('O')\n        elif num == 8:\n            btn8.set('O')\n        else:\n            btn9.set('O')\n        count += 1\n        click = True\n        checkWin()<\/code><\/pre>\n\n\n\n<p>As we know, here press() function will help to check which button is pressed. If click=True that means &#8216;X&#8217; is clicked and if click=False that means it&#8217;s time for &#8216;0&#8217; to click and this will go back and forth. <\/p>\n\n\n\n<p>In the above lines of code, we created a Label widget as well, which allows us to write something in the parent window. In the first parameter, we will give the value of the parent window i.e. here is its root. Further, in the second parameter, we have to set the text we have to display in the window i.e. we have passed the image as the parameter with the &#8216;X&#8217; and &#8216;0&#8217; images in it. To display these images we have used the Tkinter grid() method.<\/p>\n\n\n\n<p>Now, we all are aware that tic tac toe is a two-player game. Isn&#8217;t it?<\/p>\n\n\n\n<p> So, we will need to make two such labels as created above. Wasn&#8217;t that easy!<\/p>\n\n\n\n<p>After this, it&#8217;s time to check who wins. For this purpose, we will check all the values for the player to win and we know there is a total of 8 winning conditions for the player to win, by checking which player makes three of their marks in a row(up, down, across, or diagonally).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def checkWin():\n    global count,click\n    \n    if (btn1.get() == 'X' and btn2.get() == 'X' and btn3.get() == 'X' or\n        btn4.get() == 'X' and btn5.get() == 'X' and btn6.get() == 'X' or\n        btn7.get() == 'X' and btn8.get() == 'X' and btn9.get() == 'X' or\n        btn1.get() == 'X' and btn4.get() == 'X' and btn7.get() == 'X' or\n        btn2.get() == 'X' and btn5.get() == 'X' and btn8.get() == 'X' or\n        btn3.get() == 'X' and btn6.get() == 'X' and btn9.get() == 'X' or\n        btn1.get() == 'X' and btn5.get() == 'X' and btn9.get() == 'X' or\n        btn3.get() == 'X' and btn5.get() == 'X' and btn7.get() == 'X'):\n        tkinter.messagebox.showinfo(\"Tic-Tac-Toe\", 'X Wins !')\n        click = True\n        count = 0\n        clear()\n        start()\n        \n    elif (btn1.get() == 'O' and btn2.get() == 'O' and btn3.get() == 'O' or\n          btn4.get() == 'O' and btn5.get() == 'O' and btn6.get() == 'O' or\n          btn7.get() == 'O' and btn8.get() == 'O' and btn9.get() == 'O' or\n          btn1.get() == 'O' and btn4.get() == 'O' and btn7.get() == 'O' or\n          btn2.get() == 'O' and btn5.get() == 'O' and btn8.get() == 'O' or\n          btn3.get() == 'O' and btn6.get() == 'O' and btn9.get() == 'O' or\n          btn1.get() == 'O' and btn5.get() == 'O' and btn9.get() == 'O' or\n          btn3.get() == 'O' and btn5.get() == 'O' and btn7.get() == 'O'):\n          tkinter.messagebox.showinfo(\"Tic-Tac-Toe\", 'O Wins !')\n          count = 0\n          clear()\n          start()\n          \n    elif (count == 9):\n         tkinter.messagebox.showinfo(\"Tic-Tac-Toe\", 'Tie Game!')\n         click = True\n         count = 0\n         clear()\n         start()\n<\/code><\/pre>\n\n\n\n<p>Finally, the check function will check the board row-wise, column-wise, and diagonal-wise for equality and displays the result of the game. <\/p>\n\n\n\n<p>To display the result, showinfo() method in the messagebox widget is used. It shows some relevant information according to our conditions. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><span style=\"text-decoration: underline\">Output:<\/span><\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"508\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-1024x508.jpg\" alt=\"final output for Tic Tac Toe in Python\" class=\"wp-image-8942 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-1024x508.jpg 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-300x149.jpg 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-768x381.jpg 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-1536x762.jpg 1536w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-1026x509.jpg 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main-675x335.jpg 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-winner-main.jpg 1993w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/508;\" \/><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>So, this is what our game will look like and we are almost done with it!!<\/p>\n\n\n\n<p>After each game, this is how the winner will be displayed in another window.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"677\" height=\"368\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/Winning-tk-main1.jpg\" alt=\"results\" class=\"wp-image-8951 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/Winning-tk-main1.jpg 677w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/Winning-tk-main1-300x163.jpg 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/Winning-tk-main1-675x367.jpg 675w\" data-sizes=\"(max-width: 677px) 100vw, 677px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 677px; --smush-placeholder-aspect-ratio: 677\/368;\" \/><\/figure>\n\n\n\n<p>Now, the game has been over so we will clear the tiles by defining a clear function i.e. clear():<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def clear():\n    btn1.set('')\n    btn2.set('')\n    btn3.set('')\n    btn4.set('')\n    btn5.set('')\n    btn6.set('')\n    btn7.set('')\n    btn8.set('')\n    btn9.set('')<\/code><\/pre>\n\n\n\n<p>Finally, mainloop() method executes when we want to run the program and we are done!<\/p>\n\n\n\n<p>Thus, we have developed one of the interesting game <strong>Tic Tac Toe<\/strong> using Tkinter in Python. Now, you know how easy it is to play this game. Let&#8217;s get hands over  the complete code:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Code for Tic Tac Toe in Python<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#Tic Tac Toe game using tkinter\n\n#Importing modules\nfrom tkinter import *\nimport tkinter.messagebox\n\n#Window defined\nroot = Tk()\n\nroot.iconbitmap('tic-tac-toe.ico')\n\nroot.title('Tic-Tac-Toe')\n\nroot.resizable(False,False)\n\nclick = True\n\n#Count variable to check the no. of turns\ncount = 0\n\nbtn1 = StringVar()\nbtn2 = StringVar()\nbtn3 = StringVar()\nbtn4 = StringVar()\nbtn5 = StringVar()\nbtn6 = StringVar()\nbtn7 = StringVar()\nbtn8 = StringVar()\nbtn9 = StringVar()\n\nxPhoto = PhotoImage(file = 'cross.png')\noPhoto = PhotoImage(file = 'happy.png')\n\n#Grid buttons\ndef start():\n    button1 = Button(root,height=9,width=19,bd=.5,relief = 'sunken',bg = '#ccfff7',textvariable = btn1,\n                     command=lambda: press(1,0,0)) \n    button1.grid(row=0,column=0)\n\n    button2 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#ccfff7',textvariable = btn2,\n                     command=lambda: press(2,0,1))\n    button2.grid(row=0,column=1)\n\n    button3 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#ccfff7',textvariable = btn3,\n                     command=lambda: press(3,0,2))\n    button3.grid(row=0,column=2)\n\n    button4 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn4,\n                     command=lambda: press(4,1,0))\n    button4.grid(row=1,column=0)\n\n    button5 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn5,\n                     command=lambda: press(5,1,1))\n    button5.grid(row=1,column=1)\n\n    button6 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#99ffee',textvariable = btn6,\n                     command=lambda: press(6,1,2))\n    button6.grid(row=1,column=2)\n\n    button7 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn7,\n                     command=lambda: press(7,2,0))\n    button7.grid(row=2,column=0)\n\n    button8 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn8,\n                     command=lambda: press(8,2,1))\n    button8.grid(row=2,column=1)\n\n    button9 = Button(root,height=9,width=19,bd = .5,relief = 'sunken',bg = '#66ffe6',textvariable = btn9,\n                     command=lambda: press(9,2,2))\n    button9.grid(row=2,column=2)\n\n#Changing the value of button\ndef press(num,r,c):\n    global click,count\n    if click == True:\n        labelPhoto = Label(root,image = xPhoto)\n        labelPhoto.grid(row=r,column=c)\n        if num == 1:\n            btn1.set('X')\n        elif num == 2:\n            btn2.set('X')\n        elif num == 3:\n            btn3.set('X')\n        elif num == 4:\n            btn4.set('X')\n        elif num == 5:\n            btn5.set('X')\n        elif num == 6:\n            btn6.set('X')\n        elif num == 7:\n            btn7.set('X')\n        elif num == 8:\n            btn8.set('X')\n        else:\n            btn9.set('X')\n        count += 1\n        click = False\n        checkWin()\n        \n    else:\n        labelPhoto = Label(root,image = oPhoto)\n        labelPhoto.grid(row=r,column=c)\n        if num == 1:\n            btn1.set('O')\n        elif num == 2:\n            btn2.set('O')\n        elif num == 3:\n            btn3.set('O')\n        elif num == 4:\n            btn4.set('O')\n        elif num == 5:\n            btn5.set('O')\n        elif num == 6:\n            btn6.set('O')\n        elif num == 7:\n            btn7.set('O')\n        elif num == 8:\n            btn8.set('O')\n        else:\n            btn9.set('O')\n        count += 1\n        click = True\n        checkWin()\n \n#Checks the winner      \n    \ndef checkWin():\n    global count,click\n    \n    if (btn1.get() == 'X' and btn2.get() == 'X' and btn3.get() == 'X' or\n        btn4.get() == 'X' and btn5.get() == 'X' and btn6.get() == 'X' or\n        btn7.get() == 'X' and btn8.get() == 'X' and btn9.get() == 'X' or\n        btn1.get() == 'X' and btn4.get() == 'X' and btn7.get() == 'X' or\n        btn2.get() == 'X' and btn5.get() == 'X' and btn8.get() == 'X' or\n        btn3.get() == 'X' and btn6.get() == 'X' and btn9.get() == 'X' or\n        btn1.get() == 'X' and btn5.get() == 'X' and btn9.get() == 'X' or\n        btn3.get() == 'X' and btn5.get() == 'X' and btn7.get() == 'X'):\n        tkinter.messagebox.showinfo(\"Tic-Tac-Toe\", 'X Wins !')\n        click = True\n        count = 0\n        clear()\n        start()\n        \n    elif (btn1.get() == 'O' and btn2.get() == 'O' and btn3.get() == 'O' or\n          btn4.get() == 'O' and btn5.get() == 'O' and btn6.get() == 'O' or\n          btn7.get() == 'O' and btn8.get() == 'O' and btn9.get() == 'O' or\n          btn1.get() == 'O' and btn4.get() == 'O' and btn7.get() == 'O' or\n          btn2.get() == 'O' and btn5.get() == 'O' and btn8.get() == 'O' or\n          btn3.get() == 'O' and btn6.get() == 'O' and btn9.get() == 'O' or\n          btn1.get() == 'O' and btn5.get() == 'O' and btn9.get() == 'O' or\n          btn3.get() == 'O' and btn5.get() == 'O' and btn7.get() == 'O'):\n          tkinter.messagebox.showinfo(\"Tic-Tac-Toe\", 'O Wins !')\n          count = 0\n          clear()\n          start()\n          \n    elif (count == 9):\n         tkinter.messagebox.showinfo(\"Tic-Tac-Toe\", 'Tie Game!')\n         click = True\n         count = 0\n         clear()\n         start()\n\n#Clear the tiles\ndef clear():\n    btn1.set('')\n    btn2.set('')\n    btn3.set('')\n    btn4.set('')\n    btn5.set('')\n    btn6.set('')\n    btn7.set('')\n    btn8.set('')\n    btn9.set('')\n\nstart()\n\nroot.mainloop()\n\n<\/code><\/pre>\n\n\n\n<p>Now, let&#8217;s run the code and check the output:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><span style=\"text-decoration: underline\">Output:<\/span><\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"864\" style=\"aspect-ratio: 1536 \/ 864;\" width=\"1536\" controls src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/03\/tic-tac-toe-vdo-main-1.mp4\"><\/video><\/figure>\n\n\n\n<p>Here it is! See, How easy it was!<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">NOTE<\/span><\/strong> : Enter the path of the images you are using in your code.<\/p>\n\n\n\n<p>Thanks for reading Tic Tac Toe in Python, <a href=\"https:\/\/copyassignment.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">visit our website<\/a> for more, try to code, and practice the same. Happy Coding!!<\/p>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/create-your-own-chatgpt-with-python\/\">Create your own ChatGPT with\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bakery-management-system-in-python-class-12-project\/\">Bakery Management System in Python | Class 12 Project<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sqlite-crud-operations-in-python\/\">SQLite | CRUD Operations in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/event-management-system-project-in-python\/\">Event Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ticket-booking-and-management-in-python\/\">Ticket Booking and Management in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hostel-management-system-project-in-python\/\">Hostel Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sales-management-system-project-in-python\/\">Sales Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bank-management-system-project-in-cpp\/\">Bank Management System Project in C++<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-download-file-from-url-4-methods\/\">Python Download File from URL | 4 Methods<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-programming-examples-fundamental-programs-in-python\/\">Python Programming Examples | Fundamental Programs in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/spell-checker-in-python\/\">Spell Checker in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/portfolio-management-system-in-python\/\">Portfolio Management System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/stickman-game-in-python\/\">Stickman Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/contact-book-project-in-python\/\">Contact Book project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/loan-management-system-project-in-python\/\">Loan Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/cab-booking-system-in-python\/\">Cab Booking System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/brick-breaker-game-in-python\/\">Brick Breaker Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tank-game-in-python\/\">Tank game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-piano-in-python\/\">GUI Piano in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ludo-game-in-python\/\">Ludo Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/rock-paper-scissors-game-in-python\/\">Rock Paper Scissors Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/snake-and-ladder-game-in-python\/\">Snake and Ladder Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/puzzle-game-in-python\/\">Puzzle Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/medical-store-management-system-project-in-python\/\">Medical Store Management System Project in\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/creating-dino-game-in-python\/\">Creating Dino Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tic-tac-toe-game-in-python\/\">Tic Tac Toe Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/test-typing-speed-using-python-app\/\">Test Typing Speed using Python App<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/moviepy-python-video-editing-library\/\">MoviePy: Python Video Editing Library<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python-2\/\">Scientific Calculator in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-to-do-list-app-in-python-tkinter\/\">GUI To-Do List App in Python Tkinter<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Hey folks, as we all are now aware that Python supports thousands of modules and packages to crack any logic behind the idea! And here&#8230;<\/p>\n","protected":false},"author":62,"featured_media":9002,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1060,1061,1403],"tags":[1356,1357,1355,1358],"class_list":["post-8915","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allcategorites","category-game-in-python","category-gui-python-projects","category-python-projects","tag-tic-tac-toe","tag-tic-tac-toe-project-using-python","tag-tic-tac-toe-using-python","tag-tic-tac-toe-using-pythonproject","wpcat-22-id","wpcat-1060-id","wpcat-1061-id","wpcat-1403-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/8915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=8915"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/8915\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media\/9002"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=8915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=8915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=8915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}