# malintha-induwara/connect-four-game architecture

> Architecture diagram of the GitHub repository malintha-induwara/connect-four-game, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/malintha-induwara/connect-four-game
- Repository: https://github.com/malintha-induwara/connect-four-game

## Overview

##### Project Type and Purpose
- This is a full-stack desktop application (using JavaFX for UI and Java backend logic) that implements the classic Connect Four game.
- The project’s main emphasis is on the game logic (game board state, player actions, win condition evaluation) and the AI implementation using MCTS.
- It also includes testing and experimental code for comparing different MCTS versions or LLM produced implementations.

##### Analyzing the File Structure
- Top-level: You have configuration files (pom.xml, README.md, etc.) that set up the project as a Maven project.
- src/main/java: The core of the logic resides here. Packages include:
- lk.ijse.dep: General package structure.
- controller: Contains controllers such as BoardController and CreatePlayerController that handle UI events and game state changes.
- service: Contains the domain logic including Board, BoardImpl, BoardUI, Player, HumanPlayer, AiPlayer, and also the AI/LLM-related MCTS implementations (organized under service.llm/v1 and service.llm/v2).
- util: Contains utility classes like DEPAlert.
- src/main/resources: Provides UI resources:
- view: FXML files (Board.fxml and CreatePlayer.fxml) that define the JavaFX UI.
- style: CSS files for styling the UI.
- asset: (Possibly for images or other UI assets)
- The separation clearly indicates the use of an MVC/MVP approach where the Controllers manage UI events and delegate to Service classes.

a. Main Components to Represent:
- Application Bootstrap:
- Launcher.java and AppInitializer.java: These are responsible for starting up the JavaFX application and initializing the game.
- UI Layer (View):
- FXML files under src/main/resources/view (Board.fxml, CreatePlayer.fxml)
- CSS files under src/main/resources/style that style these views.
- Controller Layer:
- BoardController and CreatePlayerController which respond to UI actions and interact with the game logic.
- Service/Domain Layer:
- Core game components such as Board, BoardImpl, BoardUI, and core entities like Piece, Player, HumanPlayer, AiPlayer, Winner.
- AI Module:
- Specific MCTS implementations are found under src/main/java/lk/ijse/dep/service/llm (with versions v1 and v2). These are used by the AiPlayer or directly through service classes.
- Utility Components:
- Utility functions and alert mechanisms (such as those in DEPAlert.java).

b. Relationships and Data Flow:
- The Launcher/Application Initializer bootstraps the application.
- The UI loads the FXML files, which instantiate the controllers.
- Controllers receive user input and interact with the Service Layer to update game state.
- The Service Layer handles game logic including updating the Board, validating moves, and invoking the AI player.
- The AI (MCTS) module is called by the AiPlayer service or similar components when the computer’s move is required.
- Results (e.g., updated board state or game results) are sent back through the controller to update the UI.

c. Architectural Patterns:
- MVC/MVP Pattern: The clear separation between View (FXML and CSS), Controller (JavaFX controllers), and Model/Service (game and AI logic).
- Layered Architecture: UI, Controllers, Services, and Utility layers are distinctly separated.
- Modular Design: The LLM testing and MCTS implementations are encapsulated in distinct sub-packages (v1 and v2) to allow easy benchmarking or swapping.

d. Key Technologies and Frameworks:
- JavaFX for building the user interface.
- Maven for build and dependency management.
- Monte Carlo Tree Search algorithm (implemented in multiple versions) as the core AI.
- Java built-in libraries and potential dependency utilities for JavaFX (ensuring the project is run via the javafx:run Maven command).

##### Elements
- For each component (e.g., Launcher, AppInitializer, BoardController, CreatePlayerController, AiPlayer, Board, HumanPlayer, MCTS implementations):
- From Launcher/AppInitializer to the JavaFX UI initialization.
- From UI (FXML) to Controllers, indicating user input flow.
- From Controllers to Service/Domain components, illustrating command or data requests.
- From Service components to the AI module (MCTS) when an AI move is being calculated.
- From Service back to Controllers, and then Controllers back to UI (to update view).

##### Final Detailed Recommendations
- The Presentation Layer (JavaFX UI: FXML and CSS)
- The Controller Layer (BoardController, CreatePlayerController)
- The Service/Domain Layer (Board, Players, AiPlayer, and Game Logic)
- The AI Module (MCTS implementations in v1 and v2)
- Utility/Support elements.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
graph TD
    %% Application Bootstrap
    subgraph "Application Bootstrap"
        Launcher["Launcher"]:::bootstrap
        AppInitializer["AppInitializer"]:::bootstrap
    end

    %% UI Layer
    subgraph "UI Layer"
        subgraph "View (FXML)"
            BoardFXML["Board.fxml"]:::view
            CreatePlayerFXML["CreatePlayer.fxml"]:::view
        end
        subgraph "Style (CSS)"
            BoardCSS["Board.css"]:::css
            CreatePlayerCSS["CreatePlayer.css"]:::css
            StyleCSS["Style.css"]:::css
        end
    end

    %% Controller Layer
    subgraph "Controller Layer"
        BoardController["BoardController"]:::controller
        CreatePlayerController["CreatePlayerController"]:::controller
    end

    %% Service/Domain Layer
    subgraph "Service/Domain Layer"
        Board["Board"]:::service
        BoardImpl["BoardImpl"]:::service
        BoardUI["BoardUI"]:::service
        Piece["Piece"]:::service
        Player["Player"]:::service
        HumanPlayer["HumanPlayer"]:::service
        AiPlayer["AiPlayer"]:::service
        Winner["Winner"]:::service
    end

    %% AI Module
    subgraph "AI Module"
        MCTS_v1["MCTS v1"]:::ai
        MCTS_v2["MCTS v2"]:::ai
        MCTSNode["MCTSNode"]:::ai
    end

    %% Utility Components
    subgraph "Utility Components"
        DEPAlert["DEPAlert"]:::utility
    end

    %% Connections
    Launcher -->|"initializes"| AppInitializer
    AppInitializer -->|"loads"| BoardFXML
    AppInitializer -->|"loads"| CreatePlayerFXML
    BoardFXML -->|"binds"| BoardController
    CreatePlayerFXML -->|"binds"| CreatePlayerController

    BoardController -->|"updates"| BoardUI
    BoardController -->|"processes"| Board
    BoardController -->|"determines"| Winner
    CreatePlayerController -->|"manages"| Player

    Player -->|"extends"| HumanPlayer
    Player -->|"extends"| AiPlayer

    AiPlayer -->|"uses"| MCTS_v1
    AiPlayer -->|"uses"| MCTS_v2
    MCTS_v2 -->|"contains"| MCTSNode

    BoardUI -->|"implements"| BoardImpl

    BoardController -->|"alerts"| DEPAlert

    %% Link Style Resources to View
    BoardCSS -->|"styles"| BoardFXML
    CreatePlayerCSS -->|"styles"| CreatePlayerFXML
    StyleCSS -->|"styles"| BoardFXML
    StyleCSS -->|"styles"| CreatePlayerFXML

    %% Click Events
    click Launcher "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/Launcher.java"
    click AppInitializer "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/AppInitializer.java"
    click BoardFXML "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/resources/view/Board.fxml"
    click CreatePlayerFXML "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/resources/view/CreatePlayer.fxml"
    click BoardCSS "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/resources/style/Board.css"
    click CreatePlayerCSS "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/resources/style/CreatePlayer.css"
    click StyleCSS "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/resources/style/Style.css"
    click BoardController "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/controller/BoardController.java"
    click CreatePlayerController "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/controller/CreatePlayerController.java"
    click Board "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/Board.java"
    click BoardImpl "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/BoardImpl.java"
    click BoardUI "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/BoardUI.java"
    click Piece "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/Piece.java"
    click Player "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/Player.java"
    click HumanPlayer "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/HumanPlayer.java"
    click AiPlayer "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/AiPlayer.java"
    click Winner "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/Winner.java"
    click MCTS_v1 "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/llm/v1/MCTS.java"
    click MCTS_v2 "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/llm/v2/MCTS.java"
    click MCTSNode "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/service/llm/v2/MCTSNode.java"
    click DEPAlert "https://github.com/malintha-induwara/connect-four-game/blob/main/src/main/java/lk/ijse/dep/util/DEPAlert.java"

    %% Styles
    classDef bootstrap fill:#f9c,stroke:#333,stroke-width:2px;
    classDef view fill:#bbf,stroke:#333,stroke-width:2px;
    classDef css fill:#cfc,stroke:#333,stroke-width:2px;
    classDef controller fill:#fc9,stroke:#333,stroke-width:2px;
    classDef service fill:#cfc,stroke:#333,stroke-width:2px;
    classDef ai fill:#ffc,stroke:#333,stroke-width:2px;
    classDef utility fill:#cff,stroke:#333,stroke-width:2px;
```
