hangman.py:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from random import choice
from hangman_lib import hang,wordlist
def find_index(word,char):
"""find all the positions of char in word"""
index_list=[]
for i in range(len(word)):
if word[i]==char:
index_list.append(i)
return index_list
def replace_letter(blank,word,char):
"""insert char in the positions, where char apears in word"""
index_list=find_index(word,char)
for i in index_list:
blank[i]=char
return blank
def inp():
"""validate input, only 1 letter per turn.
Also, some recurtion-foo at the end :D"""
i=raw_input(u"вход=>".encode('utf-8'))
if len(i)==1:
return i
else:
print "one letter at a time"
return inp()
def win(blank, word):
"""check if we win"""
if blank == word:
return True
else:
return False
def main(blank,word,lives):
"""main function, some more recurtion-foo here :D"""
print blank
if lives >0:
char=inp()
if char in word:
blank=replace_letter(blank,word,char)
if win(blank,word):
print "you win"
print word
else:
main(blank,word,lives) #recurse with new blank
else:
print hang[lives-1] #hang is the dict with the graphics
print 'you have %s lives left' %(lives-1)
main(blank,word,lives-1) #recurse with one live less
else:
print hang[0]
print "you lose!"
print word
if __name__=='__main__':
print hang[9]
word=choice(wordlist) #choice chooses a random item from a list
main(['_']*len(word),list(word),9) #blank is initialy only
# ["-"]*len(word)
#word is list(word),
#because blank is list
#and word must be list
#to compare them
а ето и другия файл hangman_lib.py(излъгах че е само в 60 реда, но лесно мога да събера целия файл на 2 много дълги реда:
wordlist=["boy","man","kid","worm","word","disk",
"hello","phone","desck","camera","python",
"marker","notepad","hangman","piramid",
"bullshit","hardcore","octopuss"]
hang={
0:
"""
_____
| |
| o
| /|\
| |
| /\\
|__________""",
1:
"""
_____
| |
| o
| /|\
| |
| /
|__________""",
2:
"""
_____
| |
| o
| /|\
| |
|
|__________""",
3:
"""
_____
| |
| o
| |\
| |
|
|__________""",
4:
"""
_____
| |
| o
| |
| |
|
|__________""",
5:
"""
_____
| |
| o
|
|
|
|__________""",
6:
"""
_____
| |
|
|
|
|
|__________""",
7:
"""
_____
|
|
|
|
|
|__________""",
8:
"""
|
|
|
|
|
|__________""",
9:
"""
__________""",
}
Няма коментари:
Публикуване на коментар