This project is a fully functional implementation of the popular dice game Kniffel (Yahtzee), built with Java.
It demonstrates strong Object-Oriented Programming (OOP) principles by separating game logic, rule validation, and user interface. The project features a dual-interface approach: the core logic powers both a classic Console Application and a graphical Desktop GUI built with Java Swing.
- Graphical User Interface (GUI): Interactive window with buttons and visual dice representation using
javax.swing. - Complete Ruleset: Implements all standard Kniffel rules (Full House, Straights, Kniffel, Bonus calculations).
- Rule Engine: A dedicated static utility class (
KniffelRules) handles complex score validation independently of the UI. - State Management: Holds logic for re-rolling specific dice and tracking game rounds.
The code is organized into a modular package structure in src/kniffel/.
src/kniffel/
├── Dice.java # Model: Manages the state of the 5 dice (values, hold status).
├── Scoreboard.java # Model: Tracks scores for all 13 categories (Upper/Lower section).
├── KniffelRules.java # Logic: Static Utility class to calculate points (e.g., checks for Full House).
├── KniffelGame.java # Controller (Console): Runs the text-based version of the game.
└── KniffelVisual.java # View/Controller (GUI): Runs the Swing-based graphical version.
-
Encapsulation: The Dice class encapsulates the random number generation and the "hold" mechanism, protecting the internal array state.
-
Static Logic: KniffelRules contains purely functional static methods (isFullHouse, calculateSum), making the logic testable and reusable for both the Console and GUI versions.
-
Event Handling: The GUI utilizes ActionListener inner classes to handle user inputs like rolling dice or selecting categories.
Prerequisites
- Java Development Kit (JDK) 17 or higher installed.
Installation & Execution
- Clone the repository
git clone [https://github.com/mlrlouis/Kniffel.git](https://github.com/mlrlouis/Kniffel.git)- Navigate to the project directory
cd Kniffel- Compile the source code
javac -d bin src/kniffel/*.java-
Run the Game
-
To run the GUI Version (Recommended):
java -cp bin kniffel.KniffelVisual
-
To run the Console Version:
java -cp bin kniffel.KniffelGame
-
-
Roll Dice: Click to roll. You have 3 rolls per round.
-
Hold Dice: Click "Würfel behalten" and enter the numbers (1-5) of the dice you want to keep (e.g., "1 4 5").
-
Select Category: Choose a scoring category from the dropdown menu and click "Kategorie auswählen" to lock in your score.