GRADE 12 CS PROJECT (2nd Final)
GRADE 12 CS PROJECT (2nd Final)
Affiliation no.1930398
School code: 55345
HANGMAN GAME
Team Members:
Sai Mrithul P
Saravanan A
Vibul S
Vishal S
KIKANI VIDHYA MANDIR
Affiliation no.1930398
School code: 55345
Name: Class:
Roll No:
This is to certify that ___________________________________
_____________________________________________________
of Grade 12 ,Kikani Vidhya Mandir, Coimbatore, has successfully
completed the project in Computer Science (083) for the
AISSCE held on ___________ as prescribed by CBSE in the
academic year 2023-2024.
I have taken efforts in this project. However, it would not have been
possible without the kind support and help of many individuals. I would like to
extend my sincere thanks to all of them.
I would like to express with great pleasure and sincerity to record my thanks,
gratitude and honor to our principal Mrs. GeethaRaj and vice principal Mrs.
Jayalatha Rosalin, Management and staff for their valuable guidance and support.
Finally, I thank each and every one who helped me to complete the task.
Place: Coimbatore
Date: Name and Signature of the students
TABLE OF CONTENTS
CERTIFICATE
ACKNOWLEDGEMENT
AIM
ALGORITHM
HARDWARE & SOFTWARE REQUIREMENTS
PSEUDOCODE
SOURCE CODE
OUTPUT
BIBLIOGRAPHY
SYNOPSIS
Escape the routine with Hangman—a word-guessing game designed for intellect and fun.
The code selects words randomly, creating a diverse experience. Validate your guesses,
unravel the word, and release tension with this break-proven stress reliever.
The hangman's progress adds a captivating touch. Modular functions and a looping
structure ensure a seamless experience. After a victory or defeat, choose continuity for
ongoing enjoyment.
Hangman is more than a game; it's a mindful break cultivating linguistic and strategic
skills. Ready for the journey? Execute the program and let the wordplay begin.
Discover the thrill of deciphering words while promoting cognitive function. Embrace the
challenge as each correct guess propels you forward. Engage in this interactive pursuit, and
witness the positive impact on your mind and mood.
ALGORITHM
1. Connect to MySQL:
- Connect to the MySQL database using appropriate credentials.
2. Create Highscore Table:
- Check if the highscore table exists in the database.
- If not, create a table with columns for `id` (auto-increment), `player_name`, and
`points`.
3. Word Selection:
- Read a list of words from a file.
- Randomly select a word from the list.
4. Player Input:
- Prompt the player to guess a letter.
- Validate the input to ensure it's a single lowercase letter not guessed before.
5. Game Loop:
- Continue the game loop until either the maximum allowed incorrect guesses are
reached or the entire word is guessed.
6. Display:
- Display the hangman figure based on the number of incorrect guesses.
- Show the current state of the guessed word and guessed letters.
7. Scoring:
- Assign points for correct and incorrect guesses.
- Correct guesses have a point value of 0.
- Incorrect guesses result in a deduction of 5 points each.
- Calculate total points, ensuring it doesn't go below 0.
8. Game Over Conditions:
- Check if the game is over based on reaching maximum incorrect guesses or correctly
guessing the entire word.
9. Database Interaction:
- After the game ends, prompt the player to enter their name.
- Insert the player's name and points into the highscore table.
10. Replay Option:
- Ask the player if they want to play again.
- If not, exit the loop and close the MySQL connection.
11. Close Database Connection:
- Close the cursor and the MySQL database connection.
PSUEDO CODE
Procedure ConnectToMySQLDatabase()
// Connect to MySQL database with appropriate credentials
Procedure CreateHighscoreTable()
// Check if the highscore table exists
// If not, create a table with columns (id, player_name, points)
Procedure SelectWordFromFile()
// Read a list of words from a file
// Randomly select a word from the list
Procedure GetPlayerInput(guessed_letters)
// Prompt the player to guess a letter
// Validate input (single lowercase letter not guessed before)
Procedure DisplayHangmanFigure(wrong_guesses)
// Display the hangman figure based on the number of wrong guesses
Procedure CalculatePoints(wrong_guesses)
// Calculate points for correct and incorrect guesses
// Ensure total points don't go below 0
Procedure GameOverConditions(wrong_guesses, target_word, guessed_letters)
// Check if the game is over based on conditions
Procedure PlayHangmanGame()
// Initialize loop variable to 0
while loop is 0:
Call ConnectToMySQLDatabase()
Call CreateHighscoreTable()
// Game setup
target_word = Call SelectWordFromFile()
guessed_letters = empty set
guessed_word = BuildGuessedWord(target_word, guessed_letters)
wrong_guesses = 0
total_points = 35
if player_guess is in target_word:
Display "Great guess!"
else:
Display "Sorry, it's not there."
Increment wrong_guesses
Deduct 5 points from total points
DisplayHangmanFigure(wrong_guesses)
Call DisplayOutcome(wrong_guesses, target_word, total_points)
// Database interaction
player_name = Prompt Player to Enter Name
Call InsertHighscore(player_name, total_points)
SOURCE CODE
import mysql.connector
from random import choice
import string
def create_highscore_table():
# Create a highscore table if it doesn't exist
cursor.execute("CREATE TABLE IF NOT EXISTS highscores (id INT AUTO_INCREMENT PRIMARY KEY,
player_name VARCHAR(255), points INT)")
def select_word():
with open(r'C:\Users\ADMIN\AppData\Local\Programs\Python\Python311\words.txt', "r") as words:
word_list = words.readlines()
return choice(word_list).strip()
def get_player_input(guessed_letters):
while True:
player_input = input("Guess a letter: ").lower()
if _validate_input(player_input, guessed_letters):
return player_input
def join_guessed_letters(guessed_letters):
return " ".join(sorted(guessed_letters))
def build_guessed_word(target_word, guessed_letters):
current_letters = []
for letter in target_word:
if letter in guessed_letters:
current_letters.append(letter)
else:
current_letters.append("_")
return " ".join(current_letters)
def draw_hanged_man(wrong_guesses):
hanged_man = [
r"""
-----
| |
|
|
|
|
|
|
|
|
-------
""",
r"""
-----
| |
O |
|
|
|
|
|
|
|
-------
""",
r"""
-----
| |
O |
/ |
|
|
|
|
|
|
-------
""",
r"""
-----
| |
O |
/| |
|
|
|
|
|
|
-------
""",
r"""
-----
| |
O |
/|\ |
|
|
|
|
|
|
-------
""",
r"""
-----
| |
O |
/|\ |
/ |
|
|
|
|
|
-------
""",
r"""
-----
| |
O |
/|\ |
/\ |
|
|
|
|
|
-------
"""
]
print(hanged_man[wrong_guesses])
MAX_INCORRECT_GUESSES = 6
TOTAL_POINTS = 35
POINTS_PER_WRONG_GUESS = -5
def calculate_points(wrong_guesses):
# Calculate points based on wrong guesses
return max(TOTAL_POINTS + (POINTS_PER_WRONG_GUESS * wrong_guesses), 0)
loop = 0
while loop == 0:
# Initial setup for each game loop
target_word = select_word()
guessed_letters = set()
guessed_word = build_guessed_word(target_word, guessed_letters)
wrong_guesses = 0
points = TOTAL_POINTS
print("Welcome to Hangman!")
# Game loop
while not game_over(wrong_guesses, target_word, guessed_letters):
draw_hanged_man(wrong_guesses)
print(f"Your word is: {guessed_word}")
print(
"Current guessed letters: "
f"{join_guessed_letters(guessed_letters)}\n"
)
player_guess = get_player_input(guessed_letters)
if player_guess in target_word:
print("Great guess!")
else:
print("Sorry, it's not there.")
wrong_guesses += 1
points = calculate_points(wrong_guesses)
guessed_letters.add(player_guess)
guessed_word = build_guessed_word(target_word, guessed_letters)
# Game over
draw_hanged_man(wrong_guesses)
if wrong_guesses == MAX_INCORRECT_GUESSES:
print("Sorry, you lost!")
else:
print("Congrats! You did it!")
print(f"Your word was: {target_word}")
print(f"Your total points: {points}")
OUTPUT
GAME
SQL DATABASE
BIBLIOGRAPHY
Computer Science With Python- 11th and 12th Sumita Arora
github.com
geeksforgeeks.org