Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
722 changes: 722 additions & 0 deletions 2025-08-03.json

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Development commands

### Running the application
```bash
go run main.go # Run with default settings (3x3 board, 2 players, 20 games)
go run main.go -r 5 -p 1 -g 10 # Run with 5x5 board, 1 human player, 10 games
go run main.go --filename custom_machine.json # Use custom machine learning file
go run main.go --logfile custom_log.json # Use custom log file
```

### Visualization modes
```bash
go run main.go -g 10 -p 0 -v # Text summary with statistics (works everywhere)
go run main.go -g 5 -p 0 -i # Interactive TUI with auto-play (requires compatible terminal)
go run main.go -g 1 -p 0 -R # Step-by-step game replay
go run main.go -g 3 -p 0 -v -R # Combined text summary and replay
```

**Important for TUI mode (`-i`)**: Must be run as compiled binary directly in terminal (Terminal.app, iTerm2, Ghostty), NOT through IDEs or Claude Code:
```bash
go build
./hexapawn-go -g 5 -p 0 -i
```

### Testing
```bash
go test ./... # Run all tests
go test ./hexapawn # Run tests for hexapawn package only
go test -v ./hexapawn # Run tests with verbose output
go test -run TestNewBoard ./hexapawn # Run specific test
```

### Building
```bash
go build # Build executable in current directory
go build -o hexapawn-game # Build with custom executable name
```

### Code quality
```bash
go fmt ./... # Format all Go code
go vet ./... # Run Go static analysis
go mod tidy # Clean up module dependencies
```

## Architecture overview

### Core components

**Game engine (`hexapawn/hexapawn.go`)**
- `Board`: Represents the game board with NxN grid (default 3x3)
- `Game`: Manages game state, player turns, and win conditions
- `Position`: Represents board state with available moves for machine learning
- Move validation and execution system

**Machine learning (`hexapawn/machine.go`)**
- `Machine`: Implements reinforcement learning that improves by removing losing moves
- `Step`: Represents decision points in the game tree with available moves per board state
- Training system that learns from losses by eliminating bad moves from future games
- Persistence to JSON files for learning continuity

**Game types (`hexapawn/types.go`)**
- Core data structures for board representation, moves, and game state
- JSON serialization support for machine learning persistence
- Error handling types in `hexapawn/errors.go`

**Visualization system (`hexapawn/ui.go`)**
- `GameViewer`: Interactive TUI using Bubbletea framework
- `ShowGamesText()`: Text-based summary with statistics
- `ShowGameReplay()`: Step-by-step game replay functionality
- TTY compatibility checking for robust cross-environment support

### Game mechanics

**Board representation**: String format like "WWW...BBB" where W=white pawns, B=black pawns, .=empty
**Move notation**: Algebraic notation like "a1-b2" (from square to square)
**Win conditions**:
1. Advance pawn to opposite end
2. Capture all enemy pieces
3. Block opponent from moving

### Machine learning approach

The AI uses a simplified reinforcement learning approach:
1. Generates all possible game positions and moves during initialization
2. Plays games by randomly selecting from available moves
3. After losing, removes the losing move from that board position
4. Over time, eliminates bad moves to improve play quality

### File structure

- `main.go`: CLI interface and application entry point
- `hexapawn/`: Core game logic package
- `hexapawn.go`: Game engine and board logic
- `machine.go`: Machine learning implementation
- `types.go`: Core data structures
- `ui.go`: Visualization and TUI components
- `conversions.go`: String/board conversion utilities
- `errors.go`: Custom error types
- `machine.json`: Default machine learning state persistence
- `hexapawn_log.json`: Game logging in JSON format
- `notes.md`: Development notes and game analysis
- `SESSION_SUMMARY.md`: Implementation session documentation

## Visualization features

The project includes multiple visualization modes to demonstrate machine learning progress:

### Interactive TUI (`-i` flag)
- Full-screen terminal interface with Bubbletea framework
- Real-time game replay with auto-play functionality
- Controls: ←→ (moves), ↑↓ (games), Space (play/pause), r (reset), e (end), q (quit)
- Requires direct terminal execution (not through IDEs)

### Text summary (`-v` flag)
- Statistics showing win percentages and learning progress
- Recent games with move sequences and final board states
- Works in all environments including IDEs and scripts

### Step-by-step replay (`-R` flag)
- Manual advancement through moves of the most recent game
- Interactive demonstration of auto-play functionality
- Fallback when TUI is not available

## Dependencies

- `github.com/spf13/pflag`: Command-line flag parsing
- `github.com/charmbracelet/bubbletea`: TUI framework
- `github.com/charmbracelet/lipgloss`: Terminal styling
177 changes: 177 additions & 0 deletions SESSION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Hexapawn Visualization Implementation Session Summary

## Overview
This session focused on implementing user-facing visualization features for the hexapawn-go machine learning game project. The work involved creating both text-based and interactive Terminal User Interface (TUI) visualizations to help users understand the machine learning progress and replay games.

## Issues Addressed

### 1. Missing Win Statistics Display ✅ SOLVED
**Problem**: Win statistics were being displayed at the top of the output but getting scrolled out of view in the terminal.

**Root Cause**: Statistics were shown before the game details, causing them to scroll off-screen with long output.

**Solution**: Moved statistics to the end of the output with enhanced formatting:
```
=== OVERALL STATISTICS ===
Total games: 260
White wins: 21 (8.1%)
Black wins: 239 (91.9%)
Machine learning progress: Black is winning 91.9% of games!
```

### 2. Non-Functional Space Key Auto-Play ✅ SOLVED
**Problem**: Space key wasn't working to toggle play/pause in the interactive TUI mode.

**Root Cause**: TTY (terminal device) access limitations in certain execution environments prevented Bubbletea TUI framework from initializing properly.

**Debugging Process**:
- Initial assumption: Key handling logic was incorrect
- Discovered: TUI framework couldn't access `/dev/tty` in some environments
- Key finding: `open /dev/tty: device not configured` error
- Solution: Added TTY availability checking and clear user guidance

**Final Solution**:
- Added proper TTY compatibility checking
- Implemented graceful fallback to text mode when TTY unavailable
- Fixed key handling to use `" "` for space key detection
- Added clear instructions for users on how to run in compatible environments

## Implementation Details

### New Features Added

#### 1. Interactive TUI Game Viewer (`-i` flag)
- **Full-screen terminal interface** with styled board display
- **Navigation controls**:
- `←→` (h/l): Navigate through moves
- `↑↓` (k/j): Switch between games
- `Space`: Toggle auto-play (plays moves every 1.2 seconds)
- `r`: Reset to start of game
- `e`: Jump to end of game
- `q`: Quit
- **Visual board representation** with colored pieces
- **Real-time game state display** showing current game, move, and play status

#### 2. Enhanced Text Visualization (`-v` flag)
- **Statistics at bottom** for better visibility
- **Recent games summary** (last 5 games with details)
- **Final board states** for each recent game
- **Machine learning progress indicator**

#### 3. Step-by-Step Replay (`-R` flag)
- **Interactive replay** of the most recent game
- **Manual advancement** through moves (press Enter)
- **Move-by-move board display** with player indicators
- **Fallback demonstration** of auto-play functionality

### Technical Architecture

#### TUI Framework
- **Bubbletea v1.3.6**: Modern Go TUI framework based on The Elm Architecture
- **Lipgloss**: Styling and layout for attractive terminal display
- **Event-driven model**: Update/View pattern with command handling

#### Key Components
- `GameViewer`: Main TUI model managing game state and display
- `ShowGamesText()`: Text-based fallback with statistics
- `ShowGameReplay()`: Interactive step-by-step replay
- `RunGameViewerTUI()`: TUI initialization with compatibility checking

### Environment Compatibility

#### Working Environments ✅
- **macOS**: Terminal.app, iTerm2, Ghostty with direct execution
- **Linux**: Most standard terminal emulators
- **Direct binary execution**: `./hexapawn-go -i`

#### Non-Compatible Environments ❌
- **IDE integrated terminals** (limited TTY access)
- **Script execution contexts**
- **CI/CD environments**
- **Containerized environments** without TTY
- **Claude Code execution context**

## Debugging Insights

### Key Discoveries
1. **TTY Access is Critical**: Bubbletea requires direct TTY access, not available in all environments
2. **Environment Detection**: Added `/dev/tty` accessibility check for better user experience
3. **Graceful Degradation**: Multiple fallback modes ensure functionality across environments
4. **Key Handling**: Space key detection required specific string matching approach

### Error Patterns Identified
- `could not open a new TTY: open /dev/tty: device not configured`
- `all TUI modes failed` → indicates environment limitation, not code bug

## Command Line Interface

### New Flags Added
- `-v, --visualize`: Show text summary with statistics
- `-i, --interactive`: Launch TUI game viewer (TTY required)
- `-R, --replay`: Step-by-step replay of most recent game

### Usage Examples
```bash
# Text summary (works everywhere)
./hexapawn-go -g 10 -p 0 -v

# Interactive TUI (compatible terminals)
./hexapawn-go -g 5 -p 0 -i

# Step-by-step replay
./hexapawn-go -g 1 -p 0 -R

# Combined modes
./hexapawn-go -g 3 -p 0 -v -R
```

## Machine Learning Visualization Impact

The implementation successfully demonstrates Martin Gardner's original matchbox learning concept:
- **Clear learning progression**: Black win percentage increases over time
- **Visual game evolution**: Users can see how move choices improve
- **Educational value**: Step-by-step replay helps understand strategy development

### Typical Results Observed
- **Initial games**: More balanced outcomes
- **After training**: Black consistently wins 90%+ of games
- **Learning evidence**: Fewer available moves for White over time

## Files Modified

### Core Implementation
- `hexapawn/ui.go`: New TUI implementation with Bubbletea
- `main.go`: Added visualization flags and integration
- `go.mod`: Added Bubbletea and Lipgloss dependencies

### Documentation
- `CLAUDE.md`: Updated with visualization commands
- `SESSION_SUMMARY.md`: This comprehensive summary

## Future Enhancements Identified

### Potential Improvements
1. **Move analysis**: Highlight losing moves that get removed
2. **Learning metrics**: Track win rate progression over time
3. **Export functionality**: Save games to different formats
4. **Multiple difficulty levels**: Different AI strategies
5. **Web interface**: Browser-based visualization for broader accessibility

### Technical Debt
- TUI initialization could be more robust across different terminal types
- Error handling could provide more specific troubleshooting guidance

## Testing Outcomes

### Successful Validation
✅ **Statistics display**: Clearly visible at output end
✅ **Auto-play functionality**: Space key toggles play/pause correctly
✅ **Cross-platform compatibility**: Works in iTerm2 and Ghostty on macOS
✅ **Fallback behavior**: Graceful degradation to text mode when needed
✅ **Educational value**: Learning progression clearly demonstrated

## Conclusion

The visualization implementation successfully transforms the command-line hexapawn game into an engaging, educational tool that clearly demonstrates machine learning principles. The multi-modal approach (TUI, text, replay) ensures accessibility across different environments while maintaining the educational integrity of Martin Gardner's original concept.

The debugging process revealed important insights about terminal compatibility and TUI framework limitations, leading to a robust solution with proper environment detection and graceful fallbacks.
Loading