Skip to content

Instantly share code, notes, and snippets.

@sr-murthy
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sr-murthy/68052e5bcb50519a1382 to your computer and use it in GitHub Desktop.
Save sr-murthy/68052e5bcb50519a1382 to your computer and use it in GitHub Desktop.
A Game of Hangman
# A game of Hangman
# 29/01/2015
'''
The MIT License (MIT)
Copyright (c) 2015 Sandeep Murthy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import random
import string
import time
'''
The game chooses words randomly from a list of 109582 English words from a
local file which is a copy of
http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt.
You can use any file instead of this and place it into any directory of your
choosing, but make sure to modify the path in the variable RANDOM_WORDS_FILE
below to the full actual path of where you've downloaded it.
'''
RANDOM_WORDS_FILE = '<file path>'
# The English alphabet as a lowercase string
ALPHABET = string.ascii_lowercase
# Open RANDOM_WORDS_FILE and load into a word list.
def load_words():
#print '\n\tLoading words from file ...'
#time.sleep( 2 )
file = open( RANDOM_WORDS_FILE, 'r')
word_list = []
for word_line in file:
word_list.append( string.strip( word_line ) )
#print '\t ', len( word_list ), 'English words loaded (in lowercase).'
file.close()
return word_list
# Choose a word randomly from the word list.
def choose_word( word_list ):
return random.choice( word_list )
# Check if the guessed letters make up the secret word.
def is_word_guessed( secret_word, letters_guessed ):
return ( set( secret_word ).issubset( set( ''.join( letters_guessed ) ) ) )
# Show the correctly guessed letters (if any) among the unguessed letters of the secret word.
def get_guessed_word( secret_word, letters_guessed ):
return_string = ''
for s in secret_word:
if s in letters_guessed:
for l in letters_guessed:
if s == l:
return_string += l + ' '
else:
return_string += '_ '
return return_string.strip()
# Get the available (remaining) letters that the player can guess from.
def get_available_letters( letters_guessed ):
return_string = ''
for a in ALPHABET:
if a not in letters_guessed:
return_string += a
return return_string
# Draw the hangman on the screen.
def hangedman_drawing( guesses ):
if guesses == 7:
print '\t===='
elif guesses == 6:
print '\t===='
print '\t|/ |'
elif guesses == 5:
print '\t===='
print '\t|/ |'
print '\t| (O)'
elif guesses == 4:
print '\t===='
print '\t|/ |'
print '\t| (O)'
print '\t| \|/'
elif guesses == 3:
print '\t===='
print '\t|/ |'
print '\t| (O)'
print '\t| \|/'
print '\t| |'
elif guesses == 2:
print '\t===='
print '\t|/ |'
print '\t| (O)'
print '\t| \|/'
print '\t| |'
print '\t| / \\'
elif guesses == 1:
print '\t===='
print '\t|/ |'
print '\t| (O)'
print '\t| \|/'
print '\t| |'
print '\t| / \\'
print '\t| '
elif guesses == 0:
print '\t===='
print '\t|/ |'
print '\t| (O)'
print '\t| \|/'
print '\t| |'
print '\t| / \\'
print '\t|__'
# The main game function
def hangman( secret_word ):
letters_available = ALPHABET
letters_guessed = []
misses = ''
guesses = 8
guess = ''
print '\n\tWelcome to the (low-budget, text-based) Hangman game!'
time.sleep( 3 )
print '\n\tI am thinking of a secret (lowercase) English word that is ' + str( len( secret_word ) ) + ' letters long.'
print '\tCan you guess what it is by guessing letters?',
print '\tYou can make up to 8 guesses before the game is over ...'
print '\tor you guess all the correct letters of the secret word. To exit at any time press CTRL+C.'
time.sleep( 7 )
while guesses > 0:
print '\n\t****************'
print '\n\tWord: ' + get_guessed_word( secret_word, letters_guessed )
guess = string.lower( raw_input( '\tYour guess: ' ) )
if guess not in letters_guessed:
letters_guessed.append( guess )
if guess not in secret_word:
guesses -= 1
print '\tSorry, that letter is not in my word.'
time.sleep( 3 )
misses += guess + ', '
print '\tYour misses: ' + misses,
print ' (' + str( guesses ) + ' guesses left, ',
print 'available letters: ' + get_available_letters( letters_guessed ) + ')'
time.sleep( 3 )
hangedman_drawing( guesses )
time.sleep( 3 )
continue
print '\tGood guess! ',
print get_guessed_word( secret_word, letters_guessed ),
print ' (' + str( guesses ) + ' guesses left, ',
print 'available letters: ' + get_available_letters( letters_guessed ) + ')'
time.sleep( 3 )
if is_word_guessed( secret_word, letters_guessed ):
print '\n\t****************'
print '\n\tCongratulations! You\'ve won (and beaten the hangman)!'
return None
else:
hangedman_drawing( guesses )
time.sleep( 3 )
continue
print '\n\tSorry, you\'ve already guessed that letter.'
hangedman_drawing( guesses )
continue
print '\n\t****************'
print '\n\tSorry, you ran out of guesses (and succumbed to the hangman)!. The word was \'' + secret_word + '\'.\n'
# Start the game - load the word list from the file, choose a secret word and call the main function hangman
word_list = load_words()
secret_word = choose_word( word_list ).lower()
hangman( secret_word )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment