Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hangman chapter.

Signed-off-by: Brian Hogan <brianhogan@napcs.com>
  • Loading branch information...
commit 3d02dd56bba453b5abddacafd7e07c85b4d1dcec 1 parent cba6c15
John Hobbs jmhobbs authored committed
149 chapter_hangman.xml
View
@@ -0,0 +1,149 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE chapter [
+<!ENTITY % allent SYSTEM "w3centities-f.ent">
+%allent;
+]>
+<chapter version="5.0" xmlns="http://docbook.org/ns/docbook"
+ xmlns:xlink="http://www.w3.org/1999/xlink"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns:mml="http://www.w3.org/1998/Math/MathML"
+ xmlns:html="http://www.w3.org/1999/xhtml"
+ xmlns:db="http://docbook.org/ns/docbook">
+
+ <title>Hangman</title>
+ <sect1>
+ <title>The Game Plan</title>
+ <para>Coming Soon</para>
+ </sect1>
+ <sect2>
+ <title>Game Rules</title>
+ <para>Coming Soon</para>
+ </sect2>
+ <sect3>
+ <title>Program Listing - words.txt</title>
+ <para>
+ Your program needs a list of word to use. Let's save some into words.txt.
+ </para>
+ <para>
+ You can use our words, or add your own if you like.
+ </para>
+ <programlisting language="text"><![CDATA[
+bird
+cow
+cat
+dog
+snake
+lizard
+ ]]></programlisting>
+ </sect3>
+ <sect4>
+ <title>Program Listing - Python</title>
+ <programlisting language="python"><![CDATA[
+# -*- coding: utf-8 -*-
+
+from random import choice
+
+def showHangman ( count ):
+
+ head = ' '
+ larm = ' '
+ chest = ' '
+ rarm = ' '
+ torso = ' '
+ lleg = ' '
+ rleg = ' '
+
+ if count > 0:
+ head = 'O'
+ if count > 1:
+ chest = '|'
+ if count > 2:
+ larm = '\\'
+ if count > 3:
+ rarm = '/'
+ if count > 4:
+ torso = '|'
+ if count > 5:
+ lleg = '/'
+ if count > 6:
+ rleg = '\\'
+
+ print " .-----."
+ print " | %s" % head
+ print " | %s%s%s" % ( larm, chest, rarm )
+ print " | %s" % torso
+ print " | %s %s" % ( lleg, rleg )
+ print " ____|____"
+
+def showState ( missed, guessed, stub ):
+ print "=" * 40
+ showHangman( len( missed ) )
+ print
+ print " Guessed :", " ".join( guessed )
+ print " Missed :", " ".join( missed )
+ print
+ print " Word:", "".join( stub )
+ print
+
+def main ():
+
+ f = open( 'words.txt', 'r' )
+ words = []
+ for word in f:
+ words.append( word.replace( "\n", '' ) )
+ f.close()
+
+ word = choice( words ).upper()
+ found = 0
+ stub = []
+ for i in range( 0, len( word ) ):
+ stub.append( "_" )
+
+ guessed = []
+ missed = []
+
+ print " Welcome To Hangman!"
+ print
+
+ while 7 > len( missed ) and found < len( word ):
+ showState( missed, guessed, stub )
+ while True:
+ guess = raw_input( " Guess A Letter: " ).upper()
+ print
+ if 1 != len( guess ):
+ print " Oops! You can only guess one letter at a time. Try again!"
+ print
+ elif guess in guessed:
+ print " Oops! You already guessed that letter! Try again!"
+ print
+ else:
+ if guess in word:
+ for i in range( 0, len( word ) ):
+ if guess == word[i]:
+ stub[i] = guess
+ found = found + 1
+ print " You guessed correct!"
+ else:
+ missed.append( guess )
+ print " You guessed wrong!"
+ guessed.append( guess )
+ break
+ print
+
+ showState( missed, guessed, stub )
+
+ if 7 <= len( missed ):
+ print " You lost!"
+ print
+ print " The word was", word
+ else:
+ print " You won!"
+ print " Great job!"
+ print
+
+if __name__ == "__main__":
+ main()
+ ]]></programlisting>
+ </sect4>
+</chapter>
104 code/chapter_hangman/hangman.py
View
@@ -0,0 +1,104 @@
+# -*- coding: utf-8 -*-
+
+from random import choice
+
+def showHangman ( count ):
+
+ head = ' '
+ larm = ' '
+ chest = ' '
+ rarm = ' '
+ torso = ' '
+ lleg = ' '
+ rleg = ' '
+
+ if count > 0:
+ head = 'O'
+ if count > 1:
+ chest = '|'
+ if count > 2:
+ larm = '\\'
+ if count > 3:
+ rarm = '/'
+ if count > 4:
+ torso = '|'
+ if count > 5:
+ lleg = '/'
+ if count > 6:
+ rleg = '\\'
+
+ print " .-----."
+ print " | %s" % head
+ print " | %s%s%s" % ( larm, chest, rarm )
+ print " | %s" % torso
+ print " | %s %s" % ( lleg, rleg )
+ print " ____|____"
+
+def showState ( missed, guessed, stub ):
+ print "=" * 40
+ showHangman( len( missed ) )
+ print
+ print " Guessed :", " ".join( guessed )
+ print " Missed :", " ".join( missed )
+ print
+ print " Word:", "".join( stub )
+ print
+
+def main ():
+
+ f = open( 'words.txt', 'r' )
+ words = []
+ for word in f:
+ words.append( word.replace( "\n", '' ) )
+ f.close()
+
+ word = choice( words ).upper()
+ found = 0
+ stub = []
+ for i in range( 0, len( word ) ):
+ stub.append( "_" )
+
+ guessed = []
+ missed = []
+
+ print " Welcome To Hangman!"
+ print
+
+ while 7 > len( missed ) and found < len( word ):
+ showState( missed, guessed, stub )
+ while True:
+ guess = raw_input( " Guess A Letter: " ).upper()
+ print
+ if 1 != len( guess ):
+ print " Oops! You can only guess one letter at a time. Try again!"
+ print
+ elif guess in guessed:
+ print " Oops! You already guessed that letter! Try again!"
+ print
+ else:
+ if guess in word:
+ for i in range( 0, len( word ) ):
+ if guess == word[i]:
+ stub[i] = guess
+ found = found + 1
+ print " You guessed correct!"
+ else:
+ missed.append( guess )
+ print " You guessed wrong!"
+ guessed.append( guess )
+ break
+ print
+
+ showState( missed, guessed, stub )
+
+ if 7 <= len( missed ):
+ print " You lost!"
+ print
+ print " The word was", word
+ else:
+ print " You won!"
+ print " Great job!"
+ print
+
+if __name__ == "__main__":
+ main()
6 code/chapter_hangman/words.txt
View
@@ -0,0 +1,6 @@
+bird
+cow
+cat
+dog
+snake
+lizard
Please sign in to comment.
Something went wrong with that request. Please try again.