# altafino/connect-four architecture

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

- Interactive diagram: https://gitdiagram.com/altafino/connect-four
- Repository: https://github.com/altafino/connect-four

## Overview

##### Main Components
- Game Controller / Main Function
– Serves as the entry point, handling the overall game loop.
– Coordinates game setup, player turns, and result evaluation.

- Board/State Management
– Represents the 6x7 game board using a 2D slice.

- Player Input Handling
– Manages user input via the command line.
– Processes column selection, maps it to the board indices, and triggers game state updates.

- AI Engine
– Implements the Minimax algorithm with Alpha-Beta pruning.
– Contains functions like minimax, scorePosition, evaluateWindow, and explicit win/block checks (getComputerMove).
– Uses heuristics to evaluate board positions, adjust AI depth, and make strategic move decisions.

- Utility Functions and Constants
– Defines constants (e.g., board dimensions, player IDs, AI_DEPTH).
– Contains supporting logic for move validation and terminal state checks.

##### Relationships and Interactions
- The Main Function interacts with both the player input handler and the AI engine, alternating turns between the human and AI.
- The Board/State Management component underpins most operations – both the player’s moves and AI assessments update and consult the board state.
- The AI Engine analyzes the board state (via the game logic functions) to either block imminent threats or pursue winning moves and returns its move to the game controller.

##### Architectural Patterns and Design Principles
- Although the project is a monolithic, single-file implementation, it nonetheless demonstrates separation of concerns:
– Input/Output handling is separated from game logic.
– Game state management is distinct from decision-making logic (AI Engine).
– The AI Engine encapsulates strategic algorithms (Minimax and Alpha-Beta pruning) with a clear heuristic evaluation.

##### Elements
- Clear Labels:
- Annotations:

##### Essential Architectural Elements
- Although there is only one file, decompose the logic into the aforementioned components. This helps in conceptualizing the design and in potential future refactoring.
- Provide details on how the AI moves are computed, including the use of the Minimax algorithm and heuristic evaluation.
- That the game loop triggers checks after each move—this is critical in a turn-based game.

This detailed, component-based approach will ensure clarity and present an accurate architectural overview of the game for any developer or stakeholder reviewing the design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI Layer"
        PI["Player Input Handling"]:::cli
    end

    %% Game Logic Layer
    subgraph "Game Logic Layer"
        GM["Board/State Management"]:::logic
        UT["Utility Functions and Constants"]:::utility
        MC["Main Controller"]:::controller
    end

    %% AI Component
    subgraph "AI Component"
        AI["AI Engine"]:::ai
    end

    %% Connections
    PI -->|"processes_input"| GM
    GM -->|"updates_board/checks_state"| MC
    MC -->|"validates_moves"| UT
    MC -->|"triggers_AI_move"| AI
    AI -->|"computes_move"| GM
    MC -->|"prompts_next_move"| PI

    %% Click Events
    click PI "https://github.com/altafino/connect-four/blob/main/main.go"
    click GM "https://github.com/altafino/connect-four/blob/main/main.go"
    click UT "https://github.com/altafino/connect-four/blob/main/main.go"
    click MC "https://github.com/altafino/connect-four/blob/main/main.go"
    click AI "https://github.com/altafino/connect-four/blob/main/main.go"

    %% Styles
    classDef cli fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef logic fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffcc99,stroke:#333,stroke-width:2px;
    classDef controller fill:#fa8072,stroke:#333,stroke-width:2px;
    classDef ai fill:#dda0dd,stroke:#333,stroke-width:2px;
```
