Skip to content
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

Exercise Rock-Paper-Scissors #39

Open
hcchengithub opened this issue Jan 30, 2025 · 0 comments
Open

Exercise Rock-Paper-Scissors #39

hcchengithub opened this issue Jan 30, 2025 · 0 comments

Comments

@hcchengithub
Copy link

hcchengithub commented Jan 30, 2025

Thank you so much for the video "OpenAI's Swarm - a GAME CHANGER for AI Agents".
I have make an exercide that try to make agents to play Rock-Paper-Scissors and find it not so easy.
The agents are stupid, frequently forget to do the transfer to the next agent, and I have to say 'continue' all the time.
If you may find this exercise simple then a tiny direction would be very much appreciated.

from swarm import Agent
from swarm.repl import run_demo_loop
import random
from textwrap import dedent

decrypt = {
    1: "Rock", 4: "Rock", 8: "Rock",
    2: "Scissors", 3: "Scissors", 5: "Scissors",
    6: "Paper", 7: "Paper", 9: "Paper"
}

move1 = None

def player1move(move: int):
    """
    Player 1 makes his move secretly.

    Args:
        move (int): The move number (1-9)

    Returns:
        str: A message indicating that player 1 has made their move
    """
    global move1
    
    if move not in range(1, 10):
        raise ValueError("Invalid move. Please enter a number between 1 and 9")

    move1 = decrypt[move]
    return f"Player 1 Done"
    
def get_moves():
    """Host gets player 1's move"""
    return f"Player 1: {move1}"

# --- Handoff Functions ---
def transfer_to_player1():
    """Transfers turn to Player 1."""
    return player1_agent

def transfer_to_player2():
    """Transfers turn to Player 2."""
    return player2_agent

def transfer_back_to_host():
    """Transfer back to Host."""
    return host_agent

# --- Agents ---

# Host: Controls the game flow
host_agent = Agent(
    name="Host",
    instructions=(dedent("""
        You are the host of Rock-Paper-Scissors. 
        Start each round by transferring to Player 1 to make his move.
        Player 1 will transfer to player2 to make his move.
        When it transfered back to you from Player 2
        you get player1's secret moves, disclose and announce the winner.
    """)),
    functions=[transfer_to_player1, get_moves],
)

# Player 1: Cyclic Strategy (Scissors → Rock → Paper)
player1_agent = Agent(
    name="Player 1",
    instructions=(dedent("""
        You play Rock-Paper-Scissors in a cyclic order: 4 → 3 → 7 → repeat. 
        Call function player1move(code) to secretly make your move by the code. 
        Say only 'ok' and then transfer to player 2.
    """)),
    functions=[transfer_to_player2, player1move],
)

# Player 2: Predictive Strategy (Guesses opponent’s move based on history)
player2_agent = Agent(
    name="Player 2",
    instructions=(dedent("""
        You play Rock-Paper-Scissors, predicting Player 1's move based on 
        game history and trying to counter it. You study the game host's 
        announcements from the history to make your move. If your strategy 
        loses, try to change it! If there is no history, then try your luck 
        to make a choice! Then, call your function to transfer back to the 
        host.    
    """)),
    functions=[transfer_back_to_host],
)

# --- Run the Game ---
if __name__ == "__main__":
    client, response, messages = run_demo_loop(host_agent)
    # So far I have to type 'start' and 'continue' to make the game rolling.
    # 
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant