Project Report - Hangman

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

HANGMAN

PROJECT REPORT

Name: Akshat Kashyap


Class: XI – ‘B’
Roll No.: VIS-2545

1
CERTIFICATE OF AUTHENTICITY

This is to certify that Akshat Kashyap of class XII has successfully


completed the Investigatory Project on the topic “Hangman” under the
guidance of Kaavya Ma’am for the academic year 2022-2023 in the
fulfilment of the Physics/Chemistry/Biology/Computer Science/Informatics
Practices /Mathematics/ Psychology / Physical Education practical
examination conducted by the All India Senior Secondary Education.

DATE: 27/11/22
PLACE: Bangalore

Signature of Signature of Signature of


External Examiner Subject Teacher Principal

2
ACKNOWLEDGEMENT
Apart from my efforts, the success of my project is largely dependent on the
encouragement and guidelines of many others. I take this opportunity to
express my gratitude to the people who have been instrumental in the
successful completion of this project.

My sincere thanks to our computer teacher Kavya Ma’am, who critically


reviewed my project and helped in solving each and every problem that
occurred during the implementation of this project.

My heartfelt gratitude to my friends, Daman and Sanjith, who helped me


whenever I needed them. I express my heartfelt gratitude to my parents for
constant encouragement while carrying out this project.

Last but not the least, I thank the management for giving me a chance to do
this wonderful project.

“Hangman is great, it teaches you that by saying wrong things, you could end
someone’s life”
- Anonymous

3
CONTENTS

TOPIC PAGE
NO.

1.INTRODUCTION
1.1 Python Programming Language 5
1.2 Applications of Python 7

2.SYSTEM REQUIREMENTS
2.1 Software Requirements 8
2.2 Hardware Requirements 8

3.HANGMAN
3.1 Introduction to Hangman 9
3.2 Flowchart 10

4.SOURCE CODE 11

5.OUTPUT 15

6.BIBLIOGRAPHY 18

4
INTRODUCTION

1.1 Python Programming Language

Python is one of the many open source object oriented programming


applications software available in the market. Python is developed by
Guido van Rossum. Guido van Rossum started implementing Python in
1989. Python is a very simple programming language so even if you are
new to programming, you can learn python without facing any issues.
Some of the many uses of Python are application development,
implementation of automation testing process, allowing multiple
programming build, fully constructed programming library, can be used
in all the major operating systems and platforms, database system
accessibility, simple and readable code, easy to apply on complex
software development process, aids in test driven software application
development approach, machine learning/ data analytics, helps pattern
recognitions, supported in multiple tools, permitted by many of the
provisioned frameworks, etc.
Some features of Python are: -

1. Readable: Python is a very readable language.


2. Easy to learn: Learning python is easy as this is an expressive and
high level programming language, which means it is easy to
understand the language and thus easy to learn.
3. Cross platform: Python is available and can run on various operating
systems such as Mac, Windows, Linux, Unix etc. This makes it a cross
platform and portable language.
4. Open Source: Python is an open source programming language.
5. Large standard library: Python comes with a large standard library
that has some handy codes and functions which we can use while
writing code in Python.

5
6. Free: Python is free to download and use. This means you can
download it for free and use it in your application.

7. Advanced features: Supports generators and list comprehensions.

8. Automatic memory management: Python supports automatic


memory management which means the memory is cleared and freed
automatically.

6
1.2 Applications of Python programming language
Python can be used to develop different applications like web
applications, graphic user interface based applications, software
development application, scientific and numeric applications, network
programming, Games and 3D applications and other business
applications. It makes an interactive interface and easy development of
applications. There are many applications of Python, here are some of
them.
1. Web development: Web frameworks like Django and Flask are based
on Python. They help you write server side code which helps to
manage database, write backend programming logic, mapping URLs,
etc.
2. Machine learning: There are many machine learning applications
written in Python. Machine learning is a way to write a logic so that a
machine can learn and solve a particular problem on its own. For
example, products recommendation in websites like Amazon,
Flipkart, eBay etc. is a machine learning algorithm that recognises
user’s interest. Face recognition and Voice recognition in your phone
is another example of machine learning.
3. Data Analysis: Data analysis and data visualisation in form of charts
can also be developed using Python.
4. Scripting: Scripting is writing small programs to automate simple
tasks such as sending automated response emails, etc. Such types of
applications can also be written in Python programming language.
5. Game development: You can develop games using Python.
6. Desktop applications: You can develop desktop applications in
Python using libraries like TKinter or Q

7
SYSTEM REQUIREMENTS

2.1. Software Requirements


Python 3.6
Operating System Linux – Ubuntu 16.04 to 17.10, or Windows 7 to 10

2.2. Hardware Requirements


X86 64-bit CPU (Intel / AMD architecture)
4 GB RAM
5 GB free disk space

8
HANGMAN

3.1. Introduction to Hangman


Hangman is a guessing game for two or more players. One player thinks of a
word, phrase or sentence and the other(s) try to guess it by suggesting letters
within a certain number of guesses.
The word to guess is represented by a row of dashes representing each letter
of the word. Rules may permit or forbid proper nouns, such as names, places,
brands, or slang. If the guessing player suggests a letter which occurs in the
word, the other player writes it in all its correct positions. If the suggested
letter does not occur in the word, the other player draws one element of a
hanged stick figure as a tally mark.
The player guessing the word may, at any time, attempt to guess the whole
word. If the word is correct the game is over and the guesser wins. Otherwise,
the other player may choose to penalize the guesser by adding an element to
the diagram. On the other hand, if the guesser makes enough incorrect
guesses to allow the other player to complete the diagram, the guesser loses.
However, the guesser can also win by guessing all the letters that appear in the
word thereby completing the word, before the diagram is completed.
Creating a program that can play a game requires considering all the possible
situation the AI can be in during the game and how it should respond in each
of those situations.

9
3.2 Flowchart

10
SOURCE CODE
3.1 hangMan.py
# Stages of the hanging man corresponding to number of tries
stages = ['',
R'''
____________
| |
| ◯
| \|/
| /‾\
|
|
''',
'''
____________
| |
| ◯
| \|/
| /
|
|

''',
'''
____________
| |
| ◯
| \|/
|
|
|
''',
'''
____________
| |
| ◯
| |/
|
|
|
''',
'''
____________
| |
| ◯
| |
|
|
|
''',
'''
____________
| |
| ◯
|
|
|
|
''',
'''
____________
| |
|
|
|
|
|
'''

11
3.2 wordLoader.py
import random

# Asks the user which category of words they want to play.

wordCat = int(input("Welcome!\nPlease select the category of words you want


to play.\n1. Movies\n2. Cars\n"))
if wordCat not in range(1, 3):
exit()

# Chooses a random word from the text file of the category of words.

def getWord(wordCat):
if wordCat == 1:
file = open("movies.txt")
elif wordCat == 2:
file = open("cars.txt")
else:
exit()
wordList = file.read().split("\n")
word = random.choice(wordList)
return word

word = getWord(wordCat)
# Converts the word into underscore form (except special symbols)

def wordBlanks(word):
wordDisplay = ''
for char in word:
if char.isalnum():
wordDisplay += '_'
else:
wordDisplay += char
return wordDisplay

12
3.3 game.py
import wordLoader
from hangMan import stages

# Main game function

def gameLogic():
word = wordLoader.word.upper()
wordCompletion = wordLoader.wordBlanks(wordLoader.word)
guessed = False
guessedLetters = []
guessedWords = []
tries = 6
print("You have", tries, "tries left")
print(wordCompletion)
print(stages[tries])
print("\n")
while not guessed and tries > 0:
guess = input("Please guess a letter or a word: ").upper()
if len(guess) == 1 and guess.isalnum():
if guess in guessedLetters:
print("You already guessed the letter", guess)
elif guess not in word:
print(guess, "is not in the word")
tries -= 1
guessedLetters.append(guess)
else:
print("Nice!", guess, "is in the word")
guessedLetters.append(guess)
wordAsList = list(wordCompletion)
indices = [i for i, letter in enumerate(word) if letter ==
guess]
for index in indices:
wordAsList[index] = guess
wordCompletion = ''.join(wordAsList)
if '_' not in wordCompletion:
guessed = True
elif len(guess) == len(word):
if guess in guessedWords:
print("You have already guessed this word")
elif guess != word:
print(guess, "is not the word")
tries -= 1
guessedWords.append(guess)
else:
guessed = True
wordCompletion = word
else:
print("Not a valid input")
if not guessed:
print("You now have", tries, "tries left")
if tries == 0:
print("The word was", word)
else:
print(wordCompletion)
print(stages[tries])
print("\n")
if guessed:
print("You Win")

13
3.4 main.py
import game
import wordLoader
import hangMan

def main():
game.gameLogic()
while input("Do you want to keep playing? (Y/N)").upper() == "Y":
wordCat = int(input("Welcome!\nPlease select the category of words
you want to play.\n1. Movies\n2. Cars\n"))
if wordCat not in range(1, 3):
exit()
wordLoader.word = wordLoader.getWord(wordCat)
game.gameLogic()

if __name__ == "__main__":
main()

14
OUTPUT

Draws the Hangman figure.


Prints the Word in underscore form except special characters
Correctly identifies correct letter.

15
Identifies wrong letters
Draws the body of the Hangman

Identifies wrong inputs

16
Prompts to replay the game upon completion.

17
Bibliography
1. Computer Science with Python – Class XI by Sumita Arora
2. Wikipedia.org

18

You might also like