Skip to content

Commit

Permalink
This works perfectly - Minimal game
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkbrnd committed Dec 15, 2023
1 parent b23488b commit 819b801
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions coup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
import sys

from autogen import GroupChat, GroupChatManager, UserProxyAgent, config_list_from_dotenv, AssistantAgent
Expand All @@ -19,14 +20,15 @@


def main():
# Create game handler with 5 players
# Create game handler with 3 players
handler = ResistanceCoupGameHandler(3)
print(f"First player is {handler.current_player}")

# Create AI players
agent_players = []
for ind, player in enumerate(handler.players):
agent_players.append(create_player_agent(player.name, [other_player.name for other_player in handler.players if other_player.name != player.name], player.cards, handler, config_list))
agent_players.append(create_player_agent(name=player.name, other_player_names=[other_player.name for other_player in handler.players if other_player.name != player.name],
cards=player.cards, strategy=player.strategy, handler=handler, config_list=config_list))

# Game master
game_master: AssistantAgent = create_game_master_agent(handler, config_list)
Expand Down
7 changes: 5 additions & 2 deletions src/ai/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create_game_master_agent(handler: ResistanceCoupGameHandler, config_list: li
return game_master


def create_player_agent(name: str, other_player_names: list[str], cards: list[Card],
def create_player_agent(name: str, other_player_names: list[str], cards: list[Card], strategy: str,
handler: ResistanceCoupGameHandler, config_list: list) -> AssistantAgent:
llm_config = {
"config_list": config_list,
Expand Down Expand Up @@ -114,7 +114,6 @@ def create_player_agent(name: str, other_player_names: list[str], cards: list[Ca
]
}


instructions = f"""Your name is {name} and you are a player in the game The Resistance: Coup.
You are playing against {", ".join(other_player_names)}.
Expand All @@ -131,6 +130,10 @@ def create_player_agent(name: str, other_player_names: list[str], cards: list[Ca
You also chit-chat with your opponent when you communicate an action to light up the mood.
You should ensure both you and your opponents are making valid actions. Also that everyone is only taking actions when it is their turn.
Your strategy should be to play {strategy}.
Don't hoard up coins, but rather try the assassinate or coup actions when you have a chance.
Do not apologize for making invalid actions.
Expand Down
7 changes: 5 additions & 2 deletions src/handler/game_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def __init__(self, number_of_players: int):

for i in range(number_of_players):
player_name = f"Player_{str(i + 1)}"
self._players[player_name] = Player(name=player_name)
strategy = random.choice(["conservative", "aggressive"])
self._players[player_name] = Player(name=player_name, strategy=strategy)
self._player_names.append(player_name)

self.initialize_game()
Expand All @@ -87,7 +88,9 @@ def get_game_state(self) -> dict:
players_str += f" - {player_name} with {len(player.cards)} cards and {player.coins} coins\n"

return {
"active_players": [player_name for player_name, player in self._players.items() if player.is_active],
"active_players": [{"name": player_name,
"coins": player.coins,
"cards": len(player.cards)} for player_name, player in self._players.items() if player.is_active],
"treasury_coin": self._treasury,
"next_player": self.current_player.name
}
Expand Down
1 change: 1 addition & 0 deletions src/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Player(BaseModel, ABC):
name: str
coins: int = 0
cards: List[Card] = []
strategy: str
is_active: bool = False

def __str__(self):
Expand Down

0 comments on commit 819b801

Please sign in to comment.