CoolFace
Apppublic

ShubhanshuBansod/connect-four-ai

sourceHugging Facecc-by-nc-sa-4.0updated 11mo agoView on Hugging Face
0likes
App README

๐ŸŽฎ Connect Four AI with Explainability

A fully interpretable Connect Four game AI powered by Minimax + Alpha-Beta Pruning with LLM-generated natural language explanations. Play against an intelligent opponent that explains its strategic decisions in real-time.


๐ŸŒŸ Features

โœ… Intelligent AI Opponent

  • โ€”Minimax algorithm with alpha-beta pruning
  • โ€”Adjustable difficulty (depth 3-10)
  • โ€”Real-time algorithm statistics (nodes explored, pruning efficiency)

โœ… Explainability Layer

  • โ€”LLM-powered move explanations in natural language
  • โ€”Strategic threat analysis
  • โ€”Human-friendly decision insights
  • โ€”No technical jargonโ€”easy to understand

โœ… Interactive Web Interface

  • โ€”Play directly in your browser
  • โ€”Real-time board updates
  • โ€”Game controls (New Game, Undo)
  • โ€”Mobile-friendly design

โœ… Educational Value

  • โ€”Learn classical game theory (Minimax, Alpha-Beta Pruning)
  • โ€”Understand neuro-symbolic AI (classical algorithms + LLM)
  • โ€”See interpretable AI in action

๐ŸŽฏ How It Works

1. Game Logic (connect_four_game.py)

  • โ€”6ร—7 board state management
  • โ€”Move validation and winner detection
  • โ€”Threat analysis (3-in-a-row, center control)

2. AI Decision Making (minimax_ai.py)

  • โ€”Minimax algorithm explores possible moves
  • โ€”Alpha-beta pruning eliminates unnecessary branches
  • โ€”Heuristic board evaluation (threats, board control)
  • โ€”Returns move score and algorithm statistics

3. Natural Language Explanations (llm_explanation.py)

  • โ€”Converts algorithm output to human-readable insights
  • โ€”Uses HuggingFace Inference API (Mistral-7B LLM)
  • โ€”Graceful fallback if API unavailable

4. Interactive UI (app.py)

  • โ€”Built with Streamlit
  • โ€”Real-time game state display
  • โ€”Move selection dropdown
  • โ€”Algorithm statistics & explanations

๐Ÿš€ Quick Start

Prerequisites

  • โ€”HuggingFace account (free at https://huggingface.co)
  • โ€”HuggingFace API token (get it here)

Setup (Already Running on Spaces!)

This Space is ready to use! Just:

  1. 1.Play! Select a column (0-6) and click "Play"
  2. 2.Watch AI think - See the AI analyze and make its move
  3. 3.Read explanations - Understand why it chose that move
  4. 4.Adjust difficulty - Use the sidebar slider to change AI strength (3-10)
  5. 5.New game - Click "New Game" to restart

๐ŸŽฎ How to Play

  1. 1.Select a Column: Choose where to drop your piece (0-6, left to right)
  2. 2.AI Responds: AI analyzes and plays its move
  3. 3.See Explanations: Read why AI chose that move
  4. 4.Repeat: Continue until someone wins or board is full
  5. 5.Adjust Difficulty: Use the slider to change AI strength (3-10)

Game Rules

  • โ€”Standard Connect Four: 6 rows ร— 7 columns
  • โ€”First to get 4 pieces in a row (horizontal, vertical, or diagonal) wins
  • โ€”If board fills with no winner = Draw

๐Ÿ“Š Understanding the Algorithm

Minimax Algorithm

  • โ€”Explores all possible moves (up to search depth)
  • โ€”Assigns scores: +1000 for wins, -1000 for losses, 0 for neutral
  • โ€”AI chooses move with highest score

Alpha-Beta Pruning

  • โ€”Eliminates unnecessary branches during search
  • โ€”Typically reduces nodes explored by 80-90%
  • โ€”Makes deep searches feasible in real-time

Heuristics

  • โ€”Threat Detection: 3-in-a-row patterns
  • โ€”Center Control: Pieces in center are valuable
  • โ€”Piece Count: More pieces = better position

Statistics Displayed

  • โ€”Nodes Explored: Total positions analyzed
  • โ€”Nodes Pruned: Branches eliminated by alpha-beta
  • โ€”Pruning Efficiency: % of branches skipped
  • โ€”Search Depth: Moves ahead analyzed
  • โ€”Response Time: How long AI took to decide

๐Ÿค– LLM Explanations

This Space uses HuggingFace Inference API with Mistral-7B to generate explanations.

What Explanations Include

  • โ€”Strategic Insight: Why this move is strong
  • โ€”Threat Analysis: What patterns it creates/blocks
  • โ€”Board Evaluation: How good the position is
  • โ€”Human Language: No technical jargon

Rate Limiting

  • โ€”Free tier: ~1000 API calls/day (~100-200 game moves)
  • โ€”If limit hit: Automatic fallback explanations (game still works!)
  • โ€”Reset: Resets every 24 hours

โš™๏ธ Configuration

Adjust AI Difficulty

Use the sidebar slider:

  • โ€”3-4: Easy (quick decisions, basic strategy)
  • โ€”5-6: Medium (balanced thinking, ~400ms)
  • โ€”7-8: Hard (deep analysis, ~800ms)
  • โ€”9-10: Very Hard (exhaustive search, 1-2 seconds)

๐Ÿ”ง Troubleshooting

"HuggingFace Token Not Found"

  • โ€”Contact Space owner to verify token is set
  • โ€”Token should be added to Spaces Secrets (HF_TOKEN)

"Rate limit exceeded"

  • โ€”Wait 24 hours for limit reset (free tier)
  • โ€”Or upgrade to HF Pro for higher limits

Slow responses

  • โ€”Reduce search depth using the sidebar slider
  • โ€”This makes AI think faster

App won't load

  • โ€”Refresh page
  • โ€”Clear browser cache
  • โ€”Try a different browser

๐Ÿ“ˆ Performance

Response Times

  • โ€”Move Decision: 300-800ms (depends on difficulty)
  • โ€”Explanation: 2-5 seconds (HuggingFace API)
  • โ€”Total Response: 3-6 seconds
  • โ€”Uptime: 24/7 (always available on Spaces)

๐ŸŽ“ Learning Outcomes

Playing with this AI, you'll understand:

โœ… Classical AI Algorithms

  • โ€”Minimax decision-making
  • โ€”Alpha-beta pruning optimization
  • โ€”Game tree search strategies

โœ… Modern AI Explainability

  • โ€”Neuro-symbolic AI (classical + LLM)
  • โ€”Natural language generation for AI decisions
  • โ€”Bridging the gap between algorithms and humans

โœ… Game Theory Concepts

  • โ€”Perfect information games
  • โ€”Heuristic evaluation functions
  • โ€”Strategic thinking and threat detection

๐Ÿ“ File Structure

connect-four-ai/
โ”œโ”€โ”€ app.py                    # Streamlit UI (main entry point)
โ”œโ”€โ”€ connect_four_game.py      # Game engine & logic
โ”œโ”€โ”€ minimax_ai.py             # AI decision-making algorithm
โ”œโ”€โ”€ llm_explanation.py        # Move explanations (HF API)
โ”œโ”€โ”€ requirements.txt          # Dependencies
โ”œโ”€โ”€ .streamlit/
โ”‚   โ””โ”€โ”€ config.toml           # Streamlit theme config
โ””โ”€โ”€ README.md                 # This file

๐Ÿ“ License

This project is licensed under CC BY-NC-SA 4.0 (Creative Commons Attribution-NonCommercial-ShareAlike 4.0)

You are free to:

  • โ€”โœ… Use this project for educational purposes
  • โ€”โœ… Modify and adapt the code
  • โ€”โœ… Share derivatives under same license

With conditions:

  • โ€”โš ๏ธ Give credit to original author
  • โ€”โš ๏ธ Do not use commercially (without permission)
  • โ€”โš ๏ธ Share modifications under same CC BY-NC-SA 4.0 license

For details: https://creativecommons.org/licenses/by-nc-sa/4.0/


๐Ÿ™Œ Credits & Acknowledgments

  • โ€”Minimax Algorithm: Classical computer science (Shannon, 1950s)
  • โ€”Alpha-Beta Pruning: Optimization technique (Knuth & Moore, 1970s)
  • โ€”Streamlit: Web UI framework (Streamlit Inc.)
  • โ€”HuggingFace: LLM inference & Spaces hosting
  • โ€”Mistral AI: Language model provider

๐Ÿ“š References & Further Reading

Algorithms

  • โ€”Minimax: https://en.wikipedia.org/wiki/Minimax
  • โ€”Alpha-Beta Pruning: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning
  • โ€”Game Theory: https://en.wikipedia.org/wiki/Combinatorialgametheory

AI Explainability

  • โ€”Neuro-Symbolic AI: https://arxiv.org/abs/2106.08561
  • โ€”Interpretable ML: https://christophm.github.io/interpretable-ml-book/

Tools & Frameworks

  • โ€”Streamlit: https://streamlit.io
  • โ€”HuggingFace: https://huggingface.co
  • โ€”HuggingFace Spaces: https://huggingface.co/docs/hub/spaces

๐ŸŽ‰ Have Fun!

This project demonstrates:

  • โ€”โœ… Classical AI algorithms (Minimax, Alpha-Beta Pruning)
  • โ€”โœ… Modern AI interpretability (LLM explanations)
  • โ€”โœ… Full-stack web development (Python + Streamlit)
  • โ€”โœ… Cloud deployment (HuggingFace Spaces)

Start playing and learning today! ๐Ÿš€


Last Updated: November 2025 License: CC BY-NC-SA 4.0 Python Version: 3.9+ Framework: Streamlit 1.28.1+