4 Replies - 8723 Views - Last Post: 27 February 2012 - 06:56 PM Rate Topic: -----

#1 Computer-Wiz   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

hangman game

Posted 23 February 2012 - 03:41 PM

so i have to make a text base hangman game. I need to read words from a file and of course make the game played like hangman. This is what i have so far, the only part i am stuck on is how do i make the word appear after every guess the user makes. I need help with the guess method

#random functions imported
import random

import string

def welcome(begin):
    
    title = "Welcome To This Hangman Game"
    
    if begin == "yes":
        print
        print "*"+"-"*len(title)+"*"
        print "|"+title+"|"
        print "*"+"-"*len(title)+"*"
        return True
    else:
        print "\nBye For Now!"
        return False




def getWords(filename):


    newList = []

    fileIn = open("listOfFruits.txt", "r")


    for everyLine in fileIn:
        temp = everyLine.split()
        newList.append(temp)
        everyLine.strip("\n")
        
    fileIn.close()
    
    return newList


def chooseWord(newList):
    mysteryWord = random.choice(newList)
    newList.remove(mysteryWord)
    for word in mysteryWord:
        for letter in word:
            print "_",


    print "\n"
    print word
    
    return mysteryWord
    return word
  

def guessWord(word):
    
    guessMade = ""
    while 
    
#MAIN#
######

#1 - welcome
begin = raw_input ("Would you like to start this Game - [yes/no]: ")
returnValue = welcome(begin)
while returnValue != True:
    exit
    
#2 - getWords
newList = getWords("listOfFruits.txt")

#3 - chooseWord
word = chooseWord(newList)


##
###4 - guessWord
guessWord(word)

This post has been edited by Simown: 23 February 2012 - 04:14 PM
Reason for edit:: please use code tags!


Is This A Good Question/Topic? 0
  • +

Replies To: hangman game

#2 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: hangman game

Posted 23 February 2012 - 08:20 PM

in pseudo code you want to do something like:
nGuessesAllowed = 5 #you can set this how you want
nGuesses = 0
solved = False

while( nGuesses < nGuessesAllowed  and not solved):
  get user letter
  check if letter is in word and display correct/error message
    *if the letter was in the word and word was solved: solved = True
  display the word with correct letters displayed
  nGuesses += 1

Make a decision based on whether the word was solved in time or not.


The hardest part will probably be determining if the letter is in the word. I would suggest making a blank copy of the word to fill in the letters as they are found.
Ex; wordBlank = ' '*len( word ) then when you find the letter in the word, replace the blank in wordBlank with the letter. If the letter was found at position n: wordBlank[n] = letter guessed

see where that gets you and post back if you need more help :)

This post has been edited by William_Wilson: 23 February 2012 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 Computer-Wiz   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: hangman game

Posted 24 February 2012 - 03:32 PM

Thank you very much, i really appricated your help. so i understood what you told me and it makes sense to me. The only question i have is do you know how i can split the gamePlay(newList) method into 2 because my teachers wants it to split be split up. I tried splitting ir by making one method where it would get the word randomly from the list and than the other medthod would do the gameplay but it does not seem to work.

i appricate your help

thanks


'''
Created on 2012-02-19

'''

#random functions imported
import random

import string

def welcome(begin):
    
    title = "Welcome To This Hangman Game"
    
    while begin == "yes":
        print
        print "*"+"-"*len(title)+"*"
        print "|"+title+"|"
        print "*"+"-"*len(title)+"*"
        return True
    else:
        print "\nBye For Now!"
        return False



def getWords(filename):


    newList = []

    fileIn = open("fiveandsixletterwordlist.txt", "r")


    for everyLine in fileIn:
        temp = everyLine.split()
        newList.append(temp)
        everyLine.strip("\n")
        
    fileIn.close()
    
    return newList


def gamePlay(newList):
    mysteryWord = random.choice(newList)
    word = ''
    for word in mysteryWord:
        word = word
    print word
    guesses = ''
    turns = 7
    
    print "\nYou have only", turns, "incorrect chances"
    
    while turns > 0:
      mistakes = 0
      
      for letter in word:
          
          if letter in guesses:
              
              print letter,
              
                
          else:
              
              print '_',
              mistakes += 1
              
        
      print
    
      if mistakes == 0:
        print '\nCongratulation, You win! this round:)'
        print "\nThe word was", word
        break
    
      guess = raw_input('\nPlease make a Guess: ')
      guesses += guess
    
      if guess not in word:
        turns -= 1
        print '\nSorry, that is correct. Please try again!'
        print '\nYou now have', turns,"more turns!"
        if turns == 0:
          print '\nThe correct word/answer is', word


def playAgain (cmd):
    pass
    
######
#MAIN#
######

#1 - welcome
begin = raw_input ("Would you like to start this Game - [yes/no]: ")
returnv=welcome(begin)
while begin != "yes":
    exit
#2 - getWords
newList = getWords("fiveandsixletterwordlist.txt")

#3 - gamePlay
word = gamePlay(newList)


#4 - mistakes

This post has been edited by Simown: 24 February 2012 - 03:58 PM
Reason for edit:: Still use code tags!

Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: hangman game

Posted 25 February 2012 - 02:48 PM

I am assuming the code works as it is displayed, but you only need guidance on dividing word choice and game play.

I would remove:
mysteryWord = random.choice(newList) from your gameplay function as this is the code determining the word to use. Then create a new function, say:
def getRandWord( newList ):
  return random.choice(newList)


now in the main portion you can replace word = gamePlay(newList) with:
mysterWord = getRandWord( newList )
gamePlay( mysteryWord )



your gameplay function does not return a value, the word = should never hold a valid value.
Was This Post Helpful? 0
  • +
  • -

#5 Computer-Wiz   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: hangman game

Posted 27 February 2012 - 06:56 PM

hi guys, first of all i want to thank all of you for your help i really appreciated it. I am technically done my coding for this hangman game but there is one part i am getting an error on it. At first my whole game was in the gamePlay method in the while loop but my teacher told me to split it up because she wanted me to show that i knew how to use methods so she told me to make atleast 2 good useful chunks of methods for the game. so i splitted it up - the gameplay method takes the guess and displayes it while the mistake method counts the mistakes(by the way if you think i messed up on these methods please let me know). anyway i am getting an error:

line 77, in mistakes
if guess not in word:
TypeError: argument of type 'int' is not iterable

i was really hard for me to split up the method into 2 as i am a beginner to python and not too good at it, yet. i would really appreciate some help. thanks, you guys are great.

My coding is:

'''
Created on 2012-02-19


'''

#random functions imported
import random

#string functions imported
import string

#Asks user if they want to play - while yes the game continues else it exits.
def welcome(begin):
    
    title = "Welcome To This Hangman Game"
    
    while begin == "yes":
        print
        print "*"+"-"*len(title)+"*"
        print "|"+title+"|"
        print "*"+"-"*len(title)+"*"
        return True
    else:
        print "\nBye For Now!"
        
        return False
#reads words from file
def getWords(filename):
    newList = []
    fileIn = open("fiveandsixletterwordlist.txt", "r")
    for everyLine in fileIn:
        temp = everyLine.split()
        newList.append(temp)
        everyLine.strip("\n")   
    fileIn.close()
    return newList

#gets a random word from list and display "_" for letters in word
#asks user to guess the word
#count the turns taken
def gamePlay(newList):
    mysteryWord = random.choice(newList)
    for word in mysteryWord:
        break
    print word
    guessMade = ''
    maxIncorrectTurns = 7
    print "\nYou have only", maxIncorrectTurns, "incorrect chances"
    while maxIncorrectTurns > 0:
        mistakes = 0
        for letter in word:
            if letter in guessMade:
                print letter,    
            else:
                  print '_',
                  mistakes += 1
        return mistakes
    return turns
    return guessMade
    return guess

#for user guess mistakes it counts till 7 then end game
#if user guesses word before 7 incorrect guesses it prints word for win
def mistakes(mistakes):
    guessMade= ''
    print "\n"
    turns = 7
    while turns > 0:
        if mistakes == 0:
            print '\nCongratulation, You win! this round:)'
            print "\nThe word was", word
            break
        guess = raw_input('\nPlease make a Guess: ')
        guessMade += guess
        for letter in guess:#######ERROR#######
            if guess not in word:
                maxIncorrectTurns -= 1
                print '\nSorry, that is correct. Please try again!'
                print '\nYou now have', maxIncorrectTurns,"more turns left!"
            if maxIncorrectTurns == 0:
                print '\nThe correct word/answer is', "[",word,"]"

def playAgain (cmd):
    if cmd == "yes":
        gamePlay(newList)
        
    else:
        print "\nBye for now!"
        exit
    
######
#MAIN#
######
    
begin = raw_input ("\nWould you like to start this Game - [yes/no]: ")

returnv = welcome(begin)
while begin != "yes":
    exit
    

newList = getWords("fiveandsixletterwordlist.txt")



word = gamePlay(newList)

mistakes(mistakes)


cmd = raw_input ("\nDo you want to continue - [yes/no]: ")
playAgain(cmd)
[/inline]

hi guys, first of all i want to thank all of you for your help i really appreciated it. I am technically done my coding for this hangman game but there is one part i am getting an error on it. At first my whole game was in the gamePlay method in the while loop but my teacher told me to split it up because she wanted me to show that i knew how to use methods so she told me to make atleast 2 good useful chunks of methods for the game. so i splitted it up - the gameplay method takes the guess and displayes it while the mistake method counts the mistakes(by the way if you think i messed up on these methods please let me know). anyway i am getting an error:

line 77, in mistakes
    if guess not in word:
TypeError: argument of type 'int' is not iterable 

i was really hard for me to split up the method into 2 as i am a beginner to python and not too good at it, yet. i would really appreciate some help. thanks, you guys are great.

My coding is:


'''
Created on 2012-02-19

@author: Rafay Sheikh
'''

#random functions imported
import random

#string functions imported
import string

#Asks user if they want to play - while yes the game continues else it exits.
def welcome(begin):
    
    title = "Welcome To This Hangman Game"
    
    while begin == "yes":
        print
        print "*"+"-"*len(title)+"*"
        print "|"+title+"|"
        print "*"+"-"*len(title)+"*"
        return True
    else:
        print "\nBye For Now!"
        
        return False
#reads words from file
def getWords(filename):
    newList = []
    fileIn = open("fiveandsixletterwordlist.txt", "r")
    for everyLine in fileIn:
        temp = everyLine.split()
        newList.append(temp)
        everyLine.strip("\n")   
    fileIn.close()
    return newList

#gets a random word from list and display "_" for letters in word
#asks user to guess the word
#count the turns taken
def gamePlay(newList):
    mysteryWord = random.choice(newList)
    for word in mysteryWord:
        break
    print word
    guessMade = ''
    maxIncorrectTurns = 7
    print "\nYou have only", maxIncorrectTurns, "incorrect chances"
    while maxIncorrectTurns > 0:
        mistakes = 0
        for letter in word:
            if letter in guessMade:
                print letter,    
            else:
                  print '_',
                  mistakes += 1
        return mistakes
    return turns
    return guessMade
    return guess

#for user guess mistakes it counts till 7 then end game
#if user guesses word before 7 incorrect guesses it prints word for win
def mistakes(mistakes):
    guessMade= ''
    print "\n"
    turns = 7
    while turns > 0:
        if mistakes == 0:
            print '\nCongratulation, You win! this round:)'
            print "\nThe word was", word
            break
        guess = raw_input('\nPlease make a Guess: ')
        guessMade += guess
        for letter in guess:
            if guess not in word:
                maxIncorrectTurns -= 1
                print '\nSorry, that is correct. Please try again!'
                print '\nYou now have', maxIncorrectTurns,"more turns left!"
            if maxIncorrectTurns == 0:
                print '\nThe correct word/answer is', "[",word,"]"

def playAgain (cmd):
    if cmd == "yes":
        gamePlay(newList)
        
    else:
        print "\nBye for now!"
        exit
    
######
#MAIN#
######
    
begin = raw_input ("\nWould you like to start this Game - [yes/no]: ")

returnv = welcome(begin)
while begin != "yes":
    exit
    

newList = getWords("fiveandsixletterwordlist.txt")



word = gamePlay(newList)

mistakes(mistakes)


cmd = raw_input ("\nDo you want to continue - [yes/no]: ")
playAgain(cmd)

This post has been edited by Simown: 05 March 2012 - 06:47 PM
Reason for edit:: For the third time, use code tags!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1