strat-gameplay-webapp/backend/app/core/ai_opponent.py
Cal Corum a4b99ee53e CLAUDE: Replace black and flake8 with ruff for formatting and linting
Migrated to ruff for faster, modern code formatting and linting:

Configuration changes:
- pyproject.toml: Added ruff 0.8.6, removed black/flake8
- Configured ruff with black-compatible formatting (88 chars)
- Enabled comprehensive linting rules (pycodestyle, pyflakes, isort,
  pyupgrade, bugbear, comprehensions, simplify, return)
- Updated CLAUDE.md: Changed code quality commands to use ruff

Code improvements (490 auto-fixes):
- Modernized type hints: List[T] → list[T], Dict[K,V] → dict[K,V],
  Optional[T] → T | None
- Sorted all imports (isort integration)
- Removed unused imports
- Fixed whitespace issues
- Reformatted 38 files for consistency

Bug fixes:
- app/core/play_resolver.py: Fixed type hint bug (any → Any)
- tests/unit/core/test_runner_advancement.py: Removed obsolete random mock

Testing:
- All 739 unit tests passing (100%)
- No regressions introduced

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 15:33:21 -06:00

154 lines
4.8 KiB
Python

"""
AI Opponent - Automated decision-making for AI-controlled teams.
Provides strategic decision generation for AI opponents in single-player
and AI vs AI game modes.
This is a stub implementation for Week 7. Full AI logic will be developed
in Week 9 as part of Phase 3 AI opponent integration.
Author: Claude
Date: 2025-10-29
"""
import logging
from app.models.game_models import DefensiveDecision, GameState, OffensiveDecision
logger = logging.getLogger(f"{__name__}.AIOpponent")
class AIOpponent:
"""
AI opponent decision-making engine.
Generates defensive and offensive decisions for AI-controlled teams.
Current implementation uses simple heuristics. Full AI logic will be
implemented in Week 9.
"""
def __init__(self, difficulty: str = "balanced"):
"""
Initialize AI opponent.
Args:
difficulty: AI difficulty level
- "balanced": Standard decision-making
- "yolo": Aggressive playstyle (more risks)
- "safe": Conservative playstyle (fewer risks)
"""
self.difficulty = difficulty
logger.info(f"AIOpponent initialized with difficulty: {difficulty}")
async def generate_defensive_decision(self, state: GameState) -> DefensiveDecision:
"""
Generate defensive decision for AI-controlled fielding team.
Week 7 stub: Returns default "normal" positioning.
Week 9: Implement full decision logic based on game situation.
Args:
state: Current game state
Returns:
DefensiveDecision with AI-generated strategy
TODO (Week 9):
- Analyze batter tendencies (pull/opposite field)
- Consider runner speed for hold decisions
- Evaluate double play opportunities
- Adjust positioning based on inning/score
"""
logger.debug(f"Generating defensive decision for game {state.game_id}")
# Week 7 stub: Simple default decision
decision = DefensiveDecision(
alignment="normal",
infield_depth="normal",
outfield_depth="normal",
hold_runners=[],
)
# TODO Week 9: Add actual AI logic
# if state.outs < 2 and state.is_runner_on_first():
# decision.infield_depth = "double_play"
# if state.is_runner_on_third() and state.outs < 2:
# decision.infield_depth = "in"
logger.info(
f"AI defensive decision: IF: {decision.infield_depth}, OF: {decision.outfield_depth}"
)
return decision
async def generate_offensive_decision(self, state: GameState) -> OffensiveDecision:
"""
Generate offensive decision for AI-controlled batting team.
Week 7 stub: Returns default "normal" approach.
Week 9: Implement full decision logic based on game situation.
Args:
state: Current game state
Returns:
OffensiveDecision with AI-generated strategy
TODO (Week 9):
- Evaluate stealing opportunities (runner speed, pitcher, catcher)
- Consider bunting in appropriate situations
- Adjust batting approach based on score/inning
- Implement hit-and-run logic
"""
logger.debug(f"Generating offensive decision for game {state.game_id}")
# Week 7 stub: Simple default decision
decision = OffensiveDecision(
approach="normal", steal_attempts=[], hit_and_run=False, bunt_attempt=False
)
# TODO Week 9: Add actual AI logic
# if state.is_runner_on_first() and not state.is_runner_on_second():
# # Consider stealing second
# if self._should_attempt_steal(state):
# decision.steal_attempts = [2]
logger.info(
f"AI offensive decision: steal={decision.steal_attempts}, hr={decision.hit_and_run}"
)
return decision
def _should_attempt_steal(self, state: GameState) -> bool:
"""
Determine if AI should attempt a steal (Week 9).
TODO: Implement steal decision logic
- Evaluate runner speed
- Evaluate pitcher hold rating
- Evaluate catcher arm
- Consider game situation (score, inning, outs)
Returns:
True if steal should be attempted
"""
# Week 7 stub: Never steal
return False
def _should_attempt_bunt(self, state: GameState) -> bool:
"""
Determine if AI should attempt a bunt (Week 9).
TODO: Implement bunt decision logic
- Consider sacrifice bunt situations
- Evaluate batter bunting ability
- Assess defensive positioning
Returns:
True if bunt should be attempted
"""
# Week 7 stub: Never bunt
return False
# Singleton instance for global access
ai_opponent = AIOpponent()