0% found this document useful (0 votes)
7 views15 pages

Python Tic Tac Toe Game with GUI

This document outlines the development of a Tic Tac Toe game using Python 3.12.6 and Tkinter, featuring a graphical user interface, real-time game logic, and a scoreboard for tracking player statistics. The game allows two players to compete locally with interactive controls and smooth animations, and it requires no external libraries for portability. Future improvements include adding an AI opponent, enhancing user experience, and upgrading the visual interface.

Uploaded by

Samuel Jayanth B
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Python Tic Tac Toe Game with GUI

This document outlines the development of a Tic Tac Toe game using Python 3.12.6 and Tkinter, featuring a graphical user interface, real-time game logic, and a scoreboard for tracking player statistics. The game allows two players to compete locally with interactive controls and smooth animations, and it requires no external libraries for portability. Future improvements include adding an AI opponent, enhancing user experience, and upgrading the visual interface.

Uploaded by

Samuel Jayanth B
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction

About this project:


This project develops a Tic Tac Toe game in Python 3.12.6 using
Tkinter with a clean, interactive GUI.
It includes real-time game logic for win/draw detection, turn
switching, and animated visual effects.
A scoreboard tracks player statistics, supported by controls like New
Game, Undo, and Reset Scores.
The game is portable, easy to run, and requires no external libraries

About the programming language used:


The project is built using Python 3.12.6, a high-level, interpreted
programming language known for its simplicity and readability.
Its built-in Tkinter library is used to create the graphical user interface
without external dependencies.
Python’s versatility and rich standard library make it ideal for rapid
development of interactive desktop applications like this game.

About this project:


This project is a GUI-based Tic Tac Toe game developed in Python
3.12.6 using the Tkinter library.
It allows two local players to compete on a visually appealing,
interactive game board.
The game features smooth animations, hover effects, and color-coded
X and O markers for clarity.
Real-time logic handles win detection, draw conditions, and
automatic turn switching.
A scoreboard tracks player scores and draws, with options for New
Game, Undo, and Reset Scores.
Designed for portability, it runs without external libraries, making it
easy to play on any system with Python installed.
Proposed system

Salient features:
 Graphical User Interface (GUI) built using Tkinter for an
interactive and user-friendly experience.
 Two-player local gameplay with alternating turns between X
and O.
 Real-time game logic including win detection, draw handling,
and automatic turn switching.
 Smooth animations for drawing Xs and Os along with hover
highlights for better user engagement.
 Scoreboard tracking for X, O, and Draw counts.
 Control options including New Game, Undo Move, and Reset
Scores.
 Portable and lightweight, requiring no external libraries for
installation.

Working description:
The Tic Tac Toe game starts with a clean 3×3 grid displayed in a
Tkinter window. Two players take turns selecting empty cells by
clicking on them, placing X or O based on whose turn it is. The
game logic checks after every move to determine if a player has
won, if the match is a draw, or if it should continue. Win
scenarios highlight the winning line, while draws are announced
through the status display. The scoreboard updates automatically,
and players can start a new round, undo the last move, or reset
scores entirely. The design prioritizes a smooth, responsive
experience with visual cues and intuitive controls.
About the IDLE / IDE used:
The project was developed using IDLE (Integrated Development
and Learning Environment), which comes bundled with Python.
IDLE offers a simple, lightweight environment with features such
as syntax highlighting, auto-indentation, and an interactive shell,
making it well-suited for Tkinter-based projects. However, the
code is also compatible with other IDEs like PyCharm, VS Code,
or Thonny.

About the module and packages (used):


 Tkinter – The built-in Python library for creating graphical
user interfaces, used for drawing the game grid, handling
user input, and displaying interactive elements.
 Tkinter. messagebox – Used for displaying pop-up messages
(optional in the code, can be integrated for notifications).
No third-party packages are required, making the program
easy to run on any system with Python installed

System requirements:
The project requires Python 3.10 or later installed on the system.
Tkinter, the standard Python GUI library, comes pre-installed
with most Python distributions, including the official installer
from [Link]. No additional installation is required if Tkinter
is already bundled with Python.
To verify Tkinter is installed, run the following command in the
terminal or command prompt:
Flow chart
Source code
import time
from colorama import Fore, Style, init

# Initialize colorama for Windows


init(autoreset=True)

def print_board(board, highlight_cells=None, flash=False):


"""
Prints the board. If highlight_cells is provided, those cells flash in
green.
"""
print()
for i, row in enumerate(board):
colored_row = []
for j, cell in enumerate(row):
if highlight_cells and (i, j) in highlight_cells:
if flash:
colored_row.append([Link] + cell +
Style.RESET_ALL if cell != " " else cell)
else:
colored_row.append([Link] + cell +
Style.RESET_ALL)
else:
if cell == "X":
colored_row.append([Link] + cell +
Style.RESET_ALL)
elif cell == "O":
colored_row.append([Link] + cell +
Style.RESET_ALL)
else:
colored_row.append(cell)
print(" | ".join(colored_row))
if i < 2:
print("-" * 9)
print()

def check_win(board, player):


# Rows
for i in range(3):
if all(board[i][j] == player for j in range(3)):
return True, [(i, j) for j in range(3)]
# Columns
for j in range(3):
if all(board[i][j] == player for i in range(3)):
return True, [(i, j) for i in range(3)]
# Main diagonal
if all(board[i][i] == player for i in range(3)):
return True, [(i, i) for i in range(3)]
# Anti-diagonal
if all(board[i][2 - i] == player for i in range(3)):
return True, [(i, 2 - i) for i in range(3)]
return False, []

def check_draw(board):
return all(cell != " " for row in board for cell in row)

def flash_winning_line(board, highlight_cells):


for _ in range(6): # Flash 6 times
print_board(board, highlight_cells, flash=True)
[Link](0.3)
print_board(board)
[Link](0.3)

def show_score(score_X, score_O):


crown_X = crown_O = ""
if score_X > score_O:
crown_X = " �"
elif score_O > score_X:
crown_O = " �"

print(f"{[Link]}Player X:
{score_X}{crown_X}{Style.RESET_ALL} | {[Link]}Player O:
{score_O}{crown_O}{Style.RESET_ALL}")
def tic_tac_toe():
score_X, score_O = 0, 0 # Initialize scores

while True: # Replay loop


board = [[" " for _ in range(3)] for _ in range(3)]
current_player = "X"

print("\nWelcome to Tic Tac Toe!")


show_score(score_X, score_O)
print_board(board)

while True:
try:
row = int(input(f"Player {current_player}, enter row (1-3):
")) - 1
col = int(input(f"Player {current_player}, enter col (1-3): "))
-1

if row not in range(3) or col not in range(3):


print("Invalid position. Choose 1-3 for both row and
column.")
continue

if board[row][col] != " ":


print("That cell is already taken! Choose another.")
continue
board[row][col] = current_player
print_board(board)

win, winning_cells = check_win(board, current_player)


if win:
print(f"� Player {current_player} wins!")
flash_winning_line(board, winning_cells)
if current_player == "X":
score_X += 1
else:
score_O += 1
break

if check_draw(board):
print("It's a draw!")
break

current_player = "O" if current_player == "X" else "X"

except ValueError:
print("Please enter numbers only.")

# Ask if players want to play again


choice = input("Play again? (y/n): ").strip().lower()
if choice != "y":
print("\n� Final Scores �")
show_score(score_X, score_O)
print("Thanks for playing! �")
break

if __name__ == "__main__":
tic_tac_toe()
OUTPUT
Game screen

First move
Display at end of game
Scope for improvement|

User Experience Enhancements:


 Replay Option: Allow players to restart the game without manually
re-running the program, possibly with a scoreboard tracking wins
across rounds.
 Player Names: Let users input their names, replacing generic
“Player X” and “Player O” labels for a more personalized feel.
 Input Guidance: Display a numbered board before each turn to
remind players which coordinates correspond to which cell.

Game Logic Improvements:

 AI Opponent: Implement a single-player mode with a computer


opponent, ranging from an easy random move generator to a
perfect-play Minimax algorithm.
 Win Highlighting: Visually emphasize the winning row, column, or
diagonal using color or symbols.
 Move Suggestions: Add a “hint” feature that recommends optimal
moves.

Visual & Interface Upgrades:


 Color Coding: Use ANSI escape codes to color X’s and O’s
differently for better readability.
 Graphical UI: Upgrade to a graphical interface using libraries like
Tkinter or Pygame, enabling clickable cells rather than manual
coordinate entry.
 Animations: Introduce subtle animations (like a blink effect for the
last move) to make the game more dynamic.
Error Handling & Validation:
 Robust Input Checking: Improve error handling for unexpected or
invalid inputs, such as non-numeric entries or extra spaces.
 Undo Feature: Allow players to undo their last move to correct
mistakes without restarting the round.
 Timeout Handling: Optionally set a time limit per turn to keep
gameplay fast-paced.\

Portability & Distribution


 Executable Packaging: Bundle the game as a .exe (using
PyInstaller) for Windows or as a cross-platform package for
macOS and Linux.
 Mobile Compatibility: Consider porting the game to Android or
iOS using frameworks like Kivy.
bibliography
1. Python tkinter Library:
[Link]
2. Data Visualization
Plotly: [Link]
3. Multilingual Support
Flask-Babel: [Link]
4. Class 11 ncert textbook
5. Class 12 ncert textbook
6. Advanced query features
Python CSV: [Link]

You might also like