Computer Science Project File
Computer Science Project File
ROHTAK
Name :
Class : 12
Section :
Session : 2024-25
Submitted By :
Submitted To : Mrs Arti
Acknowledgement
This is to certify that name has successfully completed the project titled "Tic-
Tac-Toe" for Class 12 Computer Science. This project involved the making of
the game tic tac toe.
1. Cover Page
2. Acknowledgement
3. Certificate
4. Index/Table of Contents
5. Introduction
6. Game code
8. Bibliography
Introduction
The Tic Tac Toe game, also known as noughts and crosses, is a classic
two-player game that has been popular for generations. It is a simple yet
engaging game where two players take turns marking spaces on a 3x3
grid with either an "X" or an "O." The goal is to place three of one's marks
in a row, column, or diagonal before the opponent does.
In this project, I have created a graphical version of the Tic Tac Toe
game using the Python programming language and the Tkinter
library. Python is a versatile and easy-to-learn programming language
that is widely used for various types of software development, including
both console and graphical applications. Tkinter, a built-in Python library,
is commonly used for creating graphical user interfaces (GUIs) and is ideal
for projects that require a simple and responsive interface.
Project Objectives:
1. Create a graphical user interface (GUI) for the Tic Tac Toe game
using Tkinter.
2. Implement game logic to detect winning conditions and determine if
the game ends in a draw.
3. Allow two players to play the game interactively, taking turns to place
'X' and 'O' on the grid.
4. Provide a reset option to start a new game once the current one ends.
5. Enhance Python programming skills by combining concepts like
functions, loops, conditionals, and working with GUI elements.
import tkinter as tk
global current_player
return
board[row][col] = current_player
buttons[row][col].config(text=current_player)
if check_winner():
reset_game()
return
# Check for a draw
if check_draw():
reset_game()
return
def check_winner():
for i in range(3):
return True
return True
# Check diagonals
return True
return True
return False
# Function to check if the game is a draw
def check_draw():
return False
return True
def reset_game():
current_player = "X"
buttons[row][col].config(text="")
root = tk.Tk()
current_player = "X"
# Create buttons for the Tic Tac Toe grid
buttons[row][col].grid(row=row, column=col)
root.mainloop()
Functions Used in the Code:
1. player_move(row, col):
o Purpose: This function handles the logic when a player clicks a
cell to make their move. It checks whether the cell is already
occupied, updates the board, and calls functions to check if the
player has won or if the game has ended in a draw.
o Parameters: row and col represent the row and column of the
clicked cell on the board.
o Logic:
▪ It first checks if the cell is already filled. If yes, it ignores the
click.
▪ If the cell is empty, the player's symbol ('X' or 'O') is placed
in that cell, and the button text is updated accordingly.
▪ Then, it checks for a winner or a draw using the
check_winner() and check_draw() functions.
2. check_winner():
o Purpose: This function checks if either player has won by aligning
three of their marks in a row, column, or diagonal.
o Logic:
▪ It checks each row, each column, and the two diagonals. If
any row, column, or diagonal has all three cells filled with
the same player's symbol ('X' or 'O'), it returns True
indicating a win.
3. check_draw():
o Purpose: This function checks if the game has ended in a draw,
i.e., when all cells are filled, but there is no winner.
o Logic:
▪ It iterates through all the rows in the board and checks if
there is any empty cell (represented by " "). If there are no
empty cells and no winner, the game is a draw.
4. reset_game():
o Purpose: This function resets the game board, allowing the players
to start a new game.
o Logic:
▪ It clears the board by resetting all cells to " " (empty).
▪ It updates the interface by resetting all button texts to empty.
▪ It also resets the current player to "X" (the first player).
Libraries Used:
1. tkinter:
o Purpose: Tkinter is the standard GUI (Graphical User Interface)
library for Python. It is used for creating windows, buttons, labels,
and other GUI components. In this project, Tkinter is used to create
the 3x3 grid of buttons (representing the Tic Tac Toe board), the
reset button, and the main window.
o Key Methods Used:
▪ tk.Tk(): Creates the main application window.
▪ tk.Button(): Creates a button widget, which is used for
each cell in the Tic Tac Toe grid and the reset button.
▪ Button.config(): Used to configure or update the
properties of the button, such as its text.
▪ messagebox.showinfo(): Displays a message box to
inform the players of the game result (win or draw).
▪ grid(): Places the button widget in the window in a grid-
like arrangement.
▪ mainloop(): Starts the Tkinter event loop, which keeps
the application running and listens for user actions.
2. messagebox:
o Purpose: Part of the Tkinter library, this is used to show pop-up
message boxes to the user. In this project,
messagebox.showinfo() is used to display the game result
(win or draw) to the players.
Loops Used in the Code:
1. Python Documentation
Python Software Foundation. (2023). The Python Language Reference.
Retrieved from https://docs.python.org/
2. Tkinter Documentation
Python Software Foundation. (2023). Tkinter — Python interface to
Tcl/Tk. Retrieved from https://docs.python.org/3/library/tkinter.html
3. GeeksforGeeks - Python Tutorials
GeeksforGeeks. (2024). Python Programming Language Tutorials.
Retrieved from https://www.geeksforgeeks.org/python-programming-
language/
4. W3Schools - Python Tkinter Tutorial
W3Schools. (2024). Python Tkinter Tutorial. Retrieved from
https://www.w3schools.com/python/python_gui_tkinter.asp
5. Stack Overflow
Stack Overflow contributors. (2024). Python Tkinter Button Grid
Example. Retrieved from https://stackoverflow.com/
6. Python Game Development with Tkinter
Chowdhury, A. (2024). Building Games with Tkinter in Python.
Retrieved from https://realpython.com