A simple demo showing a smart agent playing Tic Tac Toe against a human. The AI opponent uses GPT-4o to make strategic moves with persistent game state.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/agents/llms.txt
Use this file to discover all available pages before exploring further.
What it demonstrates
- Callable methods -
makeMoveandclearBoardas RPC endpoints - Persistent state - Game board survives restarts
- AI integration - Uses OpenAI’s GPT-4o for strategic gameplay
- State validation - Checks for valid moves and win conditions
- React integration - Real-time UI updates with
useAgent
Server Implementation
src/server.ts
How It Works
Check win condition
After each move,
checkWinner() scans all possible winning lines (rows, columns, diagonals).AI makes counter-move
If the game isn’t over, the agent calls GPT-4o with:
- Current board state
- Strategic priorities (win, block, center, fork, etc.)
- Request for optimal move coordinates
AI Strategy
The AI follows a strategic priority list:- Win immediately if possible
- Block opponent’s winning move
- Take center if open (strongest position)
- Create a fork (two ways to win)
- Block opponent’s fork
- Take a corner (second-best positions)
- Take an edge (last resort)
Game State
The board is a 3x3 grid stored in agent state:null= empty cell"X"= human player"O"= AI opponent
Win Detection
ThecheckWinner() method scans 8 possible winning lines:
"X" or "O"), or null if the game continues.
Running the Example
Play the game
Visit http://localhost:5174 and:
- Click any cell to make your move as X
- The AI (O) will respond immediately
- Continue until someone wins or it’s a draw
- Click “New Game” to reset
This example uses GPT-4o which requires an OpenAI API key. The AI makes strategic moves but isn’t unbeatable - try to beat it!
Client Integration
client.tsx
Extending This Example
Ideas for enhancements:- Difficulty levels - Adjust AI strategy based on difficulty
- Move history - Store and replay game moves
- Undo move - Let players take back their last move
- Multiplayer - Two humans playing over the network
- Tournament mode - Track wins/losses over multiple games
- Different AI models - Compare strategies from different LLMs
Related Examples
Counter
Simplest agent with callable methods
AI Chat
Full-featured AI chat with streaming
Workflows
Multi-step workflows with state
Agent API
Full Agent class documentation