You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor: Decouple game logic (Patience Game) from UI for SRP adherence
This commit refactors the Patience Solitaire game to better align
with the Single Responsibility Principle (SRP). The primary goal was
to separate core game logic from user interface concerns, leading to
a more modular, testable, and maintainable codebase.
Key changes include:
1. **`PatienceGame` Class (Game Engine):**
* Removed all direct console input (`Scanner`) and output (`System.out.println`).
* Methods performing game actions (e.g., `moveCard`, `drawCard`, `moveUncoveredCardToSuit`) now return boolean status or appropriate values instead of printing messages.
* The `playGame()` game loop and `displayGameState()` methods were removed.
* Added getter methods to allow external classes (like the UI) to access game state information.
* The `main()` method was removed.
* Refined card movement logic (e.g., `canMoveCardToLane`, `moveMultipleCards`) to rely on return values and internal state checks.
2. **`GameConsoleUI` Class (New - Console User Interface):**
* Created to handle all console-based user interactions.
* Manages the main game loop (`start()` method).
* Contains `Scanner` for user input and uses `System.out.println` for all output.
* Implements `displayGameState()` to render the game board and status.
* Parses user commands and invokes corresponding methods on the `PatienceGame` instance.
* Handles displaying error or success messages based on return values from `PatienceGame`.
* Includes logic for `initializeLaneCards()` to correctly set up the tableau with face-up/face-down cards at the start.
3. **`Application` Class (New - Entry Point):**
* Created to serve as the main entry point for the application.
* Its `main()` method initializes `PatienceGame` and `GameConsoleUI` instances and starts the game.
4. **`Card` Class:**
* Added a `faceUp` boolean property and associated `isFaceUp()`, `setFaceUp()` methods to represent card visibility in Solitaire.
* Enhanced `toString()` to display cards differently based on their `faceUp` status (e.g., "[XX]" for face-down).
* Reviewed and ensured methods like `isOneValueHigher` and `isOppositeColor` correctly support game rules.
Benefits of this refactoring:
- **Improved Modularity:** Game logic is now independent of its presentation.
- **Enhanced Testability:** `PatienceGame` can be unit-tested without UI dependencies.
- **Increased Maintainability:** Changes to UI do not affect game logic, and vice-versa.
- **Flexibility:** Easier to introduce new UIs (e.g., GUI, web) in the future by creating new UI classes that interact with the existing `PatienceGame` engine.
0 commit comments