Created
June 9, 2009 21:54
-
-
Save Wilfred/126826 to your computer and use it in GitHub Desktop.
Text mode hangman game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Hangman game | |
# Copyright (c) 2009, Wilfred Hughes | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * The name of the author may not be used to endorse or promote products | |
# derived from this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY WILFRED HUGHES ''AS IS'' AND ANY | |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL WILFRED HUGHES BE LIABLE FOR ANY | |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
import sys | |
import random | |
def print_hangman(x): | |
positions = [ | |
"/---|\n\ | |
| o\n\ | |
| /|\\\n\ | |
| / \\\n\ | |
|\n\ | |
\-----", | |
"/---|\n\ | |
| o\n\ | |
| /|\\\n\ | |
| /\n\ | |
|\n\ | |
\-----", | |
"/---|\n\ | |
| o\n\ | |
| /|\\\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"/---|\n\ | |
| o\n\ | |
| /|\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"/---|\n\ | |
| o\n\ | |
| |\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"/---|\n\ | |
| o\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"/---|\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"/---\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
|\n\ | |
\-----", | |
"\n\ | |
\n\ | |
\n\ | |
\n\ | |
\n\ | |
\-----", | |
"\n\ | |
\n\ | |
\n\ | |
\n\ | |
\n\ | |
"] | |
print(positions[x]) | |
def print_word(guessed): | |
for g in guessed: | |
letter = g[0] | |
found = g[1] | |
if(found): | |
sys.stdout.write(letter) | |
else: | |
sys.stdout.write("_") | |
sys.stdout.write("\n\n") | |
def get_random_word(): | |
# get random word from dict (grep -v "'s" <british-english | grep | |
# '[a-z]\{5\}' > ~/scratch/hangman_dict) | |
dict = open("/home/wilfred/scratch/hangman_dict",'rt') | |
words = dict.readlines() | |
word = words[random.randint(0,len(words))].rstrip().lower() | |
dict.close() | |
return word | |
def play_hangman(): | |
word = get_random_word() | |
guessed = [[letter,False] for letter in word] | |
lives = 10 | |
while(True): | |
# check alive | |
print_hangman(lives) | |
if(lives==0): | |
print("Game Over!") | |
print("The word was: " + word) | |
sys.exit() | |
# print progress so far | |
print_word(guessed) | |
# take first non-whitespace character inputted as guess | |
guess = input().strip() | |
if (len(guess)==0): # nothing typed | |
continue # play this round again | |
guess = guess[0] | |
# see if guess was in the word | |
not_in_word = True | |
for i in range(len(guessed)): | |
letter = guessed[i][0] | |
found = guessed[i][1] | |
if(letter==guess): | |
guessed[i][1] = True | |
not_in_word = False | |
if(not_in_word): | |
lives -= 1 | |
# check to see if word is done | |
all_done = True | |
for i in range(len(guessed)): | |
if(not guessed[i][1]): | |
all_done = False | |
if(all_done): | |
print(word + "! You win!") | |
sys.exit(0) | |
if(__name__=='__main__'): | |
play_hangman() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment