[fix] Demo draw state no longer flashing and adding switch to demo if player(s) abandon game.

This commit is contained in:
2026-03-07 08:28:29 +01:00
parent 73981c95c5
commit 5238fbf0f5
2 changed files with 122 additions and 107 deletions
+43 -39
View File
@@ -1,6 +1,6 @@
# 🕹️ Connect 4 AI: Master Edition
# 🕹️ Connect 4 AI: Master Edition (v2.0)
A high-performance Connect 4 implementation for ESP32-C3 and 8x8 WS2812B matrices. Features dynamic difficulty scaling, "humanized" AI movement, and a mobile-friendly web administration portal.
A high-performance, feature-rich Connect 4 implementation for the ESP32-C3. This version features a "living" AI that evolves as you play, human-like movement animations, and a robust win-detection engine.
---
@@ -15,68 +15,72 @@ A high-performance Connect 4 implementation for ESP32-C3 and 8x8 WS2812B matrice
| **Rotary Encoder B** | `GPIO 1` | Directional DT |
| **Encoder Button** | `GPIO 2` | Selection (SW) |
### 📐 Physical Dimensions
### 📐 Physical Layout
Designed for standard 8x8 matrix modules (approx. 65mm x 67mm).
The project is optimized for an 8x8 NeoPixel Matrix (65mm x 67mm).
- **Top Row (0):** Interaction and AI decision visualization.
- **Game Board:** Standard $7 \times 6$ grid.
- **UI Borders:** Fixed blue frame for visibility.
- **Row 0:** Interaction & AI Decision Visualization.
- **Row 1:** Static Blue UI border.
- **Rows 2-7:** Active $7 \times 6$ game board.
- **Status Column:** Far right column (Index 7) manages UI framing and "Glow" effects.
---
## 🧠 Advanced AI Features
## 🧠 Advanced AI & Logic Features
### 1. Progressive Difficulty (Evolution Mode)
The AI search depth (Ply) increases as the board fills. This ensures the AI is fast in the opening and lethal in the endgame.
To keep the game challenging and the CPU efficient, the AI search depth (Ply) scales as the board fills.
- **Formula:** $DynamicPly = BasePly + \lfloor \frac{DiscsOnBoard}{7} \rfloor$
- **Benefit:** High-level tactical precision exactly when the game becomes critical.
- **Benefit:** The AI is "casual" in the opening but becomes a "Grandmaster" in the endgame when tactical precision is vital.
### 2. Strategic Blunder Injection
### 2. Intelligent Win Detection & Flashing
To avoid endless stalemate draws between high-level AIs, a "Blunder" logic is used.
The win-engine has been refactored to prevent "color ghosting."
- **Demo Mode:** Always active; 20% chance to make a suboptimal move.
- **Player Mode:** Toggleable via Web Portal to make the AI more "human."
- **Winner Locking:** The `scanBoard()` function returns the specific ID of the winner (1 for Yellow, 2 for Red).
- **Flashing Accuracy:** The final animation uses this ID to ensure the winning 4-in-a-row flashes in the **correct player's color**, regardless of whose turn it was when the game ended.
### 3. Alpha-Beta Pruning & Column Ordering
### 3. Smart Watchdog (Tiered Timeout)
The engine evaluates the center column first. This triggers pruning earlier in the search tree, skipping millions of unnecessary calculations and keeping the ESP32-C3 responsive.
The game respects your "thinking time" by using a tiered idle-timeout system:
- **Menu/Finished State:** Standard timeout (e.g., 60s).
- **Playing State:** **Double Timeout** (e.g., 120s). This gives human players more time to analyze complex boards before the game auto-resets to Demo Mode.
### 4. Strategic Blunder Injection
To ensure Demo Mode doesn't end in an infinite loop of draws, a 20% "Blunder Chance" is injected. This forces the AI to occasionally make a human-like mistake, creating openings for a definitive winner.
---
## 📖 Code Architecture Details
## 📖 Code Architecture & Modules
### 🔄 State Machine
The software cycles through states:
The core loop manages five distinct states:
- **MENU:** Select mode using the rotary encoder.
- **PLAYING:** Manages player turns and the gravity-acceleration drop animation.
- **DEMO:** Auto-starts after inactivity. Randomizes Ply (3-6) and enforces blunders to ensure definitive game results.
1. **MENU:** Mode selection and board reset.
2. **PLAYING:** Active turn-based logic with gravity-accelerated drop animations.
3. **FINISHED_WIN:** Locks the winner ID and flashes the winning segment.
4. **FINISHED_DRAW:** Blinks the entire board to signify a stalemate.
5. **DEMO:** Auto-plays with randomized difficulty (Ply 3-6) and mandatory blunder logic.
### 🎨 Rendering & Mapping
### 🌐 Web Administration Portal
The `getIdx(x, y)` function maps the 2D game board to the 1D NeoPixel array. The `updateThinkingVisuals()` function provides real-time feedback of the AI's internal search process by moving a pulsing disc across the top row.
Accessible via the **"Connect4-Config"** AP at `192.168.4.1`.
- **Base Ply:** Sets the starting difficulty level.
- **Brightness:** Global LED intensity (0-255).
- **Evolution Toggle:** Turn on/off the progressive difficulty scaling.
- **Blunder Toggle:** Allow the AI to make mistakes during Human-vs-AI matches.
---
## 🌐 Web Admin Portal
## 🛠 Installation
Connect to the **"Connect4-Config"** Access Point to adjust:
- **Base Ply:** Minimum search depth.
- **Brightness:** Global LED intensity.
- **Idle Timeout:** Inactivity period before Demo Mode.
- **Toggles:** Enable/Disable Blunders and Evolution Mode.
---
## 🛠 Setup & Installation
1. Install **PlatformIO**.
2. Add dependencies: `FastLED`, `Encoder`.
3. Set your WiFi Password in `platformio.ini`: `-D WIFI_PASSWORD=\"your_pass\"`.
4. Upload to ESP32-C3.
1. **Environment:** Use VS Code with the **PlatformIO** extension.
2. **Dependencies:** `FastLED`, `Encoder`, `Preferences`.
3. **Build Flag:** Define your WiFi password in `platformio.ini`: `-D WIFI_PASSWORD=\"your_password\"`.
4. **Flash:** Upload to your ESP32-C3 and enjoy the ultimate desktop Connect 4 experience.