# darrensjz/battleship-in-scala architecture

> Architecture diagram of the GitHub repository darrensjz/battleship-in-scala, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/darrensjz/battleship-in-scala
- Repository: https://github.com/darrensjz/battleship-in-scala

## Overview

##### Project Type and Purpose
- This is a desktop, full‐feature, standalone ScalaFX application implementing the classic two-player Battleship game.
- It uses the MVC pattern: a View layer (FXML/CSS), a Controller layer (ScalaFX controllers), a Model layer (game entities and logic), and a lightweight persistence layer (ScalikeJDBC + embedded DB).

##### File‐Structure Analysis
- Top‐Level
- build.sbt, project/…: SBT build definitions and caches.
- src/main/scala: application code in packages com.dsjz.battleship.controller, model, util, plus MyApp.scala (entry point).
- src/main/resources: FXML layouts under view/, CSS styles, audio files, images.
- Patterns
- MVC: controllers under controller/, models under model/.
- UI: defined via FXML + controllers + ScalaFX glue in MyApp.scala.
- Persistence: Database.scala in util/ uses ScalikeJDBC to store/retrieve PlayerData.
- Assets: audio/ and images/ used by game logic and UI.

##### README Insights
- Confirms ScalaFX 8 (JavaFX under Java 8), ScalikeJDBC backed by Derby/H2, SBT build, and FXML/CSS for UI.
- Describes game features (ship placement, attack phases, ultimate abilities, forfeit, endgame dialog).
- Mentions Player vs Player mode and where persistence is used (player statistics).

a. Main Components
1. UI Layer
- FXML files + CSS (view definitions)
- ScalaFX Application (MyApp.scala)
2. Controller Layer
- One controller per screen (WelcomeController, ShipPlacementP1Controller, …, P1AttackPhaseController, etc.)
3. Model Layer
- Game Entities: Board, Ship, Player (Player1, Player2), Abilities (Salvo, CarpetBomb, Ultimate)
4. Persistence Layer
- Database util (ScalikeJDBC) + embedded DB (Derby/H2) for PlayerData
5. Resources
- Audio engine (plays Hit.mp3, Miss.mp3, etc.)
- Image assets
b. Relationships & Data Flow
- User clicks UI → FXML controller receives event → updates Model (e.g., Board.placeShip or Board.fireShot) → model returns result → controller updates view (highlight cells, play audio) → on game end or stats change → controller calls Database util to save/retrieve stats → returns to UI.
- MyApp.scala initializes DB connection, loads primary FXML (RootLayout.fxml), sets up navigation between screens.
c. Architectural Patterns
- MVC for UI separation
- Layered architecture: Presentation (FXML/CSS), Application logic (controllers), Domain model, Persistence
- Event-driven UI: ScalaFX + FXML
- Single‐process, in-memory/embedded DB
d. Key Technologies
- ScalaFX / JavaFX (UI toolkit)
- FXML + CSS (layout & styling)
- SBT (build tool)
- ScalikeJDBC + Derby/H2 (ORM & DB)
- Scala 2.12 (language)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Orchestrator
    MyApp["MyApp.scala"]:::orchestrator
    click MyApp "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/MyApp.scala"

    %% View Layer
    subgraph "View Layer" 
        direction TB
        RootLayoutFXML["RootLayout.fxml"]:::view
        click RootLayoutFXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/RootLayout.fxml"
        WelcomeFXML["Welcome.fxml"]:::view
        click WelcomeFXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/Welcome.fxml"
        AboutFXML["AboutGame.fxml"]:::view
        click AboutFXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/AboutGame.fxml"
        ShipP1FXML["ShipPlacementP1.fxml"]:::view
        click ShipP1FXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/ShipPlacementP1.fxml"
        ShipP2FXML["ShipPlacementP2.fxml"]:::view
        click ShipP2FXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/ShipPlacementP2.fxml"
        P1AttackFXML["P1AttackPhase.fxml"]:::view
        click P1AttackFXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/P1AttackPhase.fxml"
        P2AttackFXML["P2AttackPhase.fxml"]:::view
        click P2AttackFXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/P2AttackPhase.fxml"
        StatsFXML["PlayerStats.fxml"]:::view
        click StatsFXML "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/PlayerStats.fxml"
        WelcomeCSS["Welcome.css"]:::view
        click WelcomeCSS "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/Welcome.css"
        GridCSS["GridMap.css"]:::view
        click GridCSS "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/resources/com/dsjz/battleship/view/GridMap.css"
    end

    %% Controller Layer
    subgraph "Controller Layer"
        direction TB
        RootCtrl["RootLayoutController"]:::controller
        click RootCtrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/RootLayoutController.scala"
        WelcomeCtrl["WelcomeController"]:::controller
        click WelcomeCtrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/WelcomeController.scala"
        ShipP1Ctrl["ShipPlacementP1Controller"]:::controller
        click ShipP1Ctrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/ShipPlacementP1Controller.scala"
        ShipP2Ctrl["ShipPlacementP2Controller"]:::controller
        click ShipP2Ctrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/ShipPlacementP2Controller.scala"
        P1AttackCtrl["P1AttackPhaseController"]:::controller
        click P1AttackCtrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/P1AttackPhaseController.scala"
        P2AttackCtrl["P2AttackPhaseController"]:::controller
        click P2AttackCtrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/P2AttackPhaseController.scala"
        StatsCtrl["PlayerStatsController"]:::controller
        click StatsCtrl "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/controller/PlayerStatsController.scala"
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        Board["Board"]:::model
        click Board "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Board/Board.scala"
        Ship["Ship"]:::model
        click Ship "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Ship/Ship.scala"
        PlayerBase["Player"]:::model
        click PlayerBase "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Player/Player.scala"
        Player1["Player1"]:::model
        click Player1 "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Player/Player1.scala"
        Player2["Player2"]:::model
        click Player2 "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Player/Player2.scala"
        PlayerData["PlayerData"]:::model
        click PlayerData "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Player/PlayerData.scala"
        Salvo["SalvoAbility"]:::model
        click Salvo "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Abilities/SalvoAbility.scala"
        Carpet["CarpetBombAbility"]:::model
        click Carpet "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Abilities/CarpetBombAbility.scala"
        Ultimate["UltimateAbility"]:::model
        click Ultimate "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/model/Abilities/UltimateAbility.scala"
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        direction TB
        Database["Database.scala"]:::persistence
        click Database "https://github.com/darrensjz/battleship-in-scala/blob/main/src/main/scala/com/dsjz/battleship/util/Database.scala"
        DB["Embedded DB"]:::persistence
    end

    %% Resources
    subgraph "Resources"
        direction TB
        Audio["Audio Assets"]:::resource
        click Audio "https://github.com/darrensjz/battleship-in-scala/tree/main/src/main/resources/audio/"
        Images["Image Assets"]:::resource
        click Images "https://github.com/darrensjz/battleship-in-scala/tree/main/src/main/resources/images/"
    end

    %% Relationships
    MyApp -->|loads| RootLayoutFXML
    RootLayoutFXML -->|handled by| RootCtrl
    WelcomeFXML -->|handled by| WelcomeCtrl
    AboutFXML -->|handled by| WelcomeCtrl
    ShipP1FXML -->|handled by| ShipP1Ctrl
    ShipP2FXML -->|handled by| ShipP2Ctrl
    P1AttackFXML -->|handled by| P1AttackCtrl
    P2AttackFXML -->|handled by| P2AttackCtrl
    StatsFXML -->|handled by| StatsCtrl

    %% Controllers to Model
    ShipP1Ctrl -->|calls| Board
    P1AttackCtrl -->|calls| Ship
    P1AttackCtrl -->|calls| Player1
    P1AttackCtrl -->|calls| Player2
    P1AttackCtrl -->|uses| Salvo
    P1AttackCtrl -->|uses| Carpet
    P1AttackCtrl -->|uses| Ultimate
    StatsCtrl -->|uses| PlayerData

    %% Controllers to Persistence
    StatsCtrl -->|saves/loads| Database
    Database -->|stores| PlayerData

    %% Controllers & Model to Resources
    WelcomeCtrl -->|plays| Audio
    P1AttackCtrl -->|plays| Audio
    ShipP1Ctrl -->|plays| Audio
    Board -->|loads| Images

    %% Styles
    classDef orchestrator fill:#000000,stroke:#ffffff,color:#ffffff,stroke-width:2px
    classDef view fill:#87CEFA,stroke:#4682B4,color:#000000
    classDef controller fill:#98FB98,stroke:#2E8B57,color:#000000
    classDef model fill:#FFA500,stroke:#CD8500,color:#000000
    classDef persistence fill:#D3D3D3,stroke:#696969,color:#000000
    classDef resource fill:#DA70D6,stroke:#800080,color:#000000
```
