added can_bot_afford to improve profiling

This commit is contained in:
2025-11-16 15:32:22 -05:00
parent d1be25c6e8
commit 5b4609dc3b

View File

@@ -1,3 +1,5 @@
"""Bot for Splendor game."""
from __future__ import annotations from __future__ import annotations
import random import random
@@ -6,6 +8,7 @@ from .base import (
BASE_COLORS, BASE_COLORS,
Action, Action,
BuyCard, BuyCard,
Card,
GameState, GameState,
GemColor, GemColor,
PlayerState, PlayerState,
@@ -17,6 +20,18 @@ from .base import (
) )
def can_bot_afford(player: PlayerState, card: Card) -> bool:
"""Check if player can afford card, using discounts + gold."""
missing = 0
gold = player.tokens["gold"]
for color, cost in card.cost.items():
missing += max(0, cost - player.discounts.get(color, 0) - player.tokens.get(color, 0))
if missing > gold:
return False
return True
class RandomBot(Strategy): class RandomBot(Strategy):
"""Dumb bot that follows rules but doesn't think.""" """Dumb bot that follows rules but doesn't think."""
@@ -27,7 +42,7 @@ class RandomBot(Strategy):
affordable: list[tuple[int, int]] = [] affordable: list[tuple[int, int]] = []
for tier, row in game.table_by_tier.items(): for tier, row in game.table_by_tier.items():
for idx, card in enumerate(row): for idx, card in enumerate(row):
if player.can_afford(card): if can_bot_afford(player, card):
affordable.append((tier, idx)) affordable.append((tier, idx))
if affordable and random.random() < 0.5: if affordable and random.random() < 0.5:
tier, idx = random.choice(affordable) tier, idx = random.choice(affordable)