write a Hangman game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stcooperr
    New Member
    • Jan 2014
    • 2

    #16
    This is my beginner version using lists. Made me feel like a programming god. Done in version 2.7.5

    Would welcome any comments, I think my solution is unusual at least.

    Code:
    #Hangman version 2 R J While-Cooper 19-01-14
    #NEXT UPDATE WILL ALLOW PHRASES THAT INCLUDE A DELIMITER BETWEEN WORDS
    
    #THE VARIOUS STAGES OF HANGMAN
    def scaffold5():
        print""
        print "   ------|"
        print "   |    \|"
        print "         |"
        print "         |"
        print "        /|"
        print "   ____/_|_"
    
    def scaffold4():
        print""
        print "   ------|"
        print "   |    \|"
        print "   o     |"
        print "         |"
        print "        /|"
        print "   ____/_|_"
    
    def scaffold3():
        print""
        print "   ------|"
        print "   |    \|"
        print "   o     |"
        print "  /      |"
        print "        /|"
        print "   ____/_|_"
    
    def scaffold2():
        print""
        print "   ------|"
        print "   |    \|"
        print "   o     |"
        print "  /|     |"
        print "        /|"
        print "   ____/_|_"
    
    def scaffold1():
        print""
        print "   ------|"
        print "   |    \|"
        print "   o     |"
        print "  /|\    |"
        print "        /|"
        print "   ____/_|_"
    
    def scaffold0():
        print""
        print "   ------|"
        print "   |    \|"
        print "   o     |"
        print "  /|\    |"
        print "  / \   /|"
        print "   ____/_|_"
        print "YOU'VE BEEN HANGED!"
    
    #DRAWS SCAFFOLD BASED ON NUMBER OF WRONG_GUESSE
    def build_scaffold():
        if wrong_guess == 0:
            scaffold5()
        elif wrong_guess ==1:
            scaffold4()
        elif wrong_guess ==2:
            scaffold3()
        elif wrong_guess ==3:
            scaffold2()
        elif wrong_guess ==4:
            scaffold1()
    
    
    #IMPORTS TIME FUNCTION FOR DELAYS
    import time
    
    #WORD CHOICE LIST
    medium_word = ['envelope','antelope','elephant','organism','encyclopedia']
    alphabet_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','o','p','q','r','s','t','u','v','w','x','y','z']
    
    
    #ADJUST DIFFICULTY BY CHANGING MAX_GUESSES TO A LOWER NUMBER
    check_letters = 0
    max_guesses = 5
    wrong_guess = 0
    cont = "y"
    
    #PRINTS ENTIRE ALPHABET NOT REALLY USED NOW
    def print_alpha_list():
        print "The letters you can choose are : ",
        for letter in alphabet_list:
            print letter,
        print ""
    
    #PRINTS THE LETTERS THAT HAVE BEEN CHOSEN
    def print_choices():
        print "The letters you have chosen are : ",
        for letter in choices:
            print letter,
        print ""
    
    #FUNCTIONS CLEAR THE SCREEN AND ADD A SHORT DELAY BETWEEN ROUNDS
    #CLEARS SCREEN
    def cls():
        print ('\n' *50)
    
    #SETS A DELAY
    def wait_time():
        print "PLEASE WAIT ",
        for x in range(4):
            print ". ",
            time.sleep(0.5)
        
    #CALLS CLEAR AND DELAY FUNCTIONS
    def clear_wait():
        wait_time()
        cls()
    
    #ENTERS THE GAME/REPEAT LOOP
    while cont == "y":
    
        #CLEARS SCREEN AND REMOVES CHOICES FROM LAST GAME PRINTS ALPHABET FOT THE IDIOTS
        clear_wait()
        choices = []
        print_alpha_list()
    
        #RANDOMLY CHOOSES A WORD FROM THE WORD LIST, SPLITS IT INTO A LIST
        #AND GIVE THE NUMBER OF LETTERS IN THE WORD
        from random import choice
        wordchosen = choice(medium_word)
        word = list(wordchosen)
        len_word = len(word)
    
        #DOES THE FIRST MASK
        build_scaffold()
        print "Guess the word!"
        print ""
        print "_ " * len_word
    
        #YOU GET TO PLAY UNTIL YOU RUN OUT OF GUESSES
        while wrong_guess < max_guesses:
            
            #RESETS CHECKING VARIABLES IN GAME LOOP
            duplicate_letter = 0
            missing_letter_count = 0
            check_letters = 0
    
    
            #ASKS FOR USER INPUT & COVERTS TO LOWER CASE
            print ""
            guess = raw_input("Choose a letter .Enter your choice and press <ENTER> --->")
            guess = guess.lower()
            
            #SPOTS DUPLICATE LETTERS BY CHECKING IF THE GUESS IS IN THE CHOICES LIST
            #IF NOT IT ADDS TO THE CHOICES LIST
            if guess in choices:
                print "You already picked that letter!"
                duplicate_letter +=1
            else:
                choices.append(guess)
    
            clear_wait()
            
            #print word
    
            #THE ALL IMPORTANT MASK ALGORITHM
            for letter in word:
                if letter == letter in word:
                    if letter in choices:
                        print letter,
                    else:
                        missing_letter_count +=1
                        print "_ ",
    
            #CHECKES IF YOU HAVE WON BECAUSE THERE WILL BE NO DASHES WRITTEN
            if missing_letter_count == 0:
                break
            
            print "" 
            print_choices()                 
            print ""
    
            #CHECKS TO SEE IN THE ANSWER IS WRONG
            for letter in word:
                if guess != letter:
                    check_letters +=1
            if check_letters == len(word) or duplicate_letter != 0:
                wrong_guess +=1
    
            build_scaffold()
    
            #INCREMENTS THE WRONG GUESSES COUNT FOR WRONG OR DUPLICATE ANSWERS
            if wrong_guess != 0 or duplicate_letter != 0:
                print "You have had ", wrong_guess, "wrong guesses!"
                print "You have ", max_guesses - wrong_guess, "guesses left!"
    
        #PRINTS YOU WIN OR LOSE DEPENDING ON CONDITION, GIVES PLAY AGAIN CHOICE
        #FINAL SCAFFOLD DRAWN IF WRONNG_GUESS ==MAX_GUESSES
        if wrong_guess == max_guesses:
            print "You lose!"
            print "The word was ",
            print word
            print ""
            scaffold0()
            print ""
            cont = raw_input ("  Play again? Y/N ? >")
            cont = cont.lower()
            wrong_guess = 0
        else:
            print ""
            cont = raw_input ("You Win!  Play again? Y/N ? >")
            cont = cont.lower()
            
    
    print "Thanks for playing HANGMAN!"
    Last edited by stcooperr; Jan 19 '14, 11:02 AM. Reason: it mucked up on the paste

    Comment

    • stcooperr
      New Member
      • Jan 2014
      • 2

      #17
      Here's a real quick n dirty solution using a different technique - involves breaking the strings apart and using the the position of characters to draw a map. No hangman drawn on this one but it's dead simple.
      Code:
      #alternative hangman (beta version without refinements)
      #rwhile-cooper 08-02-14
      word = "testword"
      guesses = []
      chances = 5
      
      pos = 0
      length = len(word)
      word_mask = "*" * length
      #need this to compare to the original word
      wordoriginal = word
      print word_mask
       
      #if the workmask matchs the original word you win   
      while wordoriginal != word_mask:
          guess = raw_input("Enter a letter to guess.")
          if guess in guesses:
              print "You already chose that letter."
          else:
              guesses.append(guess)
      
          if word.find(guess) != -1:
              print "You guessed correctly!"
              while word.find(guess) !=-1:
                      pos =  word.find(guess)
                      #have to replace the letters with dashes or the positions don't work in future goes
                      word = word[:pos] +"_" +word[pos+1:]
                      #adds the correct letters to the wordmask
                      word_mask = word_mask[:pos] + guess + word_mask[(pos+1):]
              
          else:
              #updates wrong chances and checks for victory
              chances -=1
              print "You guessed wrong. You have ",chances  ,"chance left."
                        
          print word_mask
          if chances == 0:
              break
      
      if chances == 0:
          print "You lost"
      else:
          print "You won"

      Comment

      Working...