Console Hangman game in Ruby with test coverage and save/load functionality
A classic Hangman game implemented in Ruby. The player guesses a word letter by letter, with a limited number of attempts (7 errors).
Words are loaded from a text file or passed via command-line arguments.
You can save the current game to a JSON file and later restore it from the list of saved games.
# Run with a random word from file
ruby main.rb
# Run with a specific word
ruby main.rb word- Player must guess the hidden word by suggesting letters
- Maximum allowed errors: 7
- Correct letter reveals its position(s) in the word
- Incorrect letter increases the error counter
- Victory: all letters are guessed
- Defeat: error limit is reached
- All saves are stored in a single file:
data/saves.json - Format: JSON
hangman_with_test/
├── main.rb # Application entry point
├── lib/ # Game modules
│ ├── game.rb # Game logic
│ ├── result_printer.rb # Results output
│ ├── word_reader.rb # Word reading
│ └── save.rb # Save/load game state (JSON)
├── spec/ # RSpec tests
│ └── game_spec.rb # Game logic tests
├── data/ # Data files
│ ├── words.txt # Word list
│ └── saves.json # Saved games (JSON)
└── image/ # Visual assets (ASCII art)
- Ruby 3.4.5
- OOP (Object-Oriented Programming)
- Separation of concerns between classes
- Game logic encapsulation
- Proper use of attributes and accessor methods
- Modular architecture - separation into independent components
- Single Responsibility Principle - each class has one responsibility
- Separation of Concerns - separating logic from presentation
- Game state management
- Player move processing
- Win/loss condition checking
- Duplicate letter validation
- Reading words from file
- Getting words from command-line arguments
- Random word selection from list
- Game status display
- Progress visualization
- Results output
- Working with the
data/saves.jsonfile - Reading all saves
- Saving the current game state with a new ID
- Loading a game by its ID and restoring its state
- RSpec - testing framework
- Unit tests for game logic validation
- Test coverage for win and loss scenarios
- Exception handling (
ArgumentError,RuntimeError) - Proper interrupt handling (
Interrupt) - Input data validation
- Graceful degradation
- Reading from text files
- UTF-8 encoding support
- Interactive input/output
- User input processing
- Formatted results output
# Install dependencies
gem install rspec
# Run tests
rspec spec/game_spec.rb- Ruby >= 3.0
- RSpec (for testing)
- UTF-8 support
✅ Console application development in Ruby
✅ Object-oriented design
✅ Writing modular and extensible code
✅ Unit testing with RSpec
✅ Exception and error handling
✅ File system operations
✅ Working with encodings (UTF-8)
✅ Git version control
✅ Code documentation