GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Python Tic Tac Toe Game Project

Gk Scientist December 22, 2022 No Comments
Tweet WhatsApp Telegram

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 - Python Tic Tac Toe Game Project

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
Python Tic Tac Toe Game Project Code 2 - Python Tic Tac Toe Game Project
Python Tic Tac Toe Game Project Code 3 - Python Tic Tac Toe Game Project
Python Tic Tac Toe Game Project Code 4 - Python Tic Tac Toe Game Project

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 Project
python tic tac toe game tie - Python Tic Tac Toe Game Project
tic tac toe game x win python - Python Tic Tac Toe Game Project
tic tac game zero wins python - Python Tic Tac Toe Game Project

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

Related Articles

Create Java FileChooser
Create Java FileChooser: We can use the Eclipse IDE for …
Gk Scientist January 18, 2023 Computer

Create Java FileChooser

Automate WhatsApp Messages with Python
Automate WhatsApp Messages with Python: We can use the PyCharm …
Gk Scientist December 21, 2022 Computer

Automate WhatsApp Messages with Python

Converting JSON to Python Objects
Converting JSON to Python Objects: We can use the PyCharm …
Gk Scientist December 4, 2022 Computer

Converting JSON to Python Objects

Python MySQL IN Operator
Python MySQL IN Operator: (1) The IN operator is used …
Gk Scientist December 16, 2022 Computer

Python MySQL IN Operator

Python Data Types
Python Data Types: We can use the PyCharm code editor …
Gk Scientist November 23, 2022 Computer

Python Data Types

Python Random Module
Python Random Module: We can use the PyCharm code editor …
Gk Scientist December 3, 2022 Computer

Python Random Module

List Iteration in Python
List Iteration in Python: We can use the PyCharm code …
Gk Scientist November 27, 2022 Computer

List Iteration in Python

Computer Output Devices
Computer Output Devices: An output device is a device that …
Gk Scientist August 4, 2022 Computer

Computer Output Devices

Leave a Reply Cancel Reply

Search

  • Popular
  • Recent




GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (33) Biology Questions (88) Chemistry (57) Computer (215) Current Affairs (4) Current Content (0) Economy (17) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (114) Physics (89) Political Science (132) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...