-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_loop.py
More file actions
513 lines (390 loc) · 16.3 KB
/
game_loop.py
File metadata and controls
513 lines (390 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""Main game loop for Avalon."""
import json
import os
from datetime import datetime
from pathlib import Path
from typing import Dict, List
from anthropic import Anthropic
from dotenv import load_dotenv
from game_engine import AvalonGame
from agent_interface import AgentInterface
# Load environment variables from .env file
load_dotenv()
class LLMAgent:
"""LLM agent using Anthropic's Claude API."""
def __init__(self, name: str, api_key: str = None):
self.name = name
# Get API key from parameter or environment variable
if api_key is None:
api_key = os.environ.get("ANTHROPIC_API_KEY")
if not api_key:
raise ValueError("ANTHROPIC_API_KEY must be provided or set in environment")
self.client = Anthropic(api_key=api_key)
# Using Claude 3 Haiku - the cheapest Anthropic model
self.model = "claude-3-haiku-20240307"
def get_response(self, prompt: str = None, content_blocks: list = None) -> str:
"""Get response from Claude API with optional prompt caching support.
Args:
prompt: Simple string prompt (legacy support)
content_blocks: List of content blocks with optional cache_control
"""
try:
# Use content blocks if provided, otherwise convert prompt to content blocks
if content_blocks:
message_content = content_blocks
elif prompt:
message_content = prompt
else:
raise ValueError("Must provide either prompt or content_blocks")
message = self.client.messages.create(
model=self.model,
max_tokens=1024,
messages=[
{"role": "user", "content": message_content}
]
)
response = message.content[0].text
return response
except Exception as e:
print(f"Error getting response from {self.name}: {e}")
# Return a safe default
return "I approve of this proposal. <VOTE>approve</VOTE>"
def run_discussion_round(
game: AvalonGame,
interface: AgentInterface,
agents: Dict[str, LLMAgent],
round_number: int,
num_turns: int = 3
):
"""Run a discussion round with all players speaking in order."""
# Write round header to conversation logs
with open(os.path.join(game.output_dir, "conversation_log.txt"), "a") as f:
f.write(f"\n<ROUND>{round_number}</ROUND>\n")
with open(os.path.join(game.output_dir, "full_conversation_log.txt"), "a") as f:
f.write(f"\n\n{'#'*60}\n")
f.write(f"# ROUND {round_number}\n")
f.write(f"{'#'*60}\n")
speaker_order = game.get_speaker_order()
# Each player speaks num_turns times
for turn in range(num_turns):
for speaker in speaker_order:
# Get role info for this player
role_info = game.get_player_role_info(speaker)
# Build prompt with caching support
content_blocks = interface.build_discussion_prompt(
player=speaker,
role=role_info["role"],
initial_knowledge=role_info["knows_about"],
round_number=round_number
)
# Get agent's response using cached content
agent = agents[speaker]
response = agent.get_response(content_blocks=content_blocks)
# Parse public message from response
public_message = interface.parse_discussion_message(response)
# Save full response (including private thoughts) to player's private file
interface.save_private_thoughts(speaker, round_number, response, phase="discussion")
# Save only public message to conversation log
interface.save_conversation_message(round_number, speaker, public_message)
# Save both private thoughts and public message to full conversation log
interface.save_full_conversation_message(round_number, speaker, response, public_message)
print(f"{speaker}: {public_message}")
def collect_votes(
game: AvalonGame,
interface: AgentInterface,
agents: Dict[str, LLMAgent],
round_number: int,
proposed_team: List[str]
) -> Dict[str, str]:
"""Collect private thoughts and votes from all players."""
votes = {}
print(f"\n{'='*60}")
print("COLLECTING VOTES...")
print(f"{'='*60}\n")
for player in game.players:
role_info = game.get_player_role_info(player)
# Build private thoughts prompt with caching
content_blocks = interface.build_private_thoughts_prompt(
player=player,
role=role_info["role"],
initial_knowledge=role_info["knows_about"],
round_number=round_number,
decision_type="vote"
)
# Get agent's private thoughts using cached content
agent = agents[player]
response = agent.get_response(content_blocks=content_blocks)
# Parse vote
vote = interface.parse_vote(response)
votes[player] = vote
# Save private thoughts
interface.save_private_thoughts(player, round_number, response, phase="vote")
print(f"{player} voted: {vote}")
return votes
def collect_quest_cards(
game: AvalonGame,
interface: AgentInterface,
agents: Dict[str, LLMAgent],
round_number: int,
team_members: List[str]
) -> Dict[str, str]:
"""Collect quest card decisions from team members."""
quest_cards = {}
print(f"\n{'='*60}")
print("COLLECTING QUEST CARDS...")
print(f"{'='*60}\n")
for player in team_members:
role_info = game.get_player_role_info(player)
# Build quest decision prompt with caching
content_blocks = interface.build_private_thoughts_prompt(
player=player,
role=role_info["role"],
initial_knowledge=role_info["knows_about"],
round_number=round_number,
decision_type="quest_card"
)
# Get agent's decision using cached content
agent = agents[player]
response = agent.get_response(content_blocks=content_blocks)
# Parse quest card
card = interface.parse_quest_card(response)
# Enforce good players must play success
if role_info["role"] not in ["Assassin", "Minion"]:
card = "success"
quest_cards[player] = card
# Save private thoughts
interface.save_private_thoughts(player, round_number, response, phase="quest")
print(f"{player} played: {card}")
return quest_cards
def run_quest_round(game: AvalonGame, interface: AgentInterface, agents: Dict[str, LLMAgent]):
"""Run a complete quest round."""
round_number = len(game.rounds) + 1
print(f"\n{'#'*60}")
print(f"# QUEST {game.current_quest} - ROUND {round_number}")
print(f"# Leader: {game.current_leader}")
print(f"# Team size required: {game.quest_requirements[game.current_quest]}")
print(f"{'#'*60}\n")
# Phase 1: Pre-proposal discussion
print("=== PRE-PROPOSAL DISCUSSION ===\n")
run_discussion_round(game, interface, agents, round_number, num_turns=1)
# Phase 2: Leader proposes team
print(f"\n=== TEAM PROPOSAL ===\n")
print(f"Leader {game.current_leader} proposes team...")
# TODO: In full implementation, get leader's proposal via LLM
# For now, use a simple placeholder
required_size = game.quest_requirements[game.current_quest]
proposed_team = game.seating_order[:required_size] # Placeholder
print(f"Proposed team: {proposed_team}\n")
# Phase 3: Voting
votes = collect_votes(game, interface, agents, round_number, proposed_team)
# Count votes
approve_count = sum(1 for v in votes.values() if v == "approve")
reject_count = sum(1 for v in votes.values() if v == "reject")
vote_result = "approved" if approve_count > reject_count else "rejected"
print(f"\nVote result: {approve_count} approve, {reject_count} reject → {vote_result.upper()}")
# Create round data
round_data = {
"round_number": round_number,
"quest_number": game.current_quest,
"team_size_required": required_size,
"leader": game.current_leader,
"proposed_team": proposed_team,
"votes": votes,
"vote_result": vote_result,
"consecutive_rejections": game.consecutive_rejections + 1 if vote_result == "rejected" else 0
}
if vote_result == "rejected":
game.consecutive_rejections += 1
# Check for auto-loss (5 rejections)
if game.consecutive_rejections >= 5:
print("\n5 consecutive rejections! Evil wins!")
return "evil_wins"
# Rotate leader and try again
game.rotate_leader()
round_data["quest_outcome"] = None
game.rounds.append(round_data)
game.update_public_log(round_data)
return "rejected"
# Reset consecutive rejections on approval
game.consecutive_rejections = 0
# Phase 4: Quest execution
print(f"\n=== QUEST EXECUTION ===\n")
quest_cards = collect_quest_cards(game, interface, agents, round_number, proposed_team)
# Count results
success_count = sum(1 for c in quest_cards.values() if c == "success")
fail_count = sum(1 for c in quest_cards.values() if c == "fail")
quest_result = "success" if fail_count == 0 else "fail"
print(f"\nQuest result: {success_count} success, {fail_count} fail → {quest_result.upper()}")
# Update quest score
if quest_result == "success":
game.quest_score["good"] += 1
else:
game.quest_score["evil"] += 1
# Add quest outcome to round data
round_data["quest_outcome"] = {
"success_cards": success_count,
"fail_cards": fail_count,
"result": quest_result
}
# Save full round data (with quest cards played by each player)
full_round_data = round_data.copy()
full_round_data["quest_cards_played"] = quest_cards
# Update logs
game.rounds.append(round_data)
game.update_public_log(round_data)
game.update_full_log(full_round_data)
# Move to next quest
game.current_quest += 1
game.rotate_leader()
# Check win conditions
if game.quest_score["good"] >= 3:
return "good_wins_quests"
elif game.quest_score["evil"] >= 3:
return "evil_wins"
return "continue"
def run_assassination_phase(
game: AvalonGame,
interface: AgentInterface,
agents: Dict[str, LLMAgent]
) -> str:
"""Run the assassination phase where the Assassin tries to identify Merlin."""
print(f"\n{'='*60}")
print("=== ASSASSINATION PHASE ===")
print(f"{'='*60}\n")
print("Good has won 3 quests!")
print("The Assassin gets one chance to identify and assassinate Merlin.")
print("If they guess correctly, evil wins!\n")
# Write assassination header to conversation logs
with open(os.path.join(game.output_dir, "conversation_log.txt"), "a") as f:
f.write(f"\n<ASSASSINATION>\n")
with open(os.path.join(game.output_dir, "full_conversation_log.txt"), "a") as f:
f.write(f"\n\n{'#'*60}\n")
f.write(f"# ASSASSINATION PHASE\n")
f.write(f"{'#'*60}\n")
# Phase 1: Final discussion about who might be Merlin
print("=== FINAL DISCUSSION - WHO IS MERLIN? ===\n")
speaker_order = game.get_speaker_order()
for speaker in speaker_order:
role_info = game.get_player_role_info(speaker)
# Build prompt for final discussion with caching
content_blocks = interface.build_discussion_prompt(
player=speaker,
role=role_info["role"],
initial_knowledge=role_info["knows_about"],
round_number="assassination",
phase="assassination"
)
# Modify the last block to add assassination-specific text
content_blocks[-1]["text"] = content_blocks[-1]["text"].replace(
"It is now your turn to speak.",
"FINAL DISCUSSION: Good has won 3 quests, but the Assassin will try to identify Merlin.\n\nIt is now your turn to speak."
)
# Get agent's response using cached content
agent = agents[speaker]
response = agent.get_response(content_blocks=content_blocks)
# Parse public message from response
public_message = interface.parse_discussion_message(response)
# Save full response to player's private file
interface.save_private_thoughts(speaker, "assassination", response, phase="discussion")
# Save only public message to conversation log
interface.save_conversation_message("assassination", speaker, public_message)
# Save both to full conversation log
interface.save_full_conversation_message("assassination", speaker, response, public_message)
print(f"{speaker}: {public_message}")
# Phase 2: Assassin makes their choice
print(f"\n{'='*60}")
print("THE ASSASSIN CHOOSES...")
print(f"{'='*60}\n")
# Find the Assassin
assassin = None
for player, role in game.roles.items():
if role == "Assassin":
assassin = player
break
if not assassin:
print("ERROR: No Assassin found!")
return "good_wins"
# Get Assassin's role info
role_info = game.get_player_role_info(assassin)
# Build assassination prompt with caching
content_blocks = interface.build_assassination_prompt(
assassin=assassin,
initial_knowledge=role_info["knows_about"],
players=game.players
)
# Get Assassin's decision using cached content
agent = agents[assassin]
response = agent.get_response(content_blocks=content_blocks)
# Parse assassination target
target = interface.parse_assassination_target(response)
# Save Assassin's reasoning
interface.save_private_thoughts(assassin, "assassination", response, phase="assassination_decision")
print(f"\nThe Assassin ({assassin}) has chosen to assassinate: {target}")
# Find who is actually Merlin
merlin = None
for player, role in game.roles.items():
if role == "Merlin":
merlin = player
break
print(f"\nMerlin was: {merlin}")
# Determine the result
if target == merlin:
print("\n🗡️ The Assassin correctly identified Merlin!")
print("EVIL WINS!")
return "evil_wins"
else:
print("\n✨ The Assassin failed to identify Merlin!")
print("GOOD WINS!")
return "good_wins"
def main():
"""Run a full game of Avalon."""
print("="*60)
print(" THE RESISTANCE: AVALON")
print("="*60)
# Create output directory for this game
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
output_dir = os.path.join("outputs", f"game-{timestamp}")
Path(output_dir).mkdir(parents=True, exist_ok=True)
print(f"\nGame outputs will be saved to: {output_dir}")
# Initialize game
game = AvalonGame(output_dir=output_dir)
interface = AgentInterface(output_dir=output_dir)
# Create agents for each player
agents = {player: LLMAgent(player) for player in game.players}
print(f"\nSeating order: {', '.join(game.seating_order)}")
print(f"Initial leader: {game.current_leader}")
print(f"Quest score: Good {game.quest_score['good']} - {game.quest_score['evil']} Evil\n")
# Main game loop
game_result = None
while game_result is None:
result = run_quest_round(game, interface, agents)
if result == "rejected":
print("\nTeam rejected. Moving to next leader...\n")
continue
elif result == "continue":
print(f"\nQuest score: Good {game.quest_score['good']} - {game.quest_score['evil']} Evil\n")
continue
else:
game_result = result
break
# Handle end game
print(f"\n{'='*60}")
if game_result == "good_wins_quests":
# Run assassination phase
final_result = run_assassination_phase(game, interface, agents)
game_result = final_result
elif game_result == "evil_wins":
print("EVIL WINS!")
# Print final result
if game_result == "good_wins":
print(f"\n{'='*60}")
print("FINAL RESULT: GOOD WINS!")
print(f"{'='*60}")
elif game_result == "evil_wins":
print(f"\n{'='*60}")
print("FINAL RESULT: EVIL WINS!")
print(f"{'='*60}")
print(f"\n{'='*60}\n")
print("Game complete! Check game logs for full details.")
if __name__ == "__main__":
main()