You are on page 1of 17

“Strategic of tic tac toe : Battle of X’s and O’s”

MINI PROJECT REPORT ON THEORY COURSE

ADVANCED PROGRAMMING PRACTICE (18CSC207J)

Submitted by

Yash Garg [RA2111003030046]


Abhishek Kumar [RA2111003030050]
Under the guidance of

Mr. Harendra Sharma

(Assistant Professor, Department of CSE)


in partial fulfilment for the award of the
degree of

BACHELOR OF TECHNOLOGY
In
Computer Science & Engineering

FACULTY OF ENGINEERING AND TECHNOLOGY

Delhi NCR Campus, Modinagar, Ghaziabad (U.P.)


(Under Section 3 of UGC Act, 1956)

1
BONAFIDE CERTIFICATE

Certified that this project report titled “Strategic of tic tac toe : Battle of
X’s and O’s” is the Bonafide work of “Yash Garg [RA2111003030046]
Abhishek Kumar [RA2111003030050]”, who carried out the project
work under my supervision. Certified further, that to the best of my
knowledge the work reported herein does not form any other project
report or dissertation on the basis of which a degree or award was
conferred on an earlier occasion on this or any other candidate.

SIGNATURE SIGNATURE
Mr. Harendra Sharma Dr.R.P. Mahapatra
Assistant Professor HEAD OF THE DEPARTMENT
Computer Science & Engineering Dept. of Computer Science &
Engineering

2
ABSTRACT

Tic Tac Toe is a classic two-player game played on a 3x3 grid. The
objective of the game is for a player to get three of their marks (X or O)
in a row, column, or diagonal. In this digital version of the game, players
can compete against each other locally using a graphical user interface
built with the Tkinter library in Python.

The game interface is designed to be user-friendly and easy to navigate.


The game provides a clean and minimalistic interface that enables players
to select their preferred game mode, either two-player mode or player
versus computer mode. The game also features a scoreboard that keeps
track of the number of games won by each player.

The project demonstrates the use of the Tkinter library, including widgets
such as labels, buttons, and frames, to create a graphical user interface. It
also showcases the ability to handle user input, manipulate data, and
implement game logic in Python. This Tic Tac Toe game is a fun and
engaging way to demonstrate basic programming skills while also
enjoying a timeless game.

3
ACKNOWLEDGEMENT

We would like to express my deepest gratitude to my


programming instructor Mr. Harendra Sharma for providing
guidance and support throughout the development of this project.
I am also grateful to my peers who provided valuable feedback and
suggestions, which helped improve the quality of the game.

I would also like to acknowledge the developers of the Tkinter


library for creating such a versatile tool for building graphical user
interfaces in Python. The library enabled me to create a visually
appealing game that is both fun and easy to play.

Finally, I would like to thank my family and friends for their


unwavering support and encouragement throughout my journey in
programming. Without their support, this project would not have
been possible.

4
TABLE OF C0ONTENTS
ABSTRACT 3.
ACKNOWLEDGEMENT 4.

1. Introduction 6.
1.1. Tkinter
1.2. Brief Description of the Project
1.3. Literature Survey

2. Methodology 8.
2.1. Hardware Design
2.2. Software Design

3. Implementation 10.

4. Results and Discussion 11.


4.1. Starting Screen of the Game
4.2. Beginning Section of the Game
4.3. Middle Section of the Game
4.4. Game Over Screen

5. Conclusion and Future Enhancement 13.


5.1. Conclusion
5.2. Future Enhancement
5.3. Program Code
5.4. References

5.5.

5
Chapter – 1
Introduction

1.1 Tkinter
Tkinter is a standard Python library for creating graphical user interfaces
(GUIs). It provides a set of tools and widgets to create windows, dialog
boxes, buttons, labels, menus, and many other GUI components that
enable interaction with the user. Tkinter is based on the Tcl/Tk GUI
toolkit and has been included with every Python distribution since
version 1.5.2.
Tkinter provides a powerful and flexible platform for building GUI
applications. It is easy to use, especially for beginners, as it has a
relatively simple syntax and a range of pre-built widgets that can be
easily customized to suit your needs. It also offers a range of event-
driven programming capabilities, making it possible to build interactive
applications that respond to user input.
Tkinter is widely used for a variety of applications, including scientific
and engineering simulations, data analysis tools, database front-ends,
multimedia applications, games, and many others. Its cross-platform
compatibility and simplicity make it an ideal choice for developing
applications that can run on multiple operating systems without any
modifications.
Overall, Tkinter is a versatile and easy-to-use GUI library that provides
developers with a powerful platform for building graphical user
interfaces in Python.

1.2 Brief Description of the Project


The project of Tic Tac Toe Game is a digital implementation of the
classic two-player game played on a 3x3 grid, where players aim to get
three of their marks (X or O) in a row, column, or diagonal. The project is
developed using Python programming language and the Tkinter library,
which provides the graphical user interface for the game.

The game features two modes of play, including two-player mode and
player versus computer mode. Players can select their preferred mode
from the game's interface, and the game keeps track of the number of

6
games won by each player. The player versus computer mode has
multiple difficulty levels, ranging from easy to unbeatable.

The project showcases fundamental programming concepts such as data


structures, loops, and conditional statements, and also demonstrates the
ability to create a graphical user interface and handle user input. The
game provides a fun and engaging way to demonstrate basic
programming skills while also enjoying a timeless game.

1.3 Literature Survey


Tic Tac Toe is a classic game that has been studied in the field of game
theory and artificial intelligence. Various research works have been done
on this game, including game tree analysis, optimal move strategies, and
reinforcement learning.

One such research work by Victor Allis, titled "Searching for Solutions in
Games and Artificial Intelligence," discussed the game tree analysis of
Tic Tac Toe and other board games. The paper outlined the search
algorithms used to analyze the game tree and the complexity of various
board games.

Another research work by Chan Wu, titled "Optimal Tic-Tac-Toe Moves:


Towards a Perfect Play," investigated the optimal move strategies for Tic
Tac Toe. The paper analyzed the game's symmetry and the possible move
combinations to determine the optimal moves for each player.

In the field of artificial intelligence, researchers have used Tic Tac Toe as
a benchmark for reinforcement learning algorithms. A research work by
Niklas Törnqvist, titled "Reinforcement Learning in Tic-Tac-Toe Game
Using Q-Learning," discussed the use of Q-learning algorithm to train a
computer agent to play Tic Tac Toe.

In terms of implementing Tic Tac Toe as a programming project, various


resources are available online that provide step-by-step guides and
tutorials. The Python Programming for the Absolute Beginner by Michael
Dawson is one such resource that provides a detailed explanation of the
game's implementation using Python programming language.

In summary, Tic Tac Toe is a popular game that has been studied in the
fields of game theory and artificial intelligence. Various research works
have investigated the game's strategies, complexity, and optimal moves.

7
As a programming project, Tic Tac Toe provides a fun and engaging way
to demonstrate programming skills while also enjoying a timeless game.

8
Chapter - 2
Methodology

2.1 Hardware Design


The hardware design of the Tic Tac Toe game is relatively simple since
the game runs on a computer or mobile device. The game's hardware
requirements are minimal, and the game can be played on most devices
with a modern web browser or a Python interpreter.

The hardware requirements for the game include:

1. Processor: A modern processor with a clock speed of 1 GHz or


higher is sufficient to run the game smoothly.
2. Memory: The game's memory requirements are minimal, with the
game requiring only a few megabytes of RAM.
3. Display: The game's display requirements are minimal, with a
minimum resolution of 800x600 pixels being sufficient to display
the game interface.
4. Input Devices: The game can be played using a mouse or a
touchpad on a laptop or a touch screen on a mobile device.

The hardware design of the Tic Tac Toe game does not require any
specialized hardware components, and the game can be played on most
devices without any additional hardware requirements.

2.2 Software Design


The software design of the Tic Tac Toe game involves breaking down the
project into manageable components and defining their relationships and
interactions. The software design includes the following components:

1. User Interface: This component provides the graphical user


interface for the game. It includes widgets such as labels, buttons,
and frames that enable players to interact with the game.
2. Game Board: This component defines the game board and the rules
for placing X or O marks on the board. It includes functions to
check for winning conditions and to reset the game board after each
game.

9
3. Game Logic: This component defines the game's logic, including
the rules for determining the next move and the game's outcome. It
includes functions to handle the computer-controlled opponent's
moves and to calculate the optimal moves for the computer player.
4. Scoreboard: This component keeps track of the number of games
won by each player and displays the scores on the game interface.

The software design follows the Model-View-Controller (MVC)


architecture, where the game logic acts as the model, the user interface
acts as the view, and the controller manages the interactions between the
model and view. The controller handles user input and updates the game
board and scoreboard based on the game's outcome.

The software design is implemented using the Python programming


language and the Tkinter library. The game's interface is designed to be
user-friendly, with clear instructions provided for each game mode. The
game features multiple difficulty levels for the computer-controlled
opponent, ranging from easy to unbeatable.

Overall, the software design of the Tic Tac Toe game is designed to
provide a fun and engaging way to demonstrate basic programming skills
while also enjoying a timeless game.

10
Chapter - 3

Implementation
The implementation of Tic Tac Toe game using Tkinter involves creating a
graphical user interface (GUI) using widgets and defining game logic
functions. Here are the main steps involved in the implementation:

1. Importing Required Libraries: We need to import the tkinter module to


create the GUI and random module to generate random computer moves.
2. Creating the GUI: We can create the GUI using various tkinter widgets
such as labels, buttons, frames, and canvases. The GUI can be divided
into various frames for displaying different sections such as the game
board, game mode selection, and scores. We can also set the properties
of the widgets, such as text, font, color, and size.
3. Creating the Game Board: We can create the game board by creating a
2-dimensional list of buttons. The buttons are placed on a frame and
arranged in a grid format. We can also set the button properties such as
text, font, color, and size. The buttons are configured to call a function
when clicked to place the player's move.
4. Defining the Game Logic: We need to define the game logic functions,
such as the function to place the player's move, check for a winner, and
reset the game board. We also need to define the function for the
computer's move, which generates a random move for easy and medium
difficulty levels and uses a minimax algorithm for the unbeatable level.
5. Adding Game Mode Selection: We can add a game mode selection
frame that allows the player to choose the game's difficulty level. We
can use radio buttons or dropdown menus to allow the player to select
the game mode.
6. Adding Scoreboard: We can add a scoreboard frame that displays the
number of games won by the player and computer for each difficulty
level.
7. Handling Game Outcome: We need to handle the game outcome, such
as displaying a message when the player or computer wins, updating the
scoreboard, and resetting the game board.
8. Running the Game: We can run the game by creating an instance of the
Tkinter window and calling the mainloop() function.

The implementation of Tic Tac Toe game using Tkinter in Python is relatively
simple and can be completed in a few hundred lines of code. However, it can
provide a good learning experience for beginners and an enjoyable game for
players.

11
Chapter - 4

Results and Discussion

4.1 Starting screen of the Game


The Game starts with a Title Screen .

Caption : “Welcome to tic-tac-toe”


4.2 Beginning Section of the Game

Caption : "Player X/O, it's your turn!"

12
4.3 Middle Section of the Game

Caption : “Player X wins”

4.4 Game Over Screen

Caption : “The game has ended”

13
Chapter – 5

Conclusion and Future Enhancement

5.1 CONCLUSION:
A Tic Tac Toe game is a simple but classic game that involves two
players taking turns to place X's or O's on a 3x3 grid until one player gets
three in a row, or the game ends in a tie if the grid is full and no one has
won.

To develop a Tic Tac Toe game project, you would need to use a
programming language such as Python, and a graphical user interface
(GUI) library such as Tkinter to create the game board, buttons, and other
UI elements.

The key features of a Tic Tac Toe game project would include:

1. A 3x3 game board to display the X's and O's as they are placed by
the players.
2. A way to detect when a player has won or the game has ended in a
tie, and display a message to the players.
3. A way to reset the game board and start a new game after the
current game has ended.

During the development process, you may encounter challenges such as


designing and implementing the game logic, handling user input and
button clicks, and managing the UI elements to ensure a smooth and
responsive user experience. However, by persevering through these
challenges and using debugging tools and techniques, you can create a
fun and engaging Tic Tac Toe game that showcases your programming
skills.

5.2 FUTURE ENHANCEMENTS:


There are several ways to enhance a Tic Tac Toe game project to make it
more engaging and user-friendly. Here are some potential future
enhancements to consider:

14
1. Difficulty Levels: Add different levels of difficulty to the game,
such as Easy, Medium, and Hard, to challenge players of different
skill levels. For example, the computer player in the game could
make more or less strategic moves depending on the selected
difficulty level.
2. Customizable Themes: Allow players to choose their own
background color, game board style, and sound effects to
personalize their game experience.
3. Multiplayer Online Mode: Implement a multiplayer mode that
allows players to connect and play with other users online.
4. Voice Assistant: Add a voice assistant to guide players through the
game and provide tips and strategies on how to win.
5. Animated Graphics: Enhance the graphics by adding animations
and special effects, such as explosions or fireworks, to make the
game more visually appealing.
6. Statistic Tracking: Track player statistics, such as the number of
wins, losses, and ties, to create a competitive and engaging
experience.
7. Mobile App: Develop a mobile app version of the game that can be
played on smartphones and tablets, making it accessible to a wider
audience.

By implementing some or all of these future enhancements, you can


create a more engaging and entertaining Tic Tac Toe game that will keep
players coming back for more.

5.3 Program Code :


import tkinter as tk
from tkinter import messagebox

class TicTacToe:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Tic Tac Toe")

        self.current_player = "X"
        self.moves = 0
        self.board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]

        self.create_board()
        self.create_menu()

        self.window.mainloop()

15
    def create_board(self):
        self.cells = []

        for row in range(3):


            cell_row = []
            for col in range(3):
                cell = tk.Button(self.window, text="", font=("Arial", 24), width=4, height=2,
command=lambda row=row, col=col: self.play(row, col))
                cell.grid(row=row+1, column=col, padx=5, pady=5)
                cell_row.append(cell)
            self.cells.append(cell_row)

    def create_menu(self):
        menubar = tk.Menu(self.window)
        self.window.config(menu=menubar)

        filemenu = tk.Menu(menubar)
        menubar.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="New Game", command=self.new_game)
        filemenu.add_command(label="Exit", command=self.window.quit)

    def play(self, row, col):


        if self.board[row][col] == " ":
            self.moves += 1
            self.board[row][col] = self.current_player
            self.cells[row][col].configure(text=self.current_player)

            if self.check_winner(row, col):
                messagebox.showinfo("Congratulations!", f"{self.current_player} wins!")
                self.new_game()
            elif self.moves == 9:
                messagebox.showinfo("Tie Game!", "The game ended in a tie.")
                self.new_game()
            else:
                self.switch_player()

    def check_winner(self, row, col):


        if self.board[row][0] == self.board[row][1] == self.board[row][2] != " ":
            return True
        elif self.board[0][col] == self.board[1][col] == self.board[2][col] != " ":
            return True
        elif self.board[0][0] == self.board[1][1] == self.board[2][2] != " ":
            return True
        elif self.board[0][2] == self.board[1][1] == self.board[2][0] != " ":
            return True
        else:
            return False

16
    def switch_player(self):
        if self.current_player == "X":
            self.current_player = "O"
        else:
            self.current_player = "X"

    def new_game(self):
        self.current_player = "X"
        self.moves = 0
        self.board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
        for row in self.cells:
            for cell in row:
                cell.configure(text="")
        messagebox.showinfo("New Game", "Let's play again!")

if __name__ == "__main__":
    game = TicTacToe()

5.4 References :
Here are some potential references for a Tic Tac Toe game project:

1. Pygame: A Python library for creating games, including Tic Tac Toe. This
library provides advanced features such as sound effects, animations, and
custom graphics.
2. Tkinter: A Python library for creating graphical user interfaces, including
games. This library is suitable for creating a simple Tic Tac Toe game with
basic graphics and UI elements.
3. Github: A code-sharing platform that hosts a variety of open-source Tic Tac
Toe game projects. You can search for existing Tic Tac Toe game projects on
Github to learn from the code and use it as a reference for your own project.
4. Youtube: A video-sharing platform that hosts tutorials and walkthroughs for
creating Tic Tac Toe games using various programming languages and
libraries. You can search for Tic Tac Toe game tutorials on Youtube to learn
about the game logic, user input handling, and UI design.
5. Online tutorials and courses: Several online learning platforms offer courses
and tutorials on game development and programming, including Tic Tac Toe
game development. Some popular platforms include Udemy, Coursera, and
Codecademy.

Using these resources, you can learn about the game logic, programming concepts,
and UI design necessary to create a successful Tic Tac Toe game project.

17

You might also like