2010年6月27日 星期日

[Question 8] Hangman






Description



Hangman is a popular word guessing game where the player attempts to construct a missing word by guessing one letter at a time. After a certain number of incorrect guesses, the game ends and the player loses. The game also ends if the player correctly identifies all the letters of the missing word.
Input



Your program will prompt the user for letter guesses until the word is correctly guessed or the player has exceeded the maximum number of guesses. User input should be checked to make sure it's valid. A large file full of words is available for you to use in your program. You will certainly need to write some code to filter out the words of a certain length. Perhaps you could prompt the player for the length of the word they would like to guess and adjust the number of allowable wrong guesses accordingly.
Output



Your program should print a list of letters that have been guessed as well as display the correctly guessed letters in the word. A "graphical" representation of the hanging man (kinda gross when you think about it) is optional.
Sample Run



Welcome to hangman. You get seven chances to guess the mystery word.


_ _ _ _ _ _

Pick a letter --> e

Guessed letters: E

O

_ _ _ _ _ _

Pick a letter --> a

Guessed letters: E A

O
|

_ _ _ _ _ _

Pick a letter --> e
Sorry, you already guessed 'E'
Pick a letter --> i

Guessed letters: E A I

O
\|

_ _ _ _ _ _

Pick a letter --> o

Guessed letters: E A I O

O
\|

_ O _ _ _ _

Pick a letter --> u

Guessed letters: E A I O U

O
\|/

_ O _ _ _ _

Pick a letter --> y

Guessed letters: E A I O U Y

O
\|/

_ O _ _ _ Y

Pick a letter --> 4
'4' is not a valid letter
Pick a letter --> xyz
'XYZ' has more than one letter.
Pick a letter --> l

Guessed letters: E A I O U Y L

O
\|/

_ O _ _ L Y

Pick a letter --> s

Guessed letters: E A I O U Y L S

O
\|/
|

_ O _ _ L Y

Pick a letter --> r

Guessed letters: E A I O U Y L S R

O
\|/
|
/

_ O _ _ L Y

Pick a letter --> k

Guessed letters: E A I O U Y L S R K

O
\|/
|
/ \

_ O _ _ L Y

So sorry. You struck out.
The mystery word was 'COMPLY.'






#! /usr/bin/env python
print 'Welcome to hangman. You get seven chances to guess the mystery word.'

a='happy'
m=list(a)

# draw the dash line
i=0
while i print '_',
i+=1

# draw a picture
def draw(n) :
if n == 1 :
print ' 0'
elif n == 2 :
print ' 0'
print ' |'
elif n == 3 :
print ' 0'
print '\|'
elif n == 4 :
print ' 0'
print '\|/'
elif n == 5 :
print ' 0'
print '\|/'
print '/'
elif n == 6 :
print ' 0'
print '\|/'
print '/ \.'


# This is main function
array=[]
array_history=[]
i=0
j=0

while 1 :
pick_up_letter = raw_input('\n\nPick a letter -->')

if pick_up_letter in '1234567890' :
print pick_up_letter + ' is not a valid letter'
elif len(pick_up_letter) > 2 :
flag=1 # when flag = 1 will not draw the picture.
print pick_up_letter + ' has more than one letter.'
else :
# array_history.append(pick_up_letter)
# print 'Guessed letters:'+''.join(array_history).upper()
break

array_history.append(pick_up_letter)

k=0
flag=0
while i if m[i] == pick_up_letter:
flag=1
# print pick_up_letter,
array.insert(i,pick_up_letter)
k+=1 # calculate about how many letter do you got. When you guess right.
i+=1
else :
#print '_',
array.insert(i, '_')
i+=1

if flag != 1 :
j+=1
draw(j)
print 'Guessed letters:'+''.join(array_history).upper()
print ' '.join(array)



while 1 :
i=0

pick_up_letter = raw_input('\n\nPick a letter -->')

if pick_up_letter in '1234567890' :
flag=1 # when flag = 1 will not draw the picture.
print pick_up_letter + ' is not a valid letter'
print 'flag 1'
elif len(pick_up_letter) > 2 :
flag=1 # when flag = 1 will not draw the picture.
print pick_up_letter + ' has more than one letter.'
else :
print 'flag 2'
array_history.append(pick_up_letter)
print 'Guessed letters:'+''.join(array_history).upper()

flag=0
while i if m[i] == pick_up_letter:
flag = 1
if array[i]!=pick_up_letter:
print 'flag 100'
array.pop(i)
array.insert(i,pick_up_letter)
k+=1
i+=1
else :
i+=1

else :
i+=1


if flag != 1 :
j+=1
draw(j)
print ' '.join(array)

# Have some problem there
if k == len(m) :
print 'Congratulation, you win this game'
break

if j > 6 :
print 'So sorry. You struck out.'
print 'The mystery word was '+a+'.\n\n'
break








# a='happy birthday'
# m=list(a)
# print m
# Result : ['h', 'a', 'p', 'p', 'y', ' ', 'b', 'i', 'r', 't', 'h', 'd', 'a', 'y']



Question :

  1. Just have one word "happy".I must create one dictionary file.



Reference :
http://openbookproject.net/pybiblio/practice/wilson/hangman.php

Related Posts:

0 意見:

張貼留言