138 lines
5.4 KiB
Python
138 lines
5.4 KiB
Python
|
|
import random
|
||
|
|
from models.constants import CATEGORIES
|
||
|
|
|
||
|
|
class AIEngine:
|
||
|
|
@staticmethod
|
||
|
|
def get_highest_scoring_category(model, player):
|
||
|
|
best_cat, max_points = None, -1
|
||
|
|
for cat_key, _ in CATEGORIES:
|
||
|
|
if player.scores[cat_key] is None:
|
||
|
|
pts = model.calculate_score(cat_key, model.dice)
|
||
|
|
if pts > max_points:
|
||
|
|
max_points, best_cat = pts, cat_key
|
||
|
|
return best_cat, max_points
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_sacrifice_category(player):
|
||
|
|
# Order of least painful sacrifices
|
||
|
|
sacrifice_order = ["1", "2", "3", "Y", "LS", "SS", "4K", "3K", "4", "5", "6", "FH", "CH"]
|
||
|
|
for cat in sacrifice_order:
|
||
|
|
if player.scores[cat] is None:
|
||
|
|
return cat
|
||
|
|
return None
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def play_level_0(model, player):
|
||
|
|
"""Tier 0: The Rust AI. Rolls once, no rerolls, greedy score selection."""
|
||
|
|
model.reset_turn()
|
||
|
|
model.held = [False] * 5
|
||
|
|
model.roll_unheld()
|
||
|
|
model.rolls_left = 0
|
||
|
|
|
||
|
|
best_cat, max_points = AIEngine.get_highest_scoring_category(model, player)
|
||
|
|
return best_cat, max_points
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def play_level_1(model, player):
|
||
|
|
"""Tier 1: Basic Hold. Holds the most frequent die, rerolls the rest, greedy score."""
|
||
|
|
model.reset_turn()
|
||
|
|
model.held = [False] * 5
|
||
|
|
|
||
|
|
for _ in range(3): # 1 initial + 2 rerolls
|
||
|
|
if model.rolls_left < 2:
|
||
|
|
counts = {x: model.dice.count(x) for x in range(1, 7)}
|
||
|
|
best_die = max(counts, key=counts.get)
|
||
|
|
model.held = [d == best_die for d in model.dice]
|
||
|
|
model.roll_unheld()
|
||
|
|
else:
|
||
|
|
model.roll_unheld()
|
||
|
|
|
||
|
|
best_cat, max_points = AIEngine.get_highest_scoring_category(model, player)
|
||
|
|
if max_points == 0:
|
||
|
|
best_cat = AIEngine.get_sacrifice_category(player)
|
||
|
|
|
||
|
|
return best_cat, max_points
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def play_level_2(model, player):
|
||
|
|
"""Tier 2: Intermediate. Looks for Straights and Full Houses, smart sacrifices."""
|
||
|
|
model.reset_turn()
|
||
|
|
|
||
|
|
for _ in range(3):
|
||
|
|
if model.rolls_left < 2:
|
||
|
|
counts = {x: model.dice.count(x) for x in range(1, 7)}
|
||
|
|
unique_dice = set(model.dice)
|
||
|
|
|
||
|
|
# Check for near straights if straight categories are open
|
||
|
|
needs_ss = player.scores["SS"] is None
|
||
|
|
needs_ls = player.scores["LS"] is None
|
||
|
|
if (needs_ss or needs_ls) and len(unique_dice) >= 3:
|
||
|
|
# Hold unique dice to fish for straight
|
||
|
|
model.held = []
|
||
|
|
seen = set()
|
||
|
|
for d in model.dice:
|
||
|
|
if d not in seen:
|
||
|
|
model.held.append(True)
|
||
|
|
seen.add(d)
|
||
|
|
else:
|
||
|
|
model.held.append(False)
|
||
|
|
else:
|
||
|
|
# Default: hold most frequent
|
||
|
|
best_die = max(counts, key=counts.get)
|
||
|
|
model.held = [d == best_die for d in model.dice]
|
||
|
|
|
||
|
|
model.roll_unheld()
|
||
|
|
else:
|
||
|
|
model.roll_unheld()
|
||
|
|
|
||
|
|
best_cat, max_points = AIEngine.get_highest_scoring_category(model, player)
|
||
|
|
if max_points == 0:
|
||
|
|
best_cat = AIEngine.get_sacrifice_category(player)
|
||
|
|
|
||
|
|
return best_cat, max_points
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def play_level_3(model, player):
|
||
|
|
"""Tier 3: Advanced. Prioritizes Yahtzee, upper section bonus, and highly dynamic holds."""
|
||
|
|
model.reset_turn()
|
||
|
|
|
||
|
|
for _ in range(3):
|
||
|
|
if model.rolls_left < 2:
|
||
|
|
counts = {x: model.dice.count(x) for x in range(1, 7)}
|
||
|
|
most_freq_val = max(counts.values())
|
||
|
|
best_die = max(counts, key=counts.get)
|
||
|
|
|
||
|
|
# If we have 3 or 4 of a kind, ALWAYS push for Yahtzee if open
|
||
|
|
if most_freq_val >= 3 and player.scores["Y"] is None:
|
||
|
|
model.held = [d == best_die for d in model.dice]
|
||
|
|
# If Upper Section needs help (4s, 5s, 6s)
|
||
|
|
elif best_die >= 4 and player.scores[str(best_die)] is None:
|
||
|
|
model.held = [d == best_die for d in model.dice]
|
||
|
|
else:
|
||
|
|
# Fallback to basic hold
|
||
|
|
model.held = [d == best_die for d in model.dice]
|
||
|
|
|
||
|
|
model.roll_unheld()
|
||
|
|
else:
|
||
|
|
model.roll_unheld()
|
||
|
|
|
||
|
|
best_cat, max_points = AIEngine.get_highest_scoring_category(model, player)
|
||
|
|
|
||
|
|
# Don't waste Chance on a low roll (e.g., sum < 18)
|
||
|
|
if best_cat == "CH" and max_points < 18:
|
||
|
|
alt_cat = AIEngine.get_sacrifice_category(player)
|
||
|
|
if alt_cat: return alt_cat, 0
|
||
|
|
|
||
|
|
if max_points == 0:
|
||
|
|
best_cat = AIEngine.get_sacrifice_category(player)
|
||
|
|
|
||
|
|
return best_cat, max_points
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def execute_turn(level, model, player):
|
||
|
|
if level == 0: return AIEngine.play_level_0(model, player)
|
||
|
|
elif level == 1: return AIEngine.play_level_1(model, player)
|
||
|
|
elif level == 2: return AIEngine.play_level_2(model, player)
|
||
|
|
elif level == 3: return AIEngine.play_level_3(model, player)
|
||
|
|
return AIEngine.play_level_0(model, player)
|