CS Project
CS Project
CS Project
VIDYALAYA
ANDREWS GANJ
2024 - 2025
Teacher’s Signature
ACKNOWLEDGEMENT
I wish to express our sincere thanks to
Dr. Rajkumar Sharma Principal, Kendriya Vidyalaya
Andrews Ganj, New Delhi for guiding us to cause the
successful outcome of the project.
2. Game Logic:
o The game asks a set of pre-defined questions (related to science).
o For each question, the player must choose an answer (A, B, C, or D). Their response is then validated.
o The score is calculated based on the number of correct answers and is displayed as a percentage.
4. Replay Option:
o The game asks if the player wants to play again after each game session. If they choose "yes", the game
starts again; otherwise, the program ends.
Code Flow:
1. Connecting to MySQL:
o The script connects to a local MySQL server and attempts to create and use the database gamedb.
2. Game Initialization:
o A series of science-related questions and answer options are defined in the dictionary questions and
options.
o The new_game() function randomly selects five questions and presents them to the player.
o Player responses are collected and compared to the correct answers, and the score is updated
accordingly.
4. Game Replay:
o The player is asked if they want to play again after each round. If they answer "yes", the game
continues; otherwise, it ends.
Closing the Connection:
After the player finishes the game and decides not to replay, the MySQL connection is closed using
conn.close().This script combines a simple quiz game with MySQL integration to track and store player
scores, and it provides a nice interactive experience!
SOURCE CODE
import mysql.connector
import random
from mysql.connector import Error
cursor = conn.cursor()
# Create tables to store player info and scores if they don't exist
cursor.execute('''CREATE TABLE IF NOT EXISTS players (
playerid INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
)''')
def play_again():
response = input("Do you want to play again? (yes or no): ").upper()
return response == "YES"
def get_player_id(name):
cursor.execute("SELECT playerid FROM players WHERE name = %s", (name,))
result = cursor.fetchone()
if result:
return result[0] # Return the playerid if found
else:
# If player does not exist, insert into players table and return the new playerid
cursor.execute("INSERT INTO players (name) VALUES (%s)", (name,))
conn.commit()
return cursor.lastrowid # Return the last inserted playerid
new_game(playerid)
while play_again():
new_game(playerid)
conn.close() # Close the database connection
print("Bye!")
Purpose:
o Starts a new game for a player by presenting a set of pre-defined science questions.
o Randomly selects 5 questions from a pool and prompts the player to answer them.
o It then compares the player's answers to the correct answers and calculates the score.
o After completing the questions, it calls display_score to calculate the final score and save_score to
store it in the database.
2. display_score(correct_guesses, guesses)
Purpose:
o Calculates the player's score based on the number of correct answers.
o Displays the percentage score of correct guesses out of the total questions.
o Returns the score value to be saved in the database.
3. save_score(playerid, score)
Purpose:
o Saves the player's score in the scores table of the MySQL database by linking it to the player's unique
playerid.
o It inserts the score into the database for tracking.
4. play_again()
Purpose:
o Prompts the player to decide if they want to play the game again.
o Returns a boolean value (True if the player chooses to play again, False otherwise).
5. get_player_id(name)
Purpose:
o Checks if a player already exists in the players table by querying the database with the player’s name.
o If the player exists, it returns their unique playerid.
o If the player does not exist, it inserts the player's name into the database and returns the new
playerid.
FLOWCHART Start
Check Answer
Correct : Increase the score
Display Score
The user’s score is calculated
based on correct answers
In this output the program starts with prompting user to enter the
name
Output 2-
Save Score
The score is stored in the
database
5 Random questions are asked by program, user selects the option and in the end it shows Score.
Yes
Close Play Again?
Output 3-
The program saves and stores player data, including their name and scores, in a MySQL database. This ensures
that player data is persistent and can be accessed later.
Storing scores in a database allows for easy retrieval and tracking of a player's performance over time.
The program automatically creates the database and tables if they do not exist, eliminating the need for manual
setup and reducing potential errors during installation.
3. User-Friendly Interface:
The program allows users to enter their name, answer questions, and view their score. It uses an easy-to-
understand format, providing a simple yet engaging user experience.
It provides immediate feedback by informing the user whether their answer is correct or wrong.
After answering all questions, the program calculates the score as a percentage, offering immediate
performance feedback to the user.
It also displays the result in a clear and understandable format.
The program randomly selects 5 questions for each game, ensuring that each gameplay experience is different
and increasing replayability.
This randomness reduces predictability and makes the game more engaging.
6. Replayability:
Users can choose to play again after finishing a round, keeping the experience fresh and encouraging multiple
rounds of gameplay.
7. Player Tracking:
By associating each player with a unique ID, the program allows the system to track each player’s performance
over time.
The program checks if a player already exists in the database and retrieves their scores accordingly, which helps
in maintaining a history of their scores.
The program provides feedback on wrong answers by showing the correct one. This encourages learning and
helps users improve their knowledge over time.
9. Scalable for More Questions:
The pool of questions and answers can easily be expanded, allowing the game to grow and include more
questions as needed. This scalability keeps the program relevant for long periods.
The program is straightforward, focusing on a simple game mechanic without unnecessary complexity. The code
is easy to understand and maintain.
The program focuses on functionality over complexity, making it more accessible for beginner programmers or
users.
By using MySQL, the program can handle large amounts of player data and scores effectively. MySQL also
provides robust data management capabilities, which can be beneficial if the game scales up with more users.
The program includes error handling for database connections and operations, ensuring that the system is more
resilient to potential issues like connection errors.
By asking questions related to science and general knowledge, the program offers an educational aspect to the
user, making it not just a game but also a tool for learning.
DISADVANTAGES
1. Limited Question Pool:
The program uses a fixed set of 25 questions, which can make the game repetitive over time. Players may
encounter the same questions frequently, leading to boredom and reduced replay value.
The question pool is not dynamic, meaning once a user plays a certain number of times, they may already know
the answers.
The program relies on the command line interface (CLI) for interaction, which can be off-putting for users who
prefer graphical user interfaces (GUIs). A more interactive UI (e.g., using Tkinter or another GUI library) would
make the game more accessible and engaging for a wider audience.
The program assumes that users will input valid responses (A, B, C, or D) for each question. If a user types
something other than these options (e.g., "E"), the program will not handle this error, leading to unexpected
behavior or crashes.
There’s no check to ensure valid player names when entering into the system, meaning users could input names
with special characters, spaces, or other unanticipated formats that may cause issues.
There is no timer or limit on how long a player can take to answer each question. This could lead to prolonged
sessions and may not be ideal for users looking for a fast-paced game experience.
Without a time limit, players could potentially cheat or look up answers online, which undermines the purpose
of testing knowledge.
While the program gives feedback for wrong answers, it does not explain why an answer is correct. This can be a
missed opportunity to educate the user further. It could improve the learning experience if it included
explanations for correct answers.
The questions are random, and there is no option to choose questions based on categories (e.g., physics,
chemistry, biology). This limits the educational potential and makes the game feel more like a trivia game than a
focused learning tool.
7. Database Dependency:
The program is dependent on a MySQL database to store player data and scores. If the database connection fails
or is unavailable, the program will not function correctly. Users who do not have MySQL installed or configured
will not be able to run the program.
8. Lack of Documentation:
The program does not have comments or documentation explaining its logic. This makes it difficult for others to
modify, maintain, or extend the program. Clear documentation would help others understand how the code
works and how to improve it.
The feedback on correct or wrong answers is limited to just text output. There are no graphical elements or
sound effects to enhance user interaction. This may make the game less engaging and entertaining for some
users.
Once the game finishes, there is no functionality for the player to view or review their past scores. A user would
need to go into the database manually to see their score history, which is not user-friendly.
The program does not have a "profile" for players, meaning it is not possible to track their progress over time in
a meaningful way.
BIBLIOGRAPHY
w3schools.com
Chatgpt
YouTube
(Internet)