105 - Hangman

105 - Hangman#

Implement a Python version of the classic Hangman word guessing game. In addition to printing the status of the word being guessed (e.g. with “*” denoting letters not yet guessed) print one of the following ASCII art representations of the remaining guesses:

Number of errors:
    0          1          2          3          4          5          6
  +---+      +---+      +---+      +---+      +---+      +---+      +---+
  |   |      |   |      |   |      |   |      |   |      |   |      |   |
      |      0   |      0   |      0   |      0   |      0   |      0   |
      |          |      |   |     /|   |     /|\  |     /|\  |     /|\  |
      |          |          |          |          |     /    |     / \  |
      |          |          |          |          |          |          |
=========  =========  =========  =========  =========  =========  =========

Here 0 errors represents the initial state at the beginning of the game and 6 errors also represents “Game Over”.

Tips:

  • Do this as a standalone python script, not as a Jupyter notebook.

  • Create an initial version with a hardcoded list of 10 words, but then once it is working read in a longer list of words from a file that you have prepared.

  • Don’t forget to reveal the word at the end if the player gets to 6 errors and “Game Over!”

To keep things simple, use the following brute force method (and code to create a dictionary) for dealing with the different multi-line ASCII Art representations. (The “r” before the triple quote just tells the intepreter to treat it as a “raw” string and not interpret the “” as “escaping” the following character.) Add your own ASCII art text next to the various stages if you like.

Can you think of more elegant or alternate ways to manage and display the ASCII Art?

aa = {}
aa[0]=r'''
  +---+
  |   |
      |
      |
      |
      |
=========
'''
aa[1]=r'''
  +---+
  |   |
  0   |
      |
      |
      |
=========
'''
aa[2]=r'''
  +---+
  |   |
  0   |
  |   |
      |
      |
=========
'''
aa[3]=r'''
  +---+
  |   |
  0   |
 /|   |
      |
      |
=========
'''
aa[4]=r'''
  +---+
  |   |
  0   |
 /|\  |
      |
      |
=========
'''
aa[5]=r'''
  +---+
  |   |
  0   |
 /|\  |
 /    |
      |
=========
'''
aa[6]=r'''
  +---+       ____                         ___
  |   |      / ___| __ _ _ __ ___   ___   / _ \__   _____ _ __
  0   |     | |  _ / _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__|
 /|\  |     | |_| | (_| | | | | | |  __/ | |_| |\ V /  __/ |
 / \  |      \____|\__,_|_| |_| |_|\___|  \___/  \_/ \___|_|
      |
=========
'''
print(aa[3])
  +---+
  |   |
  0   |
 /|   |
      |
      |
=========
print(aa[6])
  +---+       ____                         ___
  |   |      / ___| __ _ _ __ ___   ___   / _ \__   _____ _ __
  0   |     | |  _ / _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__|
 /|\  |     | |_| | (_| | | | | | |  __/ | |_| |\ V /  __/ |
 / \  |      \____|\__,_|_| |_| |_|\___|  \___/  \_/ \___|_|
      |
=========