Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

★【Challenge!】【Easy】 Hangman Solver

Name: !WIZardrY9E!UhEWzHM7+1tYUG2 2012-05-14 13:16

⚫ The Game of Hangman

A player guesses letters that are part of a word!
If he has not guessed the complete word before making six (6) mistakes, he has lost!

      _______
     |/      |
     |      (_)
     |      \|/   It is a game over!
     |       |
     |      / \
     |
  ___|___


⚫ Your Challenge

In a language of your choice, write a program that takes as its input (in some way) the target word (with blanks) and the letters that have been guessed already. It should then output the next letter to be guessed.
For example:

$ echo -n "p...s\npds" | ./guesser
e

You may assume the word to be guessed is an English word and contains only lowercase ASCII letters.
Provide code and instructions if usage is not obvious. Consider using a tripcode to ease conversation about your entry.

Entries will be judged on GOODITUDE (defined as its win/loss ratio over twenty random games), on SPEED, and on CLEVERNESS.

Your deadline is 23:59:59 on Sunday, May 20th. Results will be posted the following day.

 |∧_∧
 |´・ω・) < Good luck!♫
 ||と ノ

Name: Anonymous 2012-05-14 15:48


from string import *
import random
from hangman_lib import *

secret_word=get_random_word()

congrats=['Magnificient','Stupendous','That was EXPERT Quality','Marveloso','Fantabulous']
snide=['Don\'t worry 2/10 people fail to beat hangman','It\'s a hard game. It\'s not as \
though you have a list of letters you could guess with.','If that was you ,who just got hung,\
you\'d be a hung man. As in asphixiated']

letters_guessed=[]

mistakes_made=0

MAX_GUESSES=8


def randomnum(x):
    return random.randint(1,len(x)-1)

valid='abcdefghijklmnopqrstuvwxyzcheat'


def word_guessed():
    my=[]
    for i in secret_word:
        if i in letters_guessed: my.append(True)
        else: my.append(False)
    return all(my)

   
def print_guessed():
    m=[]
    for i in secret_word:
        if i in letters_guessed:
            m.append(i)
        else: m.append('-')
    print join(m,' ')

##def main():
   
while True:
    print 'You have %d guesses left' % (MAX_GUESSES-mistakes_made)
    print_guessed()
    print letters_guessed
    guess_letter=lower(raw_input('Guess a letter\n'))
    if guess_letter=='cheat':
            print secret_word
    while guess_letter in letters_guessed or guess_letter not in valid:
        guess_letter=lower(raw_input('Letter already picked or invalid input.\
Pick something else.\n'))
       
    letters_guessed.append(guess_letter)
    if guess_letter not in secret_word:
        mistakes_made+=1
        print_hangman_image(mistakes_made)   
    if mistakes_made>=MAX_GUESSES:
        print 'Game over.\n'
        print 'The word was %s' %(secret_word)
        print '%s' %(snide[randomnum(snide)])
        break
    if  word_guessed():
        print '%s ! You did it!\n' %(congrats[randomnum(congrats)])
        print secret_word
        break

print '\nArt created by sk'


Hangman Lib:



def print_hangman_image(mistakes = 6):
  """Prints out a gallows image for hangman. The image printed depends on
the number of mistakes (0-6)."""
 
  if mistakes <= 0:
    print''' ____________________
| .__________________|
| | / /    
| |/ / 
| | /    
| |/  
| |    
| |  
| |   
| |    
| |   
| |  
| |  
| |   
| |     
| |     
| |      
| |     
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| |                   | |
: :                   : :
. .                   . .'''

  elif mistakes == 1:
    print''' ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \\
| |          ||  `/,|
| |          (\\\\`_.'
| |       
| |    
| |   
| |  
| |  
| |   
| |     
| |     
| |      
| |     
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| |                   | |
: :                   : :
. .                   . .'''
  elif mistakes == 2:
    print''' ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \\
| |          ||  `/,|
| |          (\\\\`_.'
| |          -`--'
| |          |. .| 
| |          |   |  
| |          | . |   
| |          |   |    
| |          || ||
| |     
| |     
| |      
| |     
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| |                   | |
: :                   : :
. .                   . .'''
  elif mistakes == 3:
    print''' ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \\
| |          ||  `/,|
| |          (\\\\`_.'
| |         .-`--'
| |        /Y . .|
| |       // |   | 
| |      //  | . |  
| |     ')   |   |    
| |          || ||
| |     
| |     
| |      
| |     
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| |                   | |
: :                   : :
. .                   . .'''
  elif mistakes == 4:
    print''' ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \\
| |          ||  `/,|
| |          (\\\\`_.'
| |         .-`--'.
| |        /Y . . Y\\
| |       // |   | \\\\
| |      //  | . |  \\\\
| |     ')   |   |   (`
| |          || ||
| |     
| |     
| |      
| |     
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| |                   | |
: :                   : :
. .                   . .'''
  elif mistakes == 5:
    print''' ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \\
| |          ||  `/,|
| |          (\\\\`_.'
| |         .-`--'.
| |        /Y . . Y\\
| |       // |   | \\\\
| |      //  | . |  \\\\
| |     ')   |   |   (`
| |          ||'||
| |          ||  
| |          ||  
| |          ||  
| |         / | 
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| |                   | |
: :                   : :
. .                   . .'''
  else: #mistakes >= 6
    print''' ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \\
| |          ||  `/,|
| |          (\\\\`_.'
| |         .-`--'.
| |        /Y . . Y\\
| |       // |   | \\\\
| |      //  | . |  \\\\
| |     ')   |   |   (`
| |          ||'||
| |          || ||
| |          || ||
| |          || ||
| |         / | | \\
""""""""""|_`-' `-' |"""|
|"|"""""""\ \       '"|"|
| |        \ \        | |
: :         \ \       : :
. .          `'       . .'''

import random
def get_random_word():
  """Returns a random word from the file word_list.txt in the same directory"""
  input_file = open('word_list.txt','r')
  word_list = [word.strip().lower() for word in input_file.readlines() if word.strip().isalpha() and len(word) > 4]
  input_file.close()
 
  return word_list[random.randint(0,len(word_list)-1)]


Words list:

Name: Anonymous 2012-05-14 15:48


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List