Вы находитесь на странице: 1из 10

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

A MINI PROJECT REPORT

on

“THE HANGMAN – A WORD GAME”

Name: VALLEPU ANIL


Regno.: 18113071
Class: III CSE – B
Date: 06.09.2019
MINI PROJECT REPORT

HANGMAN – A WORD GAME

PROBLEM STATEMENT

Hangman is a paper and pencil guessing game for two or more players. One player
thinks of a word and the other tries to guess it by suggesting the letters. The word to
guess is represented by a row of dashes, giving the number of letters. If the guessing
player suggests a letter which occurs in the word, the program writes it in all its correct
positions. If the suggested letter does not occur in the word, the other player draws one
element of the hangman diagram as a tally mark. The game is over when:

The guessing player completes the word, or guesses the whole word correctly.

HOW TO PLAY:

The code will generate a word which has to be guessed by the player. So, at the output
screen will exist marked out blanks (short lines) for each letter of a word. Then the
player will guess a letter. If that letter is in the word(s) then the project will write the
letter at everyplace it appears, and cross out that letter in the alphabet. If the letter isn't
in the word then we cross out the lifelines (which are usually a finite no. of chances)
from the list. The player will continue guessing the letters until he can either solve the
word (or phrase) or he will end up losing all the lifelines and he will be declared
a LOSER.

So, it is basically a TWO PLAYER game.But in project a single player plays the game
and the rules are strictly followed by the programme.
THE STRATEGY OF THE GAME - GOALS and DELIVERABLES

In the English language, the 12 most commonly occurring letters in descending order
are: e-t-a-o-i-n-s-h-r-d-l-u. This and other letter-frequency lists are used by the guessing
player to increase the odds when it is their turn to guess. On the other hand, the same
lists can be used by the puzzle setter to stump their opponent by choosing a word which
deliberately avoids common letters or one that contains rare letters.

Another common strategy is to guess vowels first, as English only has six vowels
(a,e,i,o,u, and y), and almost every word has at least one.

Thus the user wins if he can guess the word or else he is a loser.In this programming
assignment I intend to implement the user interface by which the code takes input as
letters of the word and checks for its presence. Also another task is to reduce the no. of
chances (lifelines) one by one as the user keeps on guessing incorrect letters.

The mini project will obviously illustrate the above mentioned task , and if time permits
even intend to enhance the Console window.
PROGRAM:

import random

class HangMan(object):

# Hangman game
hang = []
hang.append(' +---+')
hang.append(' | |')
hang.append(' |')
hang.append(' |')
hang.append(' |')
hang.append(' |')
hang.append('=======')

man = {}
man[0] = [' 0 |']
man[1] = [' 0 |', ' | |']
man[2] = [' 0 |', '/| |']
man[3] = [' 0 |', '/|\\ |']
man[4] = [' 0 |', '/|\\ |', '/ |']
man[5] = [' 0 |', '/|\\ |', '/ \\ |']

pics = []

words = '''ant baboon badger bat bear beaver camel cat clam cobra cougar coyote
crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama
mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit
ram
rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger
toad trout turkey turtle weasel whale wolf wombat zebra'''.split()

infStr='_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\''

def __init__(self, *args, **kwargs):


i, j = 2, 0
self.pics.append(self.hang[:])
for ls in self.man.values():
pic, j = self.hang[:], 0
for m in ls:
pic[i + j] = m
j += 1
self.pics.append(pic)

def pickWord(self):
return self.words[random.randint(0, len(self.words) - 1)]

def printPic(self, idx, wordLen):


for line in self.pics[idx]:
print(line)

def askAndEvaluate(self, word, result, missed):


guess = input()
if guess == None or len(guess) != 1 or (guess in result) or (guess in missed):
return None, False
i=0
right = guess in word
for c in word:
if c == guess:
result[i] = c
i += 1
return guess, right

def info(self, info):


ln=len(self.infStr)
print(self.infStr[:-3])
print(info)
print(self.infStr[3:])

def start(self):
print('Welcome to Hangman !')
word = list(self.pickWord())
result = list('*' * len(word))
print('The word is: ', result)
success, i, missed = False, 0, []
while i < len(self.pics)-1:
print('Guess the word: ', end='')
guess,right = self.askAndEvaluate(word, result, missed)
if guess == None:
print('You\'ve already entered this character.')
continue
print(''.join(result))
if result == word:
self.info('Congratulations ! You\'ve just saved a life !')
success = True
break
if not right:
missed.append(guess)
i+=1
self.printPic(i, len(word))
print('Missed characters: ', missed)

if not success:
self.info('The word was \''+''.join(word)+'\' ! You\'ve just killed a man, yo !')

a = HangMan().start()
OUTPUT:

Welcome to Hangman !

The word is: ['*', '*', '*', '*', '*']

Guess the word:

****a

+---+

| |

=======

Missed characters: []

Guess the word:

****a

+---+

| |

0 |

|
=======

Missed characters: ['e']

Guess the word:

****a

+---+

| |

0 |

| |

=======

Missed characters: ['e', 'i']

Guess the word:

*o**a

+---+

| |

0 |

| |

=======
Missed characters: ['e', 'i']

Guess the word:

co**a

+---+

| |

0 |

| |

=======

Missed characters: ['e', 'i']

Guess the word:

cob*a

+---+

| |

0 |

| |

=======

Missed characters: ['e', 'i']


Guess the word:

cobra

_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_

Congratulations ! You've just saved a life !

'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'*-_-*'

Вам также может понравиться