How darrensjz/battleship-in-scala works
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)