Code Monkey home page Code Monkey logo

cs50-hangman's Introduction

Hangman Game

Description:

This project is a simple implementation of the classic game "Hangman" in Python. The game is designed to be run in the command line and offers a fun and interactive way to guess words letter by letter.

In this version of Hangman, a player tries to guess a word by suggesting letters within a certain number of guesses. Each wrong guess brings the hangman closer to being fully drawn and the game closer to an end. The game uses a list of words imported from a file and ASCII art to visually represent the hangman's stages.

How to Play

  1. The game selects a random word from a predefined list of words.
  2. The player guesses one letter at a time.
  3. If the guessed letter is in the word, the game reveals its position(s) in the word.
  4. If the guessed letter is not in the word, one life is deducted and part of the hangman is drawn.
  5. The game continues until either the player guesses the word correctly or runs out of lives.

Features

  • Random word selection from an external file.
  • Dynamic ASCII art to represent the hangman.
  • Input validation to ensure that only single letters are accepted as guesses.
  • Case-insensitive guessing.

Requirements

  • Python 3.x
  • External files: words.txt for the word list and hangman.txt for the ASCII art stages.

Setup and Running

  1. Ensure Python 3.x is installed on your system.
  2. Clone this repository or download the source code.
  3. Place words.txt and hangman.txt in the same directory as project.py.
  4. Run the game using the command python project.py.

Project Structure

  • project.py: Contains the main game logic and functions.
  • words.txt: A text file containing possible words to guess.
  • hangman.txt: A text file containing the ASCII art for the hangman stages.
  • lose.txt: A text file containing the ASCII art for the game over - lose.
  • win.txt: A text file containing the ASCII art for the game over - win.
  • test_project.py: Contains tests for key functions of the game, executable with pytest.

Testing

This project includes unit tests for the words_import, hangman_import, and print_hangman_word functions, ensuring that key components of the game behave as expected under various conditions. Here's a brief overview of what each test verifies:

  • test_words_import: Checks if the words_import function successfully reads from the words.txt file, returns a list, and ensures that the list contains strings representing words. This test ensures that the game has a valid word list to choose from.

  • test_hangman_import: Ensures that the hangman_import function properly loads the ASCII art representations of the hangman's stages from hangman.txt into a dictionary, validating the structure and content of this dictionary. This is crucial for visually representing the game's progress.

  • test_print_hangman_word: Verifies the print_hangman_word function's ability to correctly print the current state of the word being guessed, with letters either revealed or represented as underscores based on the player's guesses. This test captures and examines the function's output to ensure it matches the expected format.

To run these tests, ensure you have pytest installed in your environment:

pip install pytest

Then, execute the following command in the terminal in your project's root directory:

pytest test_project.py

Pytest will automatically discover and run the tests defined in test_project.py, providing feedback on their success or failure. This testing step is crucial for maintaining the game's functionality and making it robust against errors.

Author

  • Name: Tomislav Dukez
  • GitHub: tomdu3
  • EdX: tomdu3
  • Location: London, UK
  • Date: 3 April 2024

Acknowledgments

Enjoy playing Hangman and feel free to contribute or suggest improvements!

cs50-hangman's People

Contributors

tomdu3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.