0% found this document useful (0 votes)
8 views

Computer Science Project File

Uploaded by

hardiksuneja07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Computer Science Project File

Uploaded by

hardiksuneja07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

MODEL SCHOOL

ROHTAK

Name :
Class : 12
Section :
Session : 2024-25

Submitted By :
Submitted To : Mrs Arti
Acknowledgement

I would like to express my heartfelt gratitude to all those who contributed to


the successful completion of this project on the game Tic Tac Toe.

First and foremost, I extend my sincere thanks to my computer science


teacher, Mrs Arti, for their guidance, support, and valuable insights throughout
the project. Their encouragement motivated me to explore this topic in depth.

Finally, I am grateful to my family for their unwavering support and


encouragement, which inspired me to pursue this project with dedication and
enthusiasm.

Thank you all for your contributions!


Certificate of Completion

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.

Teacher’s Signature: ____________________


Index/Table of Contents

1. Cover Page

2. Acknowledgement

3. Certificate

4. Index/Table of Contents

5. Introduction

6. Game code

7. Functions, libraries, loops etc used in 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.

The primary objective of this project is to demonstrate how to develop an


interactive graphical game that handles user input, performs logical
checks, and updates the interface in real-time. The project not only helps
improve programming skills but also allows an understanding of how to
manage GUI components, handle game logic, and implement functionality
such as win conditions and resetting the game.

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.

This project serves as an excellent example of how simple Python libraries


like Tkinter can be used to create engaging applications that combine
programming logic with user interaction. By the end of this project, I have
learned the intricacies of GUI development and enhanced my
understanding of how to create interactive applications from scratch.
Game Code

import tkinter as tk

from tkinter import messagebox

# Function to handle the player's move

def player_move(row, col):

global current_player

# If the selected cell is already taken, ignore the move

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

return

# Update the board with the current player's symbol

board[row][col] = current_player

buttons[row][col].config(text=current_player)

# Check if the current player has won

if check_winner():

messagebox.showinfo("Game Over", f"Player {current_player} wins!")

reset_game()

return
# Check for a draw

if check_draw():

messagebox.showinfo("Game Over", "It's a draw!")

reset_game()

return

# Switch player after the move

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

# Function to check if there is a winner

def check_winner():

# Check rows and columns

for i in range(3):

if board[i][0] == board[i][1] == board[i][2] != " ":

return True

if board[0][i] == board[1][i] == board[2][i] != " ":

return True

# Check diagonals

if board[0][0] == board[1][1] == board[2][2] != " ":

return True

if board[0][2] == board[1][1] == board[2][0] != " ":

return True

return False
# Function to check if the game is a draw

def check_draw():

for row in board:

if " " in row:

return False

return True

# Function to reset the game

def reset_game():

global current_player, board, buttons

# Reset the board and buttons

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

current_player = "X"

for row in range(3):

for col in range(3):

buttons[row][col].config(text="")

# Create the main window

root = tk.Tk()

root.title("Tic Tac Toe")

# Initialize the board and current player

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

current_player = "X"
# Create buttons for the Tic Tac Toe grid

buttons = [[None for _ in range(3)] for _ in range(3)]

for row in range(3):

for col in range(3):

buttons[row][col] = tk.Button(root, text=" ", font=("Arial", 20), width=5, height=2,

command=lambda r=row, c=col: player_move(r, c))

buttons[row][col].grid(row=row, column=col)

# Create a Reset button

reset_button = tk.Button(root, text="Reset Game", font=("Arial", 15), command=reset_game)

reset_button.grid(row=3, column=0, columnspan=3)

# Run the GUI main loop

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. for i in range(3) (in check_winner() and


reset_game()):
o Purpose: This loop iterates through the rows and columns of the
board.
o In check_winner(): It checks if all the cells in any row or
column are the same (either 'X' or 'O') by comparing the values in
the cells.
o In reset_game(): It resets each row of the board to an empty
state (represented by " ").
2. for row in range(3) and for col in range(3) (in
reset_game() and button creation):
o Purpose: These loops are used to create the 3x3 grid of buttons in
the Tkinter window. Each button corresponds to a cell on the Tic
Tac Toe board.
o In button creation: A button is created for each cell (a total of 9
buttons), and their actions (player moves) are handled by
associating each button with the player_move() function.
Bibliography

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

You might also like