You are on page 1of 19

CS

PRACTICAL
PROJECT
NAME-MADHAV GARG
ROLL. NO-14
CLASS-XII B
ACKNOWLEDGEMENT
I would like to express my heartfelt gratitude to Mr. Amit Singhal , my dedicated teacher,
whose guidance and unwavering support played an instrumental role in the successful
completion of this project. Your patience, encouragement, and expertise have been
invaluable, and I am truly fortunate to have had you as my mentor. I would also like to extend
my thanks to all the other teachers for their contributions in helping me acquire the
knowledge and resources necessary for this project. Each member of the staff, through their
commitment to education, has played a vital role in shaping my understanding of the subject
matter. I would also like to extend my thanks my fellow students and classmates for their
camaraderie and collaborative spirit, which made the journey of this project all the more
enjoyable and enlightening. This project would not have been possible without the collective
efforts and support of all these individuals. Thank you for believing in me and helping me
achieve my goals.
CERTIFICATE OF COMPLETION
This is to certify that MADHAV GARG a student of class XII - B has successfully completed the
Computer Science Project Work entitled - “TIC TAC TOE GAME” under the guidance of Mr.
Amit Singhal, subject teacher, during the academic year 2023-24 as per the guidelines
issued by Central Board of Secondary Education (CBSE).

Teacher guide
Mr. Amit Singhal
TABLE OF CONTENTS
S.No. Topic Page
1. Introduction 5
2. How it works 6
3. Source Code 7-14
4. Output 15-17
5. Bibliography 18
INTRODUCTION
Tic-tac-toe is a classic paper-and-pencil game where two players take turns
marking either an 'X' or an 'O' in a 3x3 grid. The objective is to create a row,
column, or diagonal of three of the same symbols before your opponent does.
It's a simple yet strategic game that engages critical thinking and foresight.
Players aim to outmaneuver their opponent by strategically placing their
marks to block their opponent's moves while aiming for their own winning
combination. With its straightforward rules and quick gameplay, tic-tac-toe is
a timeless game enjoyed by people of all ages.
HOW IT WORKS
What is python?

PYTHON IS A HIGH-LEVEL, VERSATILE, AND DYNAMICALLY-TYPED PROGRAMMING


LANGUAGE KNOWN FOR ITS SIMPLICITY AND READABILITY. CREATED BY GUIDO
VAN ROSSUM AND FIRST RELEASED IN 1991, PYTHON HAS BECOME ONE OF THE
MOST POPULAR PROGRAMMING LANGUAGES, PARTICULARLY IN FIELDS LIKE
WEB DEVELOPMENT, DATA ANALYSIS, ARTIFICIAL INTELLIGENCE, AND SCIENTIFIC
COMPUTING. IT EMPHASIZES CLEAN AND EASILY UNDERSTANDABLE CODE,
OFFERS AN EXTENSIVE STANDARD LIBRARY, AND HAS A STRONG AND ACTIVE
COMMUNITY. PYTHON IS OPEN SOURCE AND SUPPORTS VARIOUS
PROGRAMMING PARADIGMS, MAKING IT A VERSATILE CHOICE FOR VARIOUS
APPLICATIONS.
SOURCE CODE
import mysql.connector

# Connect to MySQL - Replace placeholders with your MySQL credentials


conn = mysql.connector.connect(
host='localhost',
user='root',
password='Maddy@123',
database='Madhav'
)
cursor = conn.cursor()
# CREATE A TABLE TO STORE GAME RESULTS IF IT DOESN'T EXIST
CURSOR.EXECUTE('''
CREATE TABLE IF NOT EXISTS GAMES (
GAME_NO INT AUTO_INCREMENT PRIMARY KEY,
WINNER VARCHAR(20)

# COMMIT THE CHANGES AND CLOSE THE CONNECTION


CONN.COMMIT()
CONN.CLOSE()
DEF DISPLAY_BOARD(BOARD):
"""DISPLAY THE TIC TAC TOE BOARD"""
PRINT("\N ", BOARD[0], "|", BOARD[1], "|", BOARD[2])
PRINT(" -----------")
PRINT(" ", BOARD[3], "|", BOARD[4], "|", BOARD[5])
PRINT(" -----------")
PRINT(" ", BOARD[6], "|", BOARD[7], "|", BOARD[8], "\N")

DEF STORE_WINNER(WINNER):
"""STORE GAME WINNER IN MYSQL DATABASE"""
CONN = MYSQL.CONNECTOR.CONNECT(
HOST='LOCALHOST',
USER='ROOT',
PASSWORD='Maddy@123',
DATABASE='Madhav'
CURSOR = CONN.CURSOR()

# INSERT THE GAME WINNER INTO THE DATABASE


CURSOR.EXECUTE('INSERT INTO GAMES (WINNER) VALUES (%S)', (WINNER,))

CONN.COMMIT()
CONN.CLOSE()

DEF CHECK_WINNER(BOARD):
"""CHECK FOR A WINNER IN THE TIC TAC TOE GAME"""
WINNING_COMBINATIONS = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
FOR COMBO IN WINNING_COMBINATIONS:
IF BOARD[COMBO[0]] == BOARD[COMBO[1]] == BOARD[COMBO[2]] != ' ':
RETURN BOARD[COMBO[0]]

IF ' ' NOT IN BOARD:


RETURN 'DRAW'

RETURN NONE

DEF PLAY_GAME():
"""PLAY THE TIC TAC TOE GAME AND STORE OVERALL WINNER"""
PLAYER1_WINS = 0
PLAYER2_WINS = 0
FOR _ IN RANGE(5):
BOARD = [' ' FOR _ IN RANGE(9)]
PLAYER = 'X'
GAME_OVER = FALSE

WHILE NOT GAME_OVER:


DISPLAY_BOARD(BOARD)
POSITION = INT(INPUT(F"PLAYER {PLAYER}, CHOOSE YOUR POSITION (1-9): ")) - 1

IF BOARD[POSITION] == ' ':


BOARD[POSITION] = PLAYER
ELSE:
PRINT("INVALID MOVE. TRY AGAIN.")
CONTINUE
WINNER = CHECK_WINNER(BOARD)
IF WINNER:
IF WINNER == 'DRAW':
PRINT("IT'S A DRAW!")
ELSE:
PRINT(F"PLAYER{WINNER} WINS THIS ROUND!")
IF WINNER == 'X':
PLAYER1_WINS += 1
ELSE:
PLAYER2_WINS += 1
GAME_OVER = TRUE
ELSE:
PLAYER = 'O' IF PLAYER == 'X' ELSE 'X'
PRINT("\NFINAL RESULT:")
IF PLAYER1_WINS > PLAYER2_WINS:
PRINT("PLAYER 1 WINS THE GAME!")
STORE_WINNER('PLAYER 1')
ELIF PLAYER2_WINS > PLAYER1_WINS:
PRINT("PLAYER 2 WINS THE GAME!")
STORE_WINNER('PLAYER 2')
ELSE:
PRINT("IT'S A TIE!")

# START THE GAME


PLAY_GAME()
OUTPUT
BIBLIOGRAPHY
1. https://youtu.be/E8fmDDtaHLU?feature=shared

2. https://youtu.be/V9MbQ2Xl4CE?feature=shared
THANK YOU

You might also like