0% found this document useful (0 votes)
27 views17 pages

GRADE 12 CS PROJECT (2nd Final)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

GRADE 12 CS PROJECT (2nd Final)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

KIKANI VIDHYA MANDIR

Affiliation no.1930398
School code: 55345

COMPUTER SCIENCE (083)


Grade XII

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.

.………………….. ………………….. ……………


Internal examiner External examiner Principal
ACKNOWLEDGEMENT
I would like to take the opportunity to express my gratitude to the almighty
God, for keeping me in good health through this grueling project work.

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.

I am highly indebted to the Internal Examiner Ms.G.Niranjana /Mrs.Komagal


Yallini for the constant supervision, as well as for providing necessary information
regarding the project and also for her support in completing the project.

I would like to express my gratitude towards my family members and friends


for the kind cooperation and encouragement which has helped me in the
completion of this project.

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.

HARDWARE AND SOFTWARE REQUIREMENTS


Desktop PC with
 Intel Core i5 11400F
 16 GB RAM
 500 GB SSD
 2 TB HDD
 Any Keyboard
 Any Mouse
 Any Monitor
 Python 3.9
 Pycharm Community Edition 2022.2
 Random package in Python
 String package in Python
AIM
To create a hangman game in python

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 InsertHighscore(player_name, points)


// Insert player_name and points into the highscore table

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 DisplayGameInfo(guessed_word, guessed_letters)


// Display the current state of the guessed word and guessed letters

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 DisplayOutcome(wrong_guesses, target_word, total_points)


// Display the final outcome (win or lose)
// Display the correct word
// Display total points

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

Display Welcome Message

// Main game loop


while not GameOverConditions(wrong_guesses, target_word, guessed_letters):
DisplayHangmanFigure(wrong_guesses)
DisplayGameInfo(guessed_word, guessed_letters)

player_guess = Call GetPlayerInput(guessed_letters)

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

Update guessed letters and guessed word

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)

Ask Player if They Want to Play Again


If not, set loop variable to 1
// Close MySQL connection
Close MySQL Connection

SOURCE CODE
import mysql.connector
from random import choice
import string

# Connect to MySQL (modify these parameters accordingly)


db_connection = mysql.connector.connect(
host="your_mysql_host",
user="your_mysql_user",
password="your_mysql_password",
database="your_database_name"
)
cursor = db_connection.cursor()

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 insert_highscore(player_name, points):


# Insert the player's points into the highscore table
cursor.execute("INSERT INTO highscores (player_name, points) VALUES (%s, %s)", (player_name, points))
db_connection.commit()

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 _validate_input(player_input, guessed_letters):


return (
len(player_input) == 1
and player_input in string.ascii_lowercase
and player_input not in guessed_letters
)

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 game_over(wrong_guesses, target_word, guessed_letters):


if wrong_guesses == MAX_INCORRECT_GUESSES:
return True
if set(target_word) <= guessed_letters:
return True
return False

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}")

player_name = input("Enter your name: ")


insert_highscore(player_name, points)

choicee = input("Would you like to play again? y or n: ")


if choicee != "y":
loop += 1

# Close the database connection


cursor.close()
db_connection.close()

OUTPUT
GAME
SQL DATABASE

BIBLIOGRAPHY
Computer Science With Python- 11th and 12th Sumita Arora
github.com
geeksforgeeks.org

You might also like