How Create a Tic-Tac-Toe Game Using Python Tabby Mungai, October 6, 2023October 9, 2023 Tic-tac-toe is a fun game that individual can play anywhere and at anytime as long as one has a piece of paper, a pencil and an opponent. Tic-tac-toe is a simple game that is immensely beginner friendly for individuals learning programming. Consequently, beginners can decide to undertake as a fun and simple programming project. Lets create a simple python game tic-tac-toe that you can play. The game is simple enough with a pen and paper for game programming we will divide the process into three parts. Playing The Tic-tac-toe Game A recap of the tic-tac-toe game is that it involve two players in a game presented by X and O. The game constitutes a board comprising of 9 boxes. There are a total of the 8 arrangements to win the tic-tac-toe game. The different arrangement modes can be observed below. The possible combinations can be illustrated using numerical values. Code Using python in VScode The code imports tkinter which is an instrumental GUI toolkit from tkinter import * from tkinter import messagebox def ButtonClick(button): global x_o, flag button["bg"] = "#0B8070" if button ["text"] == "" and x_o == True: button ["text"] = "x" x_o = False CheckForWin() flag = flag+1 elif button ["text"] == "" and x_o == False: button ["text"] = "o" x_o = True CheckForWin() flag = flag+1 else: messagebox.showinfo("Tic Tac Toe", "Player has already entered!") """ 1 2 3 4 5 6 7 8 9 1 5 9 3 5 7 1 4 7 2 5 8 3 6 9 """ def CheckForWin(): global button1, button2, button3, button4, button5, button6, button7, button8, button9 if button1["text"] == "x" and button2["text"] =="x" and button3["text"] =="x" or button4["text"] == "x" and button5["text"] == "x" and button6["text"] =="x" or button7["text"] == "x" and button8["text"] == "x" and button9["text"] == "x" or button1["text"] == "x" and button5["text"] =="x" and button9["text"] =="x" or button3["text"] == "x" and button5["text"] =="x" and button7["text"] == "x" or button1["text"] == "x" and button4["text"] == "x" and button7["text"] == "x" or button2["text"] =="x" and button5["text"] =="x" and button8["text"] =="x" or button3["text"] == "x" and button6["text"] =="x" and button9["text"] == "x": messagebox.showinfo("Tic Tac Toe", "Player x has Won!") elif button1["text"] == "o" and button2["text"] =="o" and button3["text"] =="o" or button4["text"] == "o" and button5["text"] == "o" and button6["text"] =="o" or button7["text"] == "o" and button8["text"] == "o" and button9["text"] == "o" or button1["text"] == "o" and button5["text"] =="o" and button9["text"] =="o" or button3["text"] == "o" and button5["text"] =="o" and button7["text"] == "o" or button1["text"] == "o" and button4["text"] == "o" and button7["text"] == "o" or button2["text"] =="o" and button5["text"] =="o" and button8["text"] =="o" or button3["text"] == "o" and button6["text"] =="o" and button9["text"] == "o": messagebox.showinfo("Tic Tac Toe", "Player O has Won!") elif flag == 8: messagebox.showinfo("Tic Tac Toe", "Game Tied!") main = Tk() main.title("Geviton Tic Tac Toe Game") x_o = True flag = 0 button1= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button1)) button1.grid(row=0, column=0) button2= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button2)) button2.grid(row=0, column=1) button3= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button3)) button3.grid(row=0, column=2) button4= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button4)) button4.grid(row=1, column=0) button5= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button5)) button5.grid(row=1, column=1) button6= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button6)) button6.grid(row=1, column=2) button7= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button7)) button7.grid(row=2, column=0) button8= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button8)) button8.grid(row=2, column=1) button9= Button(main, text="", font= ("arial", 60, "bold"),bg="#F97316", fg="white", width=3, command=lambda:ButtonClick (button9)) button9.grid(row=2, column=2) main.mainloop() The code allows creation of the tic tac toe near me. Although, tic tac toe without computer is great and simple once you code your own game you will be able to appreciate the code even more. Below is the first outcome the game played by two players where player X has won the game over the player O. Programming
Programming How To Set Up A Simple Website September 14, 2023October 4, 2023 This blog is a guide to making a website with python. The blog provide insights of creating a website is highly rewarding one can make a website and earn money. Read More
Programming Adding Features To A Python-Flask Website September 22, 2023September 25, 2023 Providing additional features in a website is immensely beneficial. As it allows website users to have ease of navigation through the website. Read More
Programming How To Create A Web Application Database September 27, 2023October 3, 2023 A database is generally a collection of information that one can store within a centralized location. Therefore, creating a database with python can be instrumental in allowing user input data to a given website. Read More