Hangman

On a recent train ride I made a little pyhon program to play Hangman.
My version has 7 stages (you could implement more by changing the ‘STAGES’ variable) and is at best playable if you have someone else who gives you a word (you can also type ‘r’ for a random word, but the list of random words is not very long).
Insert the wordGuessWin! (or lose...)


# Python 2.7.7 Code
# Jonathan Frech April the 7th, 2015

# import
import os, random

# gets terminal size
def getTermSize():
	rows, columns = os.popen('stty size', 'r').read().split()
	return [int(columns), int(rows)]

# init
WORDS = ["banana", "house", "you", "brick", "nice", "hammer", "clock"]
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
STAGEHEIGHT = 9
STAGES = [
"""






   #########
 #############
###############""",
"""

       #
       #
       #
       #
       #
   #########
 #############
###############""",
"""

       #  ##
       ###
       #
       #
       #
   #########
 #############
###############""",
"""
       #############
       #  ##
       ###
       #
       #
       #
   #########
 #############
###############""",
"""
       #############
       #  ##       #
       ###         O
       #
       #
       #
   #########
 #############
###############""",
"""
       #############
       #  ##       #
       ###         O
       #          -|-
       #
       #
   #########
 #############
###############""",
"""
       #############
       #  ##       #
       ###         O
       #          -|-
       #          / \\
       #
   #########
 #############
###############"""]

# run function
def run():
	# header
	
	print "\n" * getTermSize()[1]
	print "========= [a game]"
	print " HANGMAN  [by Jonathan Frech]"
	print "========= [April the 7th, 2015]"
	print ""
	
	# init
	word = ""
	guesses = 0
	guessed = []
	got = []
	info = ""
	running = True
	
	# word input
	while word == "":
		word = raw_input("word? ").lower()
		if word == "r":
			word = WORDS[random.randint(0, len(WORDS) - 1)]
	print "\n" * 100
	
	# main game loop
	while running:
		# variable creation
		won = True
		lost = False
		
		if len(guessed) >= len(STAGES) - 1:
			lost = True
		
		_word = "Word         : "
		for _ in word:
			if _ in got:
				_word += _
			else:
				_word += "_"
				won = False
		
		_letter = "Wrong letters:"
		if len(guessed) <= 0:
			_letter += " -"
		else:
			for _ in guessed:
				_letter += " " + _
		
		# won
		if won:
			print "\n" * getTermSize()[1]
			print STAGES[guesses]
			print ""
			print "=" * (len(word) + 13 + 2)
			print " '" + word.upper() + "' -- YOU WON "
			print "=" * (len(word) + 13 + 2)
			return True
		
		# lost
		if lost:
			print "\n" * getTermSize()[1]
			print STAGES[guesses]
			print ""
			print "=" * (len(word) + 14 + 2)
			print " '" + word.upper() + "' -- YOU LOST "
			print "=" * (len(word) + 14 + 2)
			return False
		
		# normal game
		else:
			# print out
			print "\n" * getTermSize()[1]
			print STAGES[guesses]
			print "[" + info + "]"
			print ""
			print _word
			print _letter
			
			# ask fr guess
			inp = raw_input("guess? ").lower()
			info = ""
			
			# check and handle guess
			if len(inp) != 1 or inp not in ALPHABET:
				info = "input must be a letter."
			elif inp in guessed:
				info = "already guessed '" + inp + "'."
			else:
				if inp in word:
					got.append(inp)
					info = "-"
				else:
					guessed.append(inp)
					guesses += 1
					info = "wrong guess."

# main loop (for replaying)
R = True
while R:
	# run the game
	run()
	
	# ask to replay
	R = False
	print ""
	i = raw_input("Replay [y/n]? ")
	if i == "y":
		R = True
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s