Hangman Phase 2: Let’s Play This Thing!

hangman

Now that you have completed and tested your drawing functions, which put Charlie’s body parts on the screen, we can move toward putting together a functioning game program.

First, let’s see what the game will look like in action.  This video shows two rounds of the Hangman Game program.

Now that you’ve seen it in action, let’s take a look at the skeleton code for the program.  Keep in mind that the code below will not run properly as written.  You still need to add funcitonality to make it work.  Specifically, you will need to add your drawing functions, the stringReplace() function that we wrote together in our notes, a welcomeMessage function to display the game title and rules, a badGuess function that decides which body part to draw after a missed letter, and a loadWordListFromFile function to create our list of possible words from a text file that I have provided on the N: drive.

Copy the code below into a file named your name-hangman.py and then add the code necessary to make the game work.  The code is also on the N: Drive in a file names yourname-hangman-incomplete.py. I’ve indicated right in the program comments the places where you will need to add your own code.  Look for the RED hash remarks.

When you get it working, call me over so I can see it in action.

# yourname-hangman.py
# This program implements a Hangman Game.

import turtle, random

# ********************************************************************************
# Drawing Function Definitions created by you
# ********************************************************************************

# Put all of your drawing functions here for Charlie.

def drawPlatform(t):

def drawHead(t):

def drawBody(t):

def drawLeftArm(t):

def drawRightArm(t):

def drawLeftLeg(t):

def drawRightLeg(t):

def drawLeftEye(t):

def drawRightEye(t):

def drawFrown(t):

# ********************************************************************************
#  Other Game Play Function Definitions that you need to complete
# ********************************************************************************

def badGuess(t, misscounter):

	# This function should use an if-elif-else block to either draw one of 
        # Charlie's body parts or draw his frown and print "You Lose".

	# Remember that for 1 miss, you draw a head, for two misses, you draw 
        # his torso, for three, misses, you draw his right arm, etc...
	# On the 9th miss you draw the frown and that's all she wrote.


def stringReplace(mystring,position,achar):
	# Put the stringReplace function from the notes here!
	# I gave it to you, just copy it.

def loadWordListFromFile(fname):
	wordsfile = # finish this line by putting the command to open file fname
	wordlist = []

	for word in wordsfile:
		wordlist.append(word.strip())

	return wordlist

def welcomeMessage():
	# Put in some print statements here to present a nice welcome to the player.
	# Use a bunch of stars (*) and a nice title.
	# Explain the rules and that you lose on the 8th miss.

# ********************************************************************************
#  Functions you can steal from Mr. Murphy
# ********************************************************************************

def replaceAllInString(mystring, currentString, achar):
	returnString = currentString
	pos = 0
	for c in mystring:
		if c.lower() == achar:
			returnString = stringReplace(returnString,pos,c)
		pos = pos + 1
	return returnString

def showCurrentString(currentString):
	displayString = ""
	for c in currentString:	
		displayString = displayString + c + " "
	return displayString

# ********************************************************************************
#  Main Program Code you can steal from Mr. Murphy.
#  If all of your functions are working, the game will play.
# ********************************************************************************
alex = turtle.Turtle()
wn = turtle.Screen()
alex.ht()

welcomeMessage()

drawPlatform(alex)

# Get words from data file and randomly choose our word
# Make sure the file hangman-word-list.txt is in the same folder as your program.
# There is a copy of the file in the HS Apps N: drive under amurphy.

secretwordlist = loadWordListFromFile("hangman-word-list.txt")
wordnumber = random.randint(0,len(secretwordlist)-1)
magicword = secretwordlist[wordnumber]

# Reset Guess Counter and Miss Counter and currentString
guesscounter = 0
misscounter = 0

# currentString shows the blanks and correct letters to the player.
currentString = "_" * len(magicword)

# Set valid characters to the alphabet
valid_characters = "abcdefghijklmnopqrstuvwxyz"

# Reset user guesses string, which shows the letter that have already been guessed.
user_guesses = ""

print("\n" + showCurrentString(currentString) + "\n")

# Main Game Loop
while currentString.lower() != magicword.lower() and misscounter < 9:

	# Get the guess
	guess = raw_input('Enter the letter of your next guess or "quit" to end: ')
	guess = guess.lower()

	# Decide if the user entered a valid guess, or quit.

	if guess == "quit":
		print("Quitting game...")
		break
	elif len(guess) != 1:
		print "Please enter a single letter\n"

	elif valid_characters.find(guess) < 0:
 		print "Please enter a valid letter (a-z)\n"

 	elif user_guesses.find(guess) >= 0:
		print ("You already guessed " + guess + 
                       ". Please try something else.\n")

	else:        
		#add this letter to already_guessed
		user_guesses = user_guesses + guess

		#check hit or miss here...
		if magicword.lower().find(guess) >= 0:
			print "Yes"
			currentString = replaceAllInString(magicword, 
                                                           currentString, guess)
			print ("\n" + showCurrentString(currentString) + "\n")
			print ('Your used letters are: ' + user_guesses +"\n")
		else:
			print "Sorry, it's not there."
			misscounter = misscounter + 1
			print ("\n" + showCurrentString(currentString) + "\n")
			print ('Your used letters are: ' + user_guesses +"\n")
			badGuess(alex, misscounter)

if misscounter < 9:
	print("You win!!!")

print("The word was " + magicword + ".")
print("Click the platform to exit!")

wn.exitonclick()