hangman package

python_hangman

A well tested, cli, python version-agnostic, multi-platform hangman game. It’s built following a TDD workflow and a MVC design pattern. Each component services a sensibly distinct logical purpose. Python Hangman is a version agnostic, tox tested, travis-backed program! Documented and distributed.

Submodules

hangman.__main__

Entry point for hangman command.

hangman.controller

hangman.controller.game_loop(game=hangman(status='________', misses=[], remaining_turns=10), flash=<hangman.utils.FlashMessage object>)[source]

Run a single game.

Parameters:
hangman.controller.run(game=hangman(status='_____', misses=[], remaining_turns=10), flash=<hangman.utils.FlashMessage object>)[source]

Run game_loop and handle exiting.

Logic is separated from game_loop to cleanly avoid python recursion limits.

Parameters:

hangman.model

class hangman.model.Hangman(answer=None)[source]

Bases: object

The the logic for managing the status of the game and raising key game related events.

>>> from hangman.model import Hangman
>>> game = Hangman(answer='hangman')
>>> game.guess('a')
hangman(status='_A___A_', misses=[], remaining_turns=10)
>>> game.guess('n').guess('z').guess('e')
hangman(status='_AN__AN', misses=['E', 'Z'], remaining_turns=8)
>>> game.status
'_AN__AN'
>>> game.misses
['E', 'Z']
>>> game.remaining_turns
8
MAX_TURNS = 10
guess(letter)[source]

Add letter to hits or misses.

hits

List of hits.

is_valid_answer(word)[source]

Validate answer. Letters only. Max:16

is_valid_guess(letter)[source]

Validate guess. Letters only. Max:1

misses

List of misses.

remaining_turns

Calculate number of turns remaining.

status

Build a string representation of status.

hangman.utils

App utilities.

class hangman.utils.WordBank[source]

Bases: object

Default collection of words to choose from

WORDS = ['ATTEMPT', 'DOLL', 'ELLEN', 'FLOATING', 'PRIDE', 'HEADING', 'FILM', 'KIDS', 'MONKEY', 'LUNGS', 'HABIT', 'SPIN', 'DISCUSSION', 'OFFICIAL', 'PHILADELPHIA', 'FACING', 'MARTIN', 'NORWAY', 'POLICEMAN', 'TOBACCO', 'VESSELS', 'TALES', 'VAPOR', 'INDEPENDENT', 'COOKIES', 'WEALTH', 'PENNSYLVANIA', 'EXPLANATION', 'DAMAGE', 'OCCASIONALLY', 'EXIST', 'SIMPLEST', 'PLATES', 'CANAL', 'NEIGHBORHOOD', 'PALACE', 'ADVICE', 'LABEL', 'DANNY', 'CLAWS', 'RUSH', 'CHOSE', 'EGYPT', 'POETRY', 'BREEZE', 'WOLF', 'MANUFACTURING', 'OURSELVES', 'SCARED', 'ARRANGEMENT', 'POSSIBLY', 'PROMISED', 'BRICK', 'ACRES', 'TREATED', 'SELECTION', 'POSITIVE', 'CONSTANTLY', 'SATISFIED', 'ZOO', 'CUSTOMS', 'UNIVERSITY', 'FIREPLACE', 'SHALLOW', 'INSTANT', 'SALE', 'PRACTICAL', 'SILLY', 'SATELLITES', 'SHAKING', 'ROCKY', 'SLOPE', 'CASEY', 'REMARKABLE', 'RUBBED', 'HAPPILY', 'MISSION', 'CAST', 'SHAKE', 'REQUIRE', 'DONKEY', 'EXCHANGE', 'JANUARY', 'MOUNT', 'AUTUMN', 'SLIP', 'BORDER', 'LEE', 'MELTED', 'TRAP', 'SOLAR', 'RECALL', 'MYSTERIOUS', 'SWUNG', 'CONTRAST', 'TOY', 'GRABBED', 'AUGUST', 'RELATIONSHIP', 'HUNTER', 'DEPTH', 'FOLKS', 'DEEPLY', 'IMAGE', 'STIFF', 'RHYME', 'ILLINOIS', 'SPECIES', 'ADULT', 'FINEST', 'THUMB', 'SLIGHT', 'GRANDMOTHER', 'SHOUT', 'HARRY', 'MATHEMATICS', 'MILL', 'ESSENTIAL', 'TUNE', 'FORT', 'COACH', 'NUTS', 'GARAGE', 'CALM', 'MEMORY', 'SOAP']
classmethod get()[source]

Get a random word from word list.

classmethod set(*values)[source]

Set word list.

class hangman.utils.FlashMessage[source]

Bases: object

Basic “flash message” implementation.

game_lost = False
game_won = False
message = ''
exception hangman.utils.GameLost[source]

Bases: Exception

Raised when out of turns.

exception hangman.utils.GameWon[source]

Bases: Exception

Raised when answer has been guessed.

exception hangman.utils.GameOverNotificationComplete[source]

Bases: Exception

Raised when controller should break game loop.

hangman.view

View layer, printing and prompting.

hangman.view.build_partial_misses(game_misses)[source]

Generator, build game misses block.

hangman.view.build_partial_picture(remaining_turns)[source]

Generator, build the iconic hangman game status.

hangman.view.draw_board(game, message=<hangman.utils.FlashMessage object>)[source]

Present the game status with pictures.

  • Clears the screen.
  • Flashes any messages.
  • Zip the two halves of the picture together.
+---------------------------------------------+
|              message 45 x 1                 |
+---------------------------------------------+
|              title 45 x 1                   |
+----------+----------------------------------+
|          |                                  |
|          |                                  |
|          |                                  |
|          |                                  |
| picture  |             misses               |
| 10 x 10  |             35 x 10              |
|          |                                  |
|          |                                  |
|          |                                  |
|          |                                  |
+----------+----------------------------------+
|              hits 45 x 1                    |
+---------------------------------------------+
Dare to pick a letter:
_

Example output:

                HANGMAN GAME
    _____
    |   |
        |
        |      MISSES:
        |      _ _ _ _ _ _ _ _ _ _
        |
        |
________|_
          _   _   _   _   _   _   _
Dare to pick a letter:
_
Parameters:
Raises:

hangman.utils.GameOverNotificationComplete

hangman.view.print_partial_body(picture, status)[source]
hangman.view.print_partial_hits(game_status)[source]
hangman.view.print_partial_message(flash, answer)[source]
hangman.view.print_partial_title()[source]
hangman.view.print_spacer()[source]

Print empty line

hangman.view.prompt_guess()[source]

Get a single letter.

hangman.view.prompt_play_again()[source]

Prompt user to play again.

hangman.view.say_goodbye()[source]

Write a goodbye message.