diff --git a/CODE_SUMMARY.md b/CODE_SUMMARY.md new file mode 100644 index 0000000..9b2f418 --- /dev/null +++ b/CODE_SUMMARY.md @@ -0,0 +1,724 @@ +# Code Operation Summary - Rivals for Catan Implementation + +## Executive Summary + +This document provides a comprehensive overview of how the Rivals for Catan card game implementation operates, including its functionalities, architectural relationships, and communication flows. The codebase is a well-structured, modular Java application that implements the Rivals for Catan card game with support for both local and network multiplayer gameplay. + +--- + +## 1. Core Functionalities + +### 1.1 Game Management +The system provides complete game flow management: +- **Game Initialization**: Set up players, decks, and starting boards +- **Turn Management**: Orchestrate turn sequences including pre-roll, event die, production, and action phases +- **Victory Tracking**: Monitor victory points and determine when a player reaches 7+ VP to win +- **Event System**: Handle event die outcomes (Brigand, Trade, Celebration, Event Cards) +- **Card Effects**: Execute diverse card effects (resource generation, placement, trading, etc.) + +### 1.2 Card and Deck Management +- **Card Loading**: Parse and load cards from `cards.json` configuration file +- **Deck Organization**: Manage multiple deck types: + - 49 center cards (24 regions, 9 settlements, 7 cities, 9 roads) + - 36 basic action cards (distributed across 4 stacks) + - 9 event cards (with "Yule" positioned 4th from bottom) +- **Card Distribution**: Deal opening hands, replenish cards, and handle deck exhaustion +- **Production**: Generate resources based on dice rolls and region tiles + +### 1.3 Placement and Building +- **Structure Placement**: Validate and place settlements, roads, cities, and buildings +- **Placement Rules**: Enforce game rules: + - Settlements require adjacent roads and no adjacent settlements + - Roads must connect to existing structures + - Cities require existing settlements at that location + - Buildings require appropriate slots in settlements/cities +- **Upgrade System**: Handle settlement-to-city upgrades + +### 1.4 Resource and Trade Management +- **Resource Tracking**: Manage 5 resource types (Lumber, Ore, Wool, Grain, Gold) +- **Trading**: Support bank trades (4:1 or 3:1 with discounts) and player-to-player trades +- **Cost Validation**: Verify players can afford card plays and building placements +- **Resource Production**: Generate resources based on dice rolls and production boosters + +### 1.5 Special Effects and Abilities +The system implements numerous special card effects: + +**Action Cards:** +- Goldsmith: Exchange gold for victory points +- Merchant: Trade advantages and resource manipulation +- Merchant Caravan: Enhanced trading capabilities +- Road Building: Place roads at reduced cost +- Relocation: Move structures on the board +- Brigitta the Wise: Pre-roll phase effects +- Scout: Card drawing mechanics +- Harvest: Enhanced resource generation + +**Event Cards:** +- Feud: Competitive effects between players +- Fraternal Feuds: Player conflict mechanics +- Invention: Free upgrades +- Trade Ships Race: Trading competitions +- Traveling Merchant: Merchant visits +- Year of Plenty: Free resources +- Yule: Special celebration effects + +**Building Effects:** +- Abbey: Card draw bonuses +- Marketplace: Trade discounts +- Storehouse: Resource storage bonuses +- Toll Bridge: Opponent resource penalties + +**Unit Effects:** +- Common Heroes: Combat and defense +- Trade Ships: Enhanced trading +- Large Trade Ships: Superior trading capabilities + +### 1.6 Input/Output Systems +- **Console Interface**: Text-based gameplay for local multiplayer +- **Network Support**: Socket-based networking for online multiplayer +- **Mock I/O**: Test doubles for automated testing +- **Board Display**: ASCII-based board visualization showing regions, structures, and resources + +### 1.7 Testing Infrastructure +Comprehensive test suite covering: +- Card effect behaviors +- Placement rules and validation +- Production mechanics +- Trade operations +- Event handling +- Victory conditions +- Edge cases and error scenarios + +--- + +## 2. Relationship Between Classes and Folders + +### 2.1 Package Structure and Responsibilities + +``` +HomeExam/ +├── src/ +│ ├── Main.java # Application entry point +│ │ +│ ├── model/ # Domain Models (12 classes) +│ │ ├── Card.java # Base card abstraction +│ │ ├── BasicCard.java # Action/building cards +│ │ ├── CenterCard.java # Region/settlement/city/road cards +│ │ ├── EventCard.java # Event deck cards +│ │ ├── Deck.java # Generic deck container +│ │ ├── Player.java # Player state and hand +│ │ ├── Principality.java # Player's board/grid +│ │ ├── RegionTile.java # Individual region tile +│ │ ├── Resource.java # Resource types enum +│ │ ├── Structure.java # Structure types enum +│ │ ├── VictoryPointTracker.java # VP calculation +│ │ └── Points.java # Point types (VP, CP, SP, etc.) +│ │ +│ ├── controller/ # Game Logic (56 classes) +│ │ ├── GameController.java # Main coordinator +│ │ ├── TurnManager.java # Turn sequencing +│ │ ├── DeckManager.java # Deck operations +│ │ ├── EventManager.java # Event coordination +│ │ ├── CardFactory.java # Card creation from JSON +│ │ ├── RuleValidator.java # Rule enforcement +│ │ ├── EffectCatalog.java # Effect registry +│ │ ├── EffectRegistry.java # Effect registration helper +│ │ ├── GameContext.java # Shared game state +│ │ ├── SetupService.java # Game initialization +│ │ ├── TurnState.java # Turn state tracking +│ │ ├── AdvantageManager.java # Player advantages +│ │ ├── ICardEffect.java # Effect interface +│ │ │ +│ │ ├── interfaces/ # Controller contracts +│ │ ├── actions/ # Action card effects (9 classes) +│ │ ├── event/ # Event card effects (7 classes) +│ │ ├── eventDieEvents/ # Event die outcomes (4 classes) +│ │ ├── phases/ # Game phases (1 class) +│ │ ├── placement/ # Placement handlers (8 classes) +│ │ └── settlement/ # Settlement effects +│ │ ├── buildings/ # Building effects (4 classes) +│ │ └── units/ # Unit effects (3 classes) +│ │ +│ ├── view/ # Display Layer +│ │ └── BoardPrinter.java # ASCII board rendering +│ │ +│ ├── io/ # I/O Abstractions +│ │ ├── ConsoleInput.java # Console input +│ │ ├── ConsoleOutput.java # Console output +│ │ ├── SocketInput.java # Network input +│ │ ├── SocketOutput.java # Network output +│ │ ├── MockIO.java # Test I/O +│ │ └── interfaces/ # I/O contracts +│ │ +│ ├── network/ # Networking Layer +│ │ ├── Server.java # Game server +│ │ ├── ServerHandler.java # Server logic +│ │ ├── ClientConnection.java # Client connection +│ │ ├── OnlinePlayer.java # Remote player +│ │ ├── NetworkService.java # Network utilities +│ │ └── interfaces/ # Network contracts +│ │ +│ ├── util/ # Utilities +│ │ ├── Dice.java # Dice rolling +│ │ ├── Logger.java # Logging +│ │ ├── Randomizer.java # RNG abstraction +│ │ ├── GameConfig.java # Game configuration +│ │ ├── CostParser.java # Cost parsing +│ │ └── policy/ # Game policies +│ │ ├── PlacementPolicy.java +│ │ ├── ProductionPolicy.java +│ │ ├── TradePolicy.java +│ │ ├── VictoryPolicy.java +│ │ └── impl/BasePolicies.java +│ │ +│ └── tests/ # Unit Tests (20+ test classes) +│ +├── cards.json # Card definitions +├── gson.jar # JSON library +└── Documentation files (*.md) +``` + +### 2.2 Key Relationships + +#### Layered Architecture: +1. **Presentation Layer** → Main.java, view/ +2. **Application Layer** → controller/ (GameController, TurnManager, etc.) +3. **Business Logic Layer** → controller/ subdirectories (actions/, events/, placement/) +4. **Domain Layer** → model/ +5. **Infrastructure Layer** → io/, network/, util/ +6. **Testing Layer** → tests/ + +#### Dependency Flow: +- **Main → Controller → Model**: One-way dependency from entry point through controllers to domain +- **Controller → I/O**: Controllers use I/O abstractions via interfaces +- **Effects → Model**: All effects modify Player/Principality state +- **View → Model**: View reads model state for display +- **Network → I/O**: Network implements I/O interfaces +- **Util → Independent**: Utilities have no dependencies on other packages + +### 2.3 Design Patterns Used + +1. **Strategy Pattern**: ICardEffect interface with multiple implementations +2. **Registry Pattern**: EffectCatalog maps effect names to implementations +3. **Factory Pattern**: CardFactory creates cards from JSON +4. **Dependency Injection**: Controllers receive dependencies via constructors +5. **Abstract Factory**: PlacementRegistry for creating placement handlers +6. **Template Method**: PlacementHandler defines placement workflow +7. **Observer Pattern** (implicit): GameContext for shared state +8. **Adapter Pattern**: Socket I/O adapters for network play + +--- + +## 3. Communication Flow + +### 3.1 Application Startup Flow + +``` +1. Main.main() + ↓ +2. Initialize components: + - Randomizer (RNG) + - GameConfig (game rules) + - ConsoleInput/ConsoleOutput (I/O) + ↓ +3. Load cards from cards.json: + - CardFactory.loadBasic() + - Parse JSON and create card objects + - Fallback to placeholder decks on failure + ↓ +4. Create DeckManager: + - Organize cards into decks (basic, event, center) + - Shuffle decks + ↓ +5. Initialize game components: + - RuleValidator (rule enforcement) + - EventManager (event handling) + - TurnManager (turn coordination) + - GameController (main orchestrator) + ↓ +6. Create players: + - Player("Player 1") + - Player("Player 2") + ↓ +7. Deal opening hands: + - Each player chooses a stack (1-4) + - Draw 3 cards from chosen stack + ↓ +8. Setup starting boards: + - SetupService.seedIntroPrincipality() + - Place initial regions, settlements, roads + ↓ +9. Display boards: + - BoardPrinter.printPlayerBoard() + ↓ +10. Randomly determine starting player + ↓ +11. Enter game loop +``` + +### 3.2 Turn Execution Flow + +``` +TurnManager.playTurn(current, opponent) + ↓ +Phase 1: PRE-ROLL PHASE + - Check for pre-roll effects (e.g., Brigitta the Wise) + - Execute pre-roll card effects if any + ↓ +Phase 2: EVENT DIE ROLL + - Dice.roll(6) → Event die result (1-6) + - Parse event result: + • 1-3: Brigand (steal resource from opponent) + • 4: Trade (execute trade event) + • 5: Celebration (bonus resources) + • 6: Draw and execute event card + ↓ +Phase 3: PRODUCTION PHASE + - Roll production die: Dice.roll(6) + - For each region with matching dice number: + • Generate resources + • Apply production boosters (e.g., Storehouse) + • Store resources on region tiles + - Collect resources from all regions + ↓ +Phase 4: ACTION PHASE (repeating until player passes) + - Display menu: + 1. Build structure (settlement/road/city/building) + 2. Play action card + 3. Trade with bank/opponent + 4. End turn + ↓ + Player chooses action: + + BUILD STRUCTURE: + - Select structure type + - Get placement handler from PlacementRegistry + - Validate placement rules + - Check resource cost + - Deduct resources + - Place structure on Principality + - Update victory points + - Return to action menu + + PLAY CARD: + - Select card from hand + - Get effect from EffectCatalog + - Validate preconditions (e.g., can afford) + - Execute ICardEffect.apply(GameContext) + - Remove card from hand + - Apply card effects (placement, resources, etc.) + - Return to action menu + + TRADE: + - Choose trade type (bank/opponent) + - Select resources to exchange + - Validate trade (4:1 or 3:1 ratio) + - Execute resource exchange + - Return to action menu + + END TURN: + - Exit action phase + ↓ +Phase 5: REPLENISH PHASE + - DeckManager.replenish() + - Player may draw cards to refill hand + ↓ +Phase 6: EXCHANGE PHASE + - Optional: Exchange resources with bank + ↓ +Phase 7: VICTORY CHECK + - RuleValidator.hasWon(player) + - Check if player has 7+ victory points + - If yes: End game, declare winner + - If no: Switch to opponent's turn +``` + +### 3.3 Card Effect Execution Flow + +``` +Player plays action card + ↓ +GameController receives play card request + ↓ +1. Retrieve card from player's hand + ↓ +2. Validate preconditions: + - RuleValidator.canAfford(player, card) + - Check card-specific prerequisites + ↓ +3. Deduct resource cost: + - player.spendResources(card.getCost()) + ↓ +4. Create GameContext: + - Contains: player, opponent, game state, I/O + ↓ +5. Lookup effect in EffectCatalog: + - EffectCatalog.get(card.getName()) + - Returns ICardEffect implementation + ↓ +6. Execute effect: + - ICardEffect.apply(GameContext) + ↓ +7. Effect-specific logic (examples): + + Goldsmith Effect: + - Prompt for number of gold to convert + - Validate player has enough gold + - Deduct gold resources + - Add victory points + - Update VictoryPointTracker + + Merchant Caravan Effect: + - Grant trading advantage + - Enable 3:1 trade ratio + - Player performs trades + - Remove advantage after use + + Road Building Effect: + - Get RoadPlacementHandler + - Prompt for road positions + - Validate placement rules + - Place roads at reduced cost + - Update Principality + + Abbey Effect: + - Check player has city + - Place Abbey in city slot + - Grant card draw bonus + - Trigger on specific events + + Scout Effect: + - Draw cards from basic deck + - Add to player hand + - No resource cost + ↓ +8. Update game state: + - Modify player resources + - Update board state + - Recalculate victory points + ↓ +9. Notify players: + - Display effect results + - Update board display + ↓ +10. Remove card from hand (if one-time use) + ↓ +11. Return to action phase +``` + +### 3.4 Placement Validation Flow + +``` +Player requests structure placement + ↓ +GameController.handlePlacement() + ↓ +1. PlacementRegistry.getHandler(cardType) + - Returns appropriate PlacementHandler: + • SettlementPlacementHandler + • RoadPlacementHandler + • CityPlacementHandler + • GenericBuildingPlacementHandler + • AbbeyPlacementHandler + • HeroPlacementHandler + ↓ +2. PlacementHandler.canHandle(card) → boolean + ↓ +3. PlacementHandler.place(card, context): + + For Settlement: + a. Prompt player for intersection coordinates + b. Validate rules: + - Is intersection empty? + - Is there an adjacent road owned by player? + - Are there no adjacent settlements? + c. If valid: + - Place settlement on Principality + - Add 1 victory point + - Return success + d. If invalid: + - Display error message + - Return failure + + For Road: + a. Prompt for edge coordinates + b. Validate rules: + - Is edge empty? + - Is road connected to player's structure? + c. If valid: + - Place road on Principality + - Return success + d. If invalid: + - Return failure + + For City: + a. Prompt for settlement to upgrade + b. Validate rules: + - Does settlement exist at location? + - Does player own the settlement? + - Can player afford upgrade cost? + c. If valid: + - Upgrade settlement to city + - Add 1 victory point + - Return success + d. If invalid: + - Return failure + + For Building (Abbey, Marketplace, etc.): + a. Find available building slot + b. Validate rules: + - Is slot available in settlement/city? + - No duplicate building type? + - Meets prerequisites (e.g., city for Abbey)? + c. If valid: + - Place building in slot + - Activate building effect + - Return success + d. If invalid: + - Return failure + ↓ +4. Update Principality state + ↓ +5. Recalculate victory points: + - VictoryPointTracker.calculate(player) + ↓ +6. Display updated board: + - BoardPrinter.printPlayerBoard() + ↓ +7. Return placement result +``` + +### 3.5 Network Multiplayer Flow (Future Feature) + +``` +HOSTING PLAYER: + Server.main() + ↓ + 1. Initialize server + 2. Listen on port + 3. Wait for client connection + ↓ + 4. Client connects + 5. Create ClientConnection + 6. Create OnlinePlayer with SocketI/O + 7. Synchronize game state + ↓ + 8. Enter game loop (same as local) + +CLIENT PLAYER: + NetworkService.connect() + ↓ + 1. Connect to server IP:port + 2. Create SocketInput/SocketOutput + 3. Receive initial game state + ↓ + 4. Enter game loop + +DURING GAMEPLAY: + Local player action: + - Execute locally + - Broadcast state to remote + - Update both clients + + Remote player action: + - Receive action via socket + - Validate action + - Execute if valid + - Broadcast state update + - Update both clients +``` + +### 3.6 Data Flow Through System + +``` +cards.json (file) + ↓ [parsed by] +CardFactory + ↓ [creates] +Card objects (BasicCard, CenterCard, EventCard) + ↓ [organized by] +DeckManager + ↓ [into] +Deck instances + ↓ [distributed to] +Player.hand (List) + ↓ [during turn] +GameController receives player action + ↓ [validates via] +RuleValidator + ↓ [executes via] +EffectCatalog → ICardEffect + ↓ [modifies] +Player (resources, hand) +Principality (board state) +VictoryPointTracker (points) + ↓ [displayed via] +BoardPrinter → IOutputService → Console/Socket + ↓ [continues until] +hasWon(player) == true +``` + +--- + +## 4. Key Design Principles + +### 4.1 SOLID Principles Adherence + +1. **Single Responsibility**: Each class has one clear purpose + - GameController: orchestrates game flow + - DeckManager: manages decks + - EventManager: handles events + - Each effect class: implements one card behavior + +2. **Open/Closed**: Extensible without modification + - New cards: add ICardEffect implementation, register in EffectCatalog + - New placement types: add PlacementHandler, register in PlacementRegistry + - New I/O: implement IInputService/IOutputService + - New policies: implement policy interfaces + +3. **Liskov Substitution**: Subtypes are substitutable + - All Card subtypes work with Deck + - All I/O implementations work interchangeably + - All effect implementations work through ICardEffect + +4. **Interface Segregation**: Focused interfaces + - IInputService, IOutputService (separate concerns) + - ICardEffect (single method) + - Policy interfaces (specific to purpose) + +5. **Dependency Inversion**: Depend on abstractions + - Controllers depend on I/O interfaces, not implementations + - Effects depend on GameContext abstraction + - High-level modules independent of low-level details + +### 4.2 Modifiability and Extensibility + +**Easy to extend:** +- Add new cards: Just implement ICardEffect and register +- Add new eras: Implement new policy interfaces +- Add new placement types: Implement PlacementHandler +- Add new I/O methods: Implement IInputService/IOutputService +- Add new game modes: Extend GameConfig + +**Hard to break:** +- Core game logic isolated from I/O +- Effects are independent and isolated +- Placement validation centralized +- Tests verify behavior + +### 4.3 Testability + +- **Mock I/O**: MockIO for automated testing without user input +- **Dependency Injection**: All dependencies passed via constructors +- **Small, focused classes**: Each class easily testable in isolation +- **Randomizer abstraction**: Dice and Randomizer can be mocked for deterministic tests +- **20+ test classes**: Comprehensive test coverage + +--- + +## 5. Technology Stack + +- **Language**: Java +- **Build Tool**: Manual compilation (javac) with Maven structure +- **Libraries**: + - Gson (JSON parsing) + - JUnit Jupiter 5.10.2 (testing) +- **Architecture**: Layered, modular +- **Paradigm**: Object-oriented with functional elements (Strategy pattern) + +--- + +## 6. File Organization + +### 6.1 Configuration Files +- `cards.json`: Card definitions with effects, costs, and properties +- `pom.xml`: Maven/JUnit configuration +- `.gitignore`: Git exclusions + +### 6.2 Documentation Files +- `README.md`: Quick start and structure reference +- `ARCHITECTURE.md`: Architecture details and design rationale +- `FOLDER_STRUCTURE.md`: Detailed folder/class diagram +- `FOLDER_CONNECTIONS.md`: Package dependencies and relationships +- `GAME_FLOW.md`: Game flow and sequence diagrams +- `DESIGN_REPORT.md`: SOLID principles and design analysis +- `TEST_SUMMARY.md`: Test coverage summary +- `ANSWERS_TO_QUESTIONS.md`: Q&A documentation +- `Rivals_for_Catan_Rulebook.pdf`: Official game rules + +### 6.3 Build Artifacts +- `target/`: Compiled classes (generated) +- `gson.jar`: JSON library dependency + +--- + +## 7. Execution Instructions + +### 7.1 Local Compilation and Execution + +```bash +# Navigate to project directory +cd /home/runner/work/HomeExam/HomeExam + +# Clean previous build +rm -rf bin +mkdir -p bin + +# Compile all Java files with gson dependency +javac -d bin -cp "gson.jar" $(find src -name "*.java") + +# Run the game +java -cp "bin:gson.jar" src.Main +``` + +### 7.2 Network Multiplayer (Future) + +```bash +# Terminal A (Host) +java -cp "bin:gson.jar" src.network.Server + +# Terminal B (Client) +java -cp "bin:gson.jar" src.network.Server online +``` + +--- + +## 8. Summary + +### 8.1 What the Code Does +The codebase implements a complete, playable version of Rivals for Catan card game with: +- Full game mechanics (production, trading, building, events) +- 100+ different cards with unique effects +- Comprehensive rule validation +- Victory point tracking +- Support for local and network multiplayer +- Extensive test coverage + +### 8.2 How It's Organized +- **Layered architecture**: Presentation → Application → Business → Domain → Infrastructure +- **Modular packages**: Each package has clear responsibility +- **56 controller classes**: Distributed across specialized subdirectories +- **12 model classes**: Core domain entities +- **20+ test classes**: Ensuring correctness + +### 8.3 How Components Communicate +- **Main** initializes and orchestrates via **GameController** +- **GameController** coordinates **TurnManager**, **DeckManager**, **EventManager**, **RuleValidator** +- **TurnManager** executes turn phases and delegates to effect implementations +- **Effects** modify **Model** objects (**Player**, **Principality**) +- **View** reads **Model** and renders via **I/O** abstractions +- **Network** layer enables remote play by implementing **I/O** interfaces + +### 8.4 Key Strengths +1. **Well-documented**: Extensive markdown documentation +2. **Testable**: Comprehensive test suite with mock I/O +3. **Extensible**: Easy to add new cards, effects, and features +4. **Maintainable**: Clear separation of concerns, SOLID principles +5. **Flexible I/O**: Console, network, and test I/O interchangeable +6. **Policy-based**: Game rules can be customized via policy interfaces + +--- + +## Conclusion + +This is a well-architected implementation of Rivals for Catan that demonstrates professional software engineering practices. The code is organized into clear layers with well-defined responsibilities, uses design patterns appropriately, and is built to be extended without modification. The comprehensive documentation and test coverage make it maintainable and reliable for future enhancements.