A Hangman game initially looks like a small exercise: select a word, ask for letters, and draw the gallows. In practice, it combines several common programming tasks, including file access, data processing, input validation, and state management.
Keeping the Dictionary Outside the Code
The terms live in dictionary.json and are loaded into a list with System.Text.Json. Random then selects one entry and normalizes it to uppercase. This keeps the game logic independent of input casing and allows the dictionary to grow without changing the source code.
Before reading the file, the application checks whether it exists. This small guard prevents an uncontrolled failure and provides a clear message instead.
A Character Array as Visible State
The round starts with one underscore for every character in the selected word. This char[] acts as both data model and display state:
char[] placeholder = new char[suchWort.Length];
Array.Fill(placeholder, '_');
After a valid guess, the program compares every character in the word. Each match replaces the placeholder at the same position, so repeated letters are revealed in a single pass.
The win condition then becomes straightforward: when the array contains no underscores, the word is complete.
Making Incorrect Guesses Visible
Seven raw string literals represent the stages of the ASCII drawing. An incorrect guess reduces the remaining attempts and advances the display index. This keeps game state and visual feedback synchronized without rebuilding the drawing line by line.
A list of previously used characters prevents repeated guesses from being counted twice. Empty input is rejected before the application tries to access its first character.
What I Would Improve Next
The prototype supports a complete round. For the next iteration, I would separate three responsibilities:
- Loading and selecting words
- Managing the game state
- Handling console input and output
This separation would make the core rules testable without console interaction. Additional improvements could include replay support, a visible list of used letters, and difficulty levels based on word length.
The main result of this version is a closed game loop: every valid input changes the state in a predictable way, and every round ends clearly with a win or loss.
The Project
The project page contains the technical overview, feature list, and current status:
