Tic-Tac-Toe
Tic-Tac-Toe

How Create a Tic-Tac-Toe Game Using Python

How Create a Tic-Tac-Toe Game Using Python

Tabby Mungai

Tabby Mungai

September 27, 2023

Tic-tac-toe is a fun game that an individual can play anywhere and at any time 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 it as a fun and simple programming project.

Let's 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 involves two players in a game presented by X and O. The game constitutes a board comprising 9 boxes. There are a total of 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 the creation of the tic tac toe near me. Although tic tac toe without a 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 of the game played by two players where player X has won the game over player O.