more functioning code
This commit is contained in:
parent
9e4df046b8
commit
68f057f1b8
2 changed files with 55 additions and 28 deletions
Binary file not shown.
|
|
@ -57,18 +57,39 @@ dealer_ai = ConversableAgent(
|
|||
"dealer_ai",
|
||||
system_message="""
|
||||
You are the Dealer AI in a game of Blackjack. Follow these rules:
|
||||
1. Start with two cards, one face up and one face down.
|
||||
2. After the player finishes their turn, reveal your hidden card.
|
||||
3. Hit until your hand value is at least 17.
|
||||
4. If your hand value exceeds 21, you bust.
|
||||
5. Do not make decisions for the player; only manage your own hand.
|
||||
- You will receive your current hand and the player's final hand value.
|
||||
- Decide whether to 'Hit' or 'Stand' based on the rules of Blackjack.
|
||||
- You must hit until your hand value is at least 17.
|
||||
- If your hand value exceeds 21, you bust.
|
||||
- Respond with only 'H' for Hit or 'S' for Stand.
|
||||
""",
|
||||
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.2, "api_key": OPENAI_API_KEY}]},
|
||||
human_input_mode="NEVER",
|
||||
)
|
||||
|
||||
# Define the Judge AI using AutoGen
|
||||
judge_ai = ConversableAgent(
|
||||
"judge_ai",
|
||||
system_message="""
|
||||
You are the Judge AI in a game of Blackjack. You will be provided with the player's final hand and the dealer's final hand.
|
||||
Your job is to determine the winner based on the rules of Blackjack:
|
||||
|
||||
Rules:
|
||||
- The player with the hand value closest to 21 without exceeding it wins.
|
||||
- If both players have the same hand value, it's a tie.
|
||||
- If both players bust (exceed 21), the dealer wins.
|
||||
|
||||
Respond with:
|
||||
- "Player" if the player wins.
|
||||
- "Dealer" if the dealer wins.
|
||||
- "Tie" if it's a tie.
|
||||
""",
|
||||
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.2, "api_key": OPENAI_API_KEY}]},
|
||||
human_input_mode="NEVER",
|
||||
)
|
||||
|
||||
# Function to play a game of Blackjack
|
||||
def play_blackjack(player_ai, dealer_ai, max_rounds=5):
|
||||
def play_blackjack(player_ai, dealer_ai, judge_ai, max_rounds=5):
|
||||
for round_number in range(1, max_rounds + 1):
|
||||
print(f"\n--- Round {round_number} ---")
|
||||
random.shuffle(deck)
|
||||
|
|
@ -84,13 +105,12 @@ def play_blackjack(player_ai, dealer_ai, max_rounds=5):
|
|||
|
||||
# Player AI decides to Hit or Stand
|
||||
player_ai_message = f"Your current hand is {player_hand} (Value: {player_value}). Dealer's upcard is {dealer_upcard}. Do you want to Hit or Stand? (H/S)"
|
||||
response = player_ai.initiate_chat(
|
||||
player_decision = player_ai.initiate_chat(
|
||||
dealer_ai,
|
||||
message=player_ai_message,
|
||||
max_turns=1,
|
||||
)
|
||||
# Extract the Player AI's decision
|
||||
player_decision = response
|
||||
if player_decision not in ['H', 'S']:
|
||||
# Fallback decision if response is invalid
|
||||
player_decision = 'S'
|
||||
|
|
@ -110,27 +130,34 @@ def play_blackjack(player_ai, dealer_ai, max_rounds=5):
|
|||
dealer_value = calculate_hand_value(dealer_hand)
|
||||
print(f"Dealer reveals hidden card: {dealer_hand[1]}")
|
||||
print(f"Dealer's Hand: {dealer_hand} (Value: {dealer_value})")
|
||||
while dealer_value < 17:
|
||||
dealer_hand.append(deal_card(deck))
|
||||
dealer_value = calculate_hand_value(dealer_hand)
|
||||
print(f"Dealer draws: {dealer_hand[-1]}")
|
||||
if dealer_value > 21:
|
||||
print(f"Dealer Busts! Hand: {dealer_hand} (Value: {dealer_value})")
|
||||
while True:
|
||||
# Dealer AI decides to Hit or Stand
|
||||
dealer_ai_message = f"Your current hand is {dealer_hand} (Value: {dealer_value}). The player's final hand value is {player_value}. Do you want to Hit or Stand? (H/S)"
|
||||
dealer_decision = dealer_ai.initiate_chat(
|
||||
player_ai, # Communicating with the player AI
|
||||
message=dealer_ai_message,
|
||||
max_turns=1,
|
||||
)
|
||||
# Extract the Dealer AI's decision
|
||||
if dealer_decision not in ['H', 'S']:
|
||||
# Fallback decision if response is invalid
|
||||
dealer_decision = 'S'
|
||||
|
||||
if dealer_decision == 'H':
|
||||
dealer_hand.append(deal_card(deck))
|
||||
dealer_value = calculate_hand_value(dealer_hand)
|
||||
print(f"Dealer draws: {dealer_hand[-1]}")
|
||||
if dealer_value > 21:
|
||||
print(f"Dealer Busts! Hand: {dealer_hand} (Value: {dealer_value})")
|
||||
break
|
||||
elif dealer_decision == 'S':
|
||||
print(f"Dealer stands with hand: {dealer_hand} (Value: {dealer_value})")
|
||||
break
|
||||
|
||||
# Determine winner
|
||||
print(f"Player's Hand: {player_hand} (Value: {player_value})")
|
||||
print(f"Dealer's Hand: {dealer_hand} (Value: {dealer_value})")
|
||||
if player_value > 21:
|
||||
print("Dealer Wins!")
|
||||
elif dealer_value > 21:
|
||||
print("Player Wins!")
|
||||
elif player_value > dealer_value:
|
||||
print("Player Wins!")
|
||||
elif dealer_value > player_value:
|
||||
print("Dealer Wins!")
|
||||
else:
|
||||
print("It's a Tie!")
|
||||
# Judge determines the winner
|
||||
judge_ai_message = f"Player's final hand: {player_hand} (Value: {player_value}). Dealer's final hand: {dealer_hand} (Value: {dealer_value}). Who wins?"
|
||||
winner = judge_ai.generate_reply(messages=[{"content": judge_ai_message, "role": "user"}])
|
||||
print(f"\n{winner} Wins!")
|
||||
|
||||
# Run the game
|
||||
play_blackjack(player_ai, dealer_ai)
|
||||
play_blackjack(player_ai, dealer_ai, judge_ai)
|
||||
Loading…
Reference in a new issue