Skip to main content

Overview

Game-playing agents demonstrate advanced AI capabilities through strategic decision-making, adversarial reasoning, and multi-agent coordination. These agents use LLMs to analyze game states, evaluate positions, and make strategic moves in real-time competitive scenarios.
Game-playing agents showcase key AI concepts like adversarial search, position evaluation, move validation, and strategic planning - all fundamental to building intelligent autonomous systems.

Chess Playing Agents

Agent White vs Agent Black: Chess Game

An advanced chess game system where two AI agents play against each other using Autogen with robust move validation and game state management. Multi-Agent Architecture:

Player White

Role: Strategic decision makerPowered by: OpenAI GPT-4oResponsibilities:
  • Position evaluation
  • Opening strategy
  • Tactical planning
  • Endgame execution

Player Black

Role: Tactical opponentPowered by: OpenAI GPT-4oResponsibilities:
  • Counter-strategy
  • Defensive planning
  • Attack opportunities
  • Position control

Board Proxy

Role: Validation agentResponsibilities:
  • Move legality checking
  • Game state tracking
  • Rule enforcement
  • Win condition detection
System Architecture:
Game Initialization

Player White Agent
├─ Analyze current position
├─ Evaluate possible moves
├─ Select optimal move
└─ Propose move in algebraic notation

Board Proxy Agent
├─ Validate move legality
├─ Check for rule violations
├─ Update game state
├─ Detect check/checkmate/stalemate
└─ Approve or reject move

If Move Valid:

Player Black Agent
├─ Analyze new position
├─ Formulate response
├─ Select counter-move
└─ Propose move

Board Proxy Agent
└─ Validate and update

Continue until game end...
Features:
Robust Move Verification:
  • Checks all chess rules (castling, en passant, promotion)
  • Prevents illegal moves
  • Validates piece movement patterns
  • Ensures king safety
Real-time Monitoring:
  • Board state tracking after each move
  • Check and checkmate detection
  • Stalemate and draw conditions
  • Move history logging
Secure Progression:
  • Prevents rule violations
  • Handles edge cases
  • Enforces turn order
  • Game integrity maintenance
Setup and Run:
# Clone repository
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
cd advanced_ai_agents/autonomous_game_playing_agent_apps/ai_chess_agent

# Install dependencies
pip install -r requirements.txt

# Set OpenAI API key
export OPENAI_API_KEY='your-api-key-here'

# Run the chess game
streamlit run ai_chess_agent.py
Game Flow:
1

Initialize Game

  • Load Streamlit interface
  • Set up chess board (standard starting position)
  • Initialize both agents (White and Black)
  • Create Board Proxy for validation
2

White's Turn

  • Agent analyzes current position
  • Evaluates candidate moves
  • Selects best move using strategic reasoning
  • Submits move in algebraic notation (e.g., “e4”)
3

Move Validation

  • Board Proxy receives move proposal
  • Validates against chess rules
  • Checks for legality (piece can make that move)
  • Ensures move doesn’t leave king in check
  • Updates board state if valid
4

Black's Response

  • Agent analyzes new position after White’s move
  • Formulates counter-strategy
  • Selects response move
  • Submits for validation
5

Game Continuation

  • Cycle continues with move validation
  • Real-time board display updates
  • Move history tracked
  • Game ends on checkmate, stalemate, or draw
Example Game Sequence:
Move 1:
White: e4 (King's Pawn Opening)
├─ Board Proxy: Valid ✓
└─ Board updated

Black: e5 (King's Pawn Defense)
├─ Board Proxy: Valid ✓
└─ Board updated

Move 2:
White: Nf3 (Developing Knight)
├─ Board Proxy: Valid ✓
└─ Board updated

Black: Nc6 (Developing Knight)
├─ Board Proxy: Valid ✓
└─ Board updated

... game continues ...
Implementation Pattern:
import autogen
import chess

# Configure LLM
llm_config = {
    "config_list": [{
        "model": "gpt-4o",
        "api_key": openai_api_key
    }]
}

# Create player agents
player_white = autogen.AssistantAgent(
    name="Player_White",
    system_message="""
    You are a strategic chess player playing White.
    Analyze the position and make the best move.
    Use standard algebraic notation (e.g., e4, Nf3, O-O).
    """,
    llm_config=llm_config
)

player_black = autogen.AssistantAgent(
    name="Player_Black",
    system_message="""
    You are a tactical chess player playing Black.
    Respond to White's moves with sound strategy.
    Use standard algebraic notation.
    """,
    llm_config=llm_config
)

# Create validation agent
board_proxy = autogen.AssistantAgent(
    name="Board_Proxy",
    system_message="""
    You validate chess moves and maintain game state.
    Check move legality and update the board.
    Detect check, checkmate, and stalemate.
    """,
    llm_config=llm_config
)

# Initialize board
board = chess.Board()

# Game loop
while not board.is_game_over():
    # White's turn
    white_move = player_white.generate_move(board)
    if board_proxy.validate_move(white_move, board):
        board.push(white_move)
    
    # Black's turn
    if not board.is_game_over():
        black_move = player_black.generate_move(board)
        if board_proxy.validate_move(black_move, board):
            board.push(black_move)
Strategic Insights: The agents learn chess strategy through their training data, including opening principles, tactical patterns, and endgame techniques. Watch how they apply concepts like center control, piece development, and king safety!

Tic-Tac-Toe Agents

Agent X vs Agent O: Tic-Tac-Toe Game

An interactive Tic-Tac-Toe game where AI agents powered by different language models compete against each other, built on Agno Agent Framework. Multi-Model Support:

OpenAI Models

  • GPT-4o
  • GPT-o3-mini
  • Advanced reasoning
  • Strategic planning

Google Models

  • Gemini Pro
  • Gemini Flash
  • Fast responses
  • Efficient processing

Other Models

  • Claude (Anthropic)
  • Llama 3 (Groq)
  • Diverse strategies
  • Comparative analysis
Agent Roles:
Master Agent (Referee)
├─ Coordinates the game
├─ Validates moves
├─ Maintains game state
├─ Determines outcome
└─ Enforces rules

Player X Agent
├─ Analyzes board state
├─ Makes strategic X moves
├─ Follows game rules
└─ Responds to opponent

Player O Agent
├─ Analyzes board state
├─ Makes strategic O moves
├─ Follows game rules
└─ Responds to opponent
Features:
Real-time Updates:
  • Instant move visualization
  • Clear X and O markers
  • Highlighted winning combination
  • Turn indicator
Visual Elements:
  • 3x3 grid display
  • Color-coded players
  • Move animations
  • Game status display
Setup:
# Clone repository
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
cd advanced_ai_agents/autonomous_game_playing_agent_apps/ai_tic_tac_toe_agent

# Install dependencies
pip install -r requirements.txt
Configure API Keys: Create a .env file:
# Required for OpenAI models (gpt-4o, o3-mini)
OPENAI_API_KEY=your_actual_openai_api_key_here

# Optional - for additional models
ANTHROPIC_API_KEY=your_actual_anthropic_api_key_here  # For Claude
GOOGLE_API_KEY=your_actual_google_api_key_here        # For Gemini
GROQ_API_KEY=your_actual_groq_api_key_here           # For Groq models
Replace placeholder values with actual API keys. The app shows helpful error messages if required keys are missing.
Run the Game:
streamlit run app.py
Open localhost:8501 to view the game interface. Game Implementation:
from agno import Agent

# Create Master Agent (Referee)
master_agent = Agent(
    name="Game_Master",
    model=model_x,
    instructions="""
    You are the game master for Tic-Tac-Toe.
    Coordinate between players, validate moves,
    maintain game state, and determine winners.
    """
)

# Create Player X
player_x = Agent(
    name="Player_X",
    model=model_x,
    instructions="""
    You play as X in Tic-Tac-Toe.
    Analyze the board and make strategic moves.
    Try to win by getting three X's in a row.
    Block opponent's winning moves.
    """
)

# Create Player O
player_o = Agent(
    name="Player_O",
    model=model_o,
    instructions="""
    You play as O in Tic-Tac-Toe.
    Analyze the board and make strategic moves.
    Try to win by getting three O's in a row.
    Block opponent's winning moves.
    """
)

# Game loop
board = [[" " for _ in range(3)] for _ in range(3)]
current_player = player_x

while not is_game_over(board):
    # Current player makes move
    move = current_player.run(
        f"Current board state: {board}. Make your move."
    )
    
    # Master validates and applies move
    if master_agent.validate_move(move, board):
        apply_move(move, board)
        current_player = player_o if current_player == player_x else player_x
    
    # Check for win/draw
    result = master_agent.check_game_over(board)
Strategic Patterns:

Offensive Strategy

Winning Moves:
  • Center control (position 5)
  • Corner positions (1, 3, 7, 9)
  • Fork creation
  • Two-way threats

Defensive Strategy

Blocking Moves:
  • Detect opponent’s two-in-a-row
  • Block winning moves
  • Force opponent to defend
  • Create counter-threats

Opening Play

First Moves:
  • Center (optimal)
  • Corners (strong)
  • Edges (suboptimal)
  • Response to opponent’s center

Endgame

Final Positions:
  • Forced wins
  • Forced draws
  • Mistake exploitation
  • Optimal play
Model Comparison:
Model MatchupStrategy TypeWin RateNotes
GPT-4o vs GPT-4oBalanced50%Optimal play both sides
GPT-4o vs o3-miniStrategic vs TacticalVariesDifferent reasoning styles
Claude vs GPT-4oAnalytical~45%Methodical approach
Gemini vs LlamaFast vs RobustVariesSpeed vs consistency
Experiment with Models: Try different model combinations to see varied playing styles! GPT-4o tends to be aggressive, Claude is defensive, and o3-mini shows creative tactics.

Key Concepts in Game-Playing Agents

Minimax Algorithm Concept:
# LLMs approximate minimax reasoning
def evaluate_move(board, player):
    """
    LLM reasons about:
    1. Immediate winning moves
    2. Blocking opponent wins
    3. Strategic position control
    4. Long-term advantage
    """
    prompt = f"""
    Board state: {board}
    You are playing as {player}.
    
    Analyze:
    1. Can you win in one move?
    2. Can opponent win if you don't block?
    3. What move gives best position?
    
    Consider both your opportunities and opponent's threats.
    """
    return llm.generate(prompt)

Position Evaluation

Chess Position Assessment:
def evaluate_position(board):
    """
    LLM evaluates multiple factors:
    - Material count (piece values)
    - Piece activity and mobility
    - King safety
    - Pawn structure
    - Control of center
    - Tactical opportunities
    """
    evaluation = llm.analyze(board)
    return evaluation.score
Tic-Tac-Toe Position Values:
Board Positions (Priority):

5 (Center)     - Highest value
1, 3, 7, 9     - Corners (high value)
2, 4, 6, 8     - Edges (lower value)

Strategic Evaluation:
- Two in a row  = High priority (win/block)
- Fork potential = Medium priority
- Center control = Baseline advantage

Move Validation

Validation Agent Pattern:
class MoveValidator(Agent):
    def validate_move(self, move, game_state):
        """
        Multi-step validation:
        1. Parse move notation
        2. Check if move is legal
        3. Verify game rules
        4. Update game state
        5. Detect end conditions
        """
        if not self.is_valid_notation(move):
            return False, "Invalid notation"
        
        if not self.is_legal_move(move, game_state):
            return False, "Illegal move"
        
        if not self.follows_rules(move, game_state):
            return False, "Rule violation"
        
        new_state = self.apply_move(move, game_state)
        game_over = self.check_end_condition(new_state)
        
        return True, new_state, game_over

Use Cases and Applications

Educational Value

Learning Game Strategy

  • Understand strategic thinking
  • Observe tactical patterns
  • Learn from AI decisions
  • Compare different approaches

AI Development

  • Agent coordination
  • State management
  • Decision-making systems
  • Validation logic

Research Applications

  • Multi-agent Systems: Study agent interaction and coordination
  • Strategic Reasoning: Analyze LLM decision-making in adversarial contexts
  • Model Comparison: Benchmark different models on strategic tasks
  • Emergent Behavior: Observe strategies that emerge from agent interaction

Practical Extensions

# Extend to other games
class GameAgent:
    """Base class for game-playing agents"""
    
    def analyze_position(self, game_state):
        """Evaluate current position"""
        pass
    
    def generate_move(self, game_state):
        """Select best move"""
        pass
    
    def validate_move(self, move, game_state):
        """Check move legality"""
        pass

# Extend to new games:
# - Checkers
# - Go (simplified)
# - Connect Four
# - Poker (with hidden information)
# - Stratego

Implementation Tips

Prompt Engineering for Games

# Effective game agent prompts
system_message = """
You are playing {game_name} as {player}.

RULES:
{game_rules}

STRATEGY:
1. Analyze the current position
2. Identify immediate threats
3. Look for winning opportunities
4. Consider long-term advantage
5. Make the best move

FORMAT:
Provide your move in {notation_format}.
Explain your reasoning briefly.

CURRENT BOARD:
{board_state}

YOUR TURN: Make your move.
"""

State Management

import streamlit as st

# Use session state for game persistence
if 'game_state' not in st.session_state:
    st.session_state.game_state = initialize_game()
    st.session_state.move_history = []
    st.session_state.current_player = "X"

# Update state after each move
def make_move(move):
    st.session_state.game_state.apply(move)
    st.session_state.move_history.append(move)
    st.session_state.current_player = switch_player()

Performance Optimization

# Cache position evaluations
@st.cache_data
def evaluate_position(board_fen):
    return llm.analyze(board_fen)

# Async move generation
async def get_moves_parallel():
    moves = await asyncio.gather(
        agent.generate_move_async(board),
        agent.evaluate_position_async(board)
    )
    return moves

# Timeout protection
import signal

def timeout_handler(signum, frame):
    raise TimeoutError("Move generation timeout")

signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(30)  # 30 second timeout

Future Enhancements

More Games

  • Backgammon
  • Poker (incomplete information)
  • Go (simplified versions)
  • Strategy games

Advanced Features

  • Opening book integration
  • Endgame tablebase
  • Position database
  • Training from games

Analysis Tools

  • Move quality analysis
  • Blunder detection
  • Strategic patterns
  • Performance metrics

Multiplayer

  • Human vs AI
  • Tournament mode
  • ELO rating system
  • Leaderboards

Next Steps

Build Your Own

Use these examples as templates to create agents for other games

Multi-Agent Teams

Learn coordination patterns from game agents

Advanced Agents

Explore sophisticated reasoning agents

Overview

Return to agents overview

Build docs developers (and LLMs) love