-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tic Tac Toe example #2066
Tic Tac Toe example #2066
Changes from 30 commits
ac546ab
01501d6
3756cdb
63b20a0
2f9d1be
fecf056
fea085a
549facd
d8fb802
542e2c3
dae1598
97eea2f
a9be589
7bd4b69
9b1951b
e8f0276
26ecfeb
f580495
3efde2b
36c6a6a
740fef4
612d66f
19d7d7a
b70a26b
a23b499
0ad7f1f
4aee58c
ce0d1fc
7cf4a2b
70107fb
7643003
2650976
9f3fa0e
138fd8d
548220c
2f478dc
a05f2f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use the names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the changes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
""" | ||
Tic Tac Toe Battle | ||
--------------------------------- | ||
This example shows how to build a Tic Tac Toe game where two AI agents play against each other. | ||
The game features a master agent (referee) coordinating between two player agents using different | ||
language models. | ||
|
||
Usage Examples: | ||
--------------- | ||
1. Quick game with default settings: | ||
master_agent = get_tic_tac_toe() | ||
play_tic_tac_toe() | ||
|
||
2. Game with debug mode off: | ||
master_agent = get_tic_tac_toe(debug_mode=False) | ||
play_tic_tac_toe(debug_mode=False) | ||
|
||
The game integrates: | ||
- Multiple AI models (Claude, GPT-4, etc.) | ||
- Turn-based gameplay coordination | ||
- Move validation and game state management | ||
""" | ||
|
||
import sys | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
from agno.agent import Agent | ||
from agno.models.anthropic import Claude | ||
from agno.models.google import Gemini | ||
from agno.models.ollama import Ollama | ||
from agno.models.openai import OpenAIChat | ||
|
||
project_root = str(Path(__file__).parent.parent.parent.parent) | ||
if project_root not in sys.path: | ||
sys.path.append(project_root) | ||
|
||
|
||
@dataclass | ||
class ModelConfig: | ||
display_name: str | ||
model_id: str | ||
provider: str | ||
|
||
def get_model(self): | ||
if self.provider == "anthropic": | ||
return Claude(id=self.model_id) | ||
if self.provider == "openai": | ||
return OpenAIChat(id=self.model_id) | ||
if self.provider == "ollama": | ||
return Ollama(id=self.model_id) | ||
if self.provider == "google": | ||
return Gemini(id=self.model_id) | ||
raise ValueError(f"Invalid provider: {self.provider}") | ||
|
||
|
||
# Model configuration | ||
MODELS: Dict[str, ModelConfig] = { | ||
"claude": ModelConfig( | ||
display_name="Claude AI", | ||
model_id="claude-3-5-sonnet-20241022", | ||
provider="anthropic", | ||
), | ||
"gpt-4o": ModelConfig( | ||
display_name="GPT-4o", | ||
model_id="gpt-4o", | ||
provider="openai", | ||
), | ||
"gemini": ModelConfig( | ||
display_name="Gemini AI", | ||
model_id="gemini-2.0-flash-exp", | ||
provider="google", | ||
), | ||
"gpt-o3-mini": ModelConfig( | ||
display_name="GPT-o3-mini", | ||
model_id="o3-mini", | ||
provider="openai", | ||
), | ||
"llama": ModelConfig( | ||
display_name="Llama 3", | ||
model_id="llama3.2", | ||
provider="ollama", | ||
), | ||
"vision": ModelConfig( | ||
display_name="GPT-4 Vision", | ||
model_id="gpt-4o", | ||
provider="openai", | ||
), | ||
} | ||
|
||
# Default model assignments | ||
DEFAULT_MODELS = { | ||
"X": MODELS["gpt-o3-mini"], | ||
"O": MODELS["gemini"], | ||
"master": MODELS["gpt-4o"], | ||
"vision": MODELS["vision"], | ||
} | ||
|
||
|
||
def get_tic_tac_toe( | ||
model_x: ModelConfig = DEFAULT_MODELS["X"], | ||
model_o: ModelConfig = DEFAULT_MODELS["O"], | ||
model_master: ModelConfig = DEFAULT_MODELS["master"], | ||
debug_mode: bool = True, | ||
) -> Agent: | ||
""" | ||
Returns an instance of the Tic Tac Toe Master Agent that coordinates the game. | ||
|
||
Args: | ||
model_x: ModelConfig for player X | ||
model_o: ModelConfig for player O | ||
model_master: ModelConfig for the master agent | ||
debug_mode: Enable logging and debug features | ||
|
||
Returns: | ||
An instance of the configured Master Agent | ||
""" | ||
player1 = Agent( | ||
name="Player 1", | ||
role="""You are the X player in Tic Tac Toe. Your goal is to win by placing three X's in a row (horizontally, vertically, or diagonally). | ||
|
||
Game Rules: | ||
- You must make valid moves on a 3x3 grid using row (0-2) and column (0-2) coordinates | ||
- The top-left corner is (0,0), and bottom-right is (2,2) | ||
- You can only place X's in empty spaces marked by " " on the board | ||
- You will be given a list of valid moves - only choose from these moves | ||
- Respond with ONLY the row and column numbers for your move, e.g. "1 2" | ||
|
||
Strategy: | ||
- Study the board carefully and make strategic moves | ||
- Try to create winning opportunities while blocking your opponent | ||
- Pay attention to the valid moves list to avoid illegal moves | ||
""", | ||
model=model_x.get_model(), | ||
) | ||
|
||
player2 = Agent( | ||
name="Player 2", | ||
role="""You are the O player in Tic Tac Toe. Your goal is to win by placing three O's in a row (horizontally, vertically, or diagonally). | ||
|
||
Game Rules: | ||
- You must make valid moves on a 3x3 grid using row (0-2) and column (0-2) coordinates | ||
- The top-left corner is (0,0), and bottom-right is (2,2) | ||
- You can only place O's in empty spaces marked by " " on the board | ||
- You will be given a list of valid moves - only choose from these moves | ||
- Respond with ONLY the row and column numbers for your move, e.g. "1 2" | ||
|
||
Strategy: | ||
- Study the board carefully and make strategic moves to win | ||
- Block your opponent's winning opportunities | ||
- Pay attention to the valid moves list to avoid illegal moves | ||
""", | ||
model=model_o.get_model(), | ||
) | ||
|
||
master_agent = Agent( | ||
name="Master Agent", | ||
role="""You are the referee of the Tic Tac Toe game. Your responsibilities include: | ||
|
||
Game Management: | ||
1. Track and maintain the current board state | ||
2. Validate all moves before they are made | ||
3. Ensure moves are only made in empty spaces (marked by " ") | ||
4. Keep track of whose turn it is (X goes first) | ||
|
||
Move Validation: | ||
1. Check that moves are within the 3x3 grid (0-2 for rows and columns) | ||
2. Verify the chosen position is empty before allowing a move | ||
3. Maintain and provide the list of valid moves to players | ||
|
||
Game State Checking: | ||
1. Check for winning conditions after each move: | ||
- Three X's or O's in a row horizontally | ||
- Three X's or O's in a row vertically | ||
- Three X's or O's in a row diagonally | ||
2. Check for draw conditions: | ||
- Board is full with no winner | ||
|
||
Communication: | ||
1. Announce the current state of the board after each move | ||
2. Declare moves as valid or invalid | ||
3. Announce the winner or draw when game ends | ||
4. Provide clear feedback to players about their moves | ||
|
||
You will coordinate between the X and O players, ensuring fair play and proper game progression. | ||
""", | ||
model=model_master.get_model(), | ||
team=[player1, player2], | ||
show_tool_calls=True, | ||
debug_mode=debug_mode, | ||
markdown=True, | ||
) | ||
|
||
return master_agent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a README via cursor. Let me know if you are happy