Python Tic Tac Toe Game Project

Python Tic Tac Toe Game Project:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) Open the PyCharm code editor and create a new project in it. We named the project tic_tac_toe.py. To build a Tic Tac Toe Game in Python, we need to install the package. For this open the terminal in PyCharm and install one package i.e. tkinter. You install this package by simply typing in the terminal following command i.e. pip install tk and then press enter as shown below.

pip install tk in python

Note: If the package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here.

(2) Now write the following code for creating Python Tic Tac Toe Game Project as shown below.

Python Tic Tac Toe Game Project Code 1
Python Tic Tac Toe Game Project Code 2
Python Tic Tac Toe Game Project Code 3
Python Tic Tac Toe Game Project Code 4

Python Tic Tac Toe Game Project Source Code:

from tkinter import *
import random

def next_turn(row, column):

    global player
    if btn[row][column]["text"] =="" and check_winner() is False:

        if player == players[0]:
            btn[row][column]["text"] = player
            if check_winner() is False:
                player = players[1]
                label.config(text=(players[1]+" turn"))
            elif check_winner() is True:
                label.config(text=(players[0] + " Wins"))
            elif check_winner() == "Tie":
                label.config(text=("Tie!"))

        else:
            btn[row][column]["text"] = player
            if check_winner() is False:
                player = players[0]
                label.config(text=(players[0] + " turn"))
            elif check_winner() is True:
                label.config(text=(players[1] + " Wins"))
            elif check_winner() == "Tie":
                label.config(text=("Tie!"))

def check_winner():

    for row in range(3):
        if btn[row][0]["text"] == btn[row][1]["text"] == btn[row][2]["text"] != "":
            btn[row][0].config(bg="#D288EA")
            btn[row][1].config(bg="#D288EA")
            btn[row][2].config(bg="#D288EA")
            return True

    for column in range(3):
        if btn[0][column]["text"] == btn[1][column]["text"] == btn[2][column]["text"] != "":
            btn[0][column].config(bg="#D288EA")
            btn[1][column].config(bg="#D288EA")
            btn[2][column].config(bg="#D288EA")
            return True

    if btn[0][0]["text"] == btn[1][1]["text"] == btn[2][2]["text"] !="":
        btn[0][0].config(bg="#D288EA")
        btn[1][1].config(bg="#D288EA")
        btn[2][2].config(bg="#D288EA")
        return True

    elif btn[0][2]["text"] == btn[1][1]["text"] == btn[2][0]["text"] !="":
        btn[0][2].config(bg="#D288EA")
        btn[1][1].config(bg="#D288EA")
        btn[2][0].config(bg="#D288EA")
        return True

    elif empty_spaces() is False:
        for row in range(3):
            for column in range(3):
                btn[row][column].config(bg="#F5B041")
        return "Tie"

    else:
        return False

def empty_spaces():

    total_spaces = 9
    for row in range(3):
        for column in range (3):
            if btn[row][column]["text"] !="":
                total_spaces -=1

    if total_spaces == 0:
        return False
    else:
        return True

def new_game():

    global player
    player = random.choice(players)
    label.config(text=player+" turn")

    for row in range(3):
        for column in range(3):
            btn[row][column].config(text="",bg="#F0F0F0")

ttt = Tk()
ttt.title("Tic Tac Toe Game")
players = ["X","O"]
player = random.choice(players)
btn = [[0,0,0],
      [0,0,0],
      [0,0,0]]

label = Label(text=player + " turn", font=("Open Sans",40))
label.pack(side="top")

reset_btn = Button(text="Restart",font=("Open Sans",20), command=new_game)
reset_btn.pack(side="top")

frame = Frame(ttt)
frame.pack()

for row in range(3):
    for column in range(3):
        btn[row][column] = Button(frame, text="",font=("Open Sans",40), width=5, height=2,
                                   command=lambda row=row, column=column: next_turn(row,column))
        btn[row][column].grid(row=row,column=column)

ttt.mainloop()

(3) After running the code, the output window will pop us as shown below.

tic tac game output window
python tic tac toe game tie
tic tac toe game x win python
tic tac game zero wins python

Tutorial MySQL
Tutorial Python
Characteristics of Computer
Wired Networking
TELNET
Application Software
Malicious Software (Malware)
Object-Oriented Programming (OOP)
Python (programming language)– Wikipedia

Comments (No)

Leave a Reply