Skip to content

feat: AI vs AI battle with multiple strategies - #17

Merged
edge2992 merged 4 commits into
mainfrom
feature/ai-battle
Mar 22, 2026
Merged

edge2992 merged 4 commits into
mainfrom
feature/ai-battle

Conversation

@edge2992

Copy link
Copy Markdown
Owner

Summary

  • Strategy インターフェースを導入し、3つのAI戦略(Greedy, Statistical, LLM)をプラガブルに実装
  • yatz battle コマンドで AI 同士を対戦させ、TUI で観戦 or quiet モードで統計表示
  • Statistical 戦略は全32通りの hold 組み合わせの期待値を計算し、Greedy を圧倒(100戦97勝、平均スコア 207 vs 112)
  • LLM 戦略は anthropic-sdk-go で Claude API を直接呼び出し、マークダウンのペルソナでキャラクター対戦が可能

Usage

# Greedy vs Statistical (quiet mode, 100 rounds)
yatz battle --players "G:greedy" --players "S:statistical" --rounds 100 --quiet

# TUI spectator mode
yatz battle --players "G:greedy" --players "S:statistical" --speed 500ms

# LLM with persona
ANTHROPIC_API_KEY=... yatz battle --players "G:greedy" --players "Claude:llm:personas/aggressive.md"

Changes

  • engine/strategy.go: Strategy interface, TurnAction type
  • engine/strategy_greedy.go: 既存 bestCategory ロジックを Strategy 実装に抽出
  • engine/strategy_statistical.go: 期待値ベースの hold/score 判断
  • engine/expected_value.go: 全 hold 組み合わせ × reroll パターンの期待値計算
  • engine/battle.go: RunBattle オーケストレーター
  • engine/ai.go: Strategy 対応に改修(hold ループ、フォールバック防御)
  • engine/client.go: AITurnResultStrategyName, HoldHistory 追加
  • bot/strategy_llm.go: Claude API 直接呼び出し、JSON パース、Greedy フォールバック
  • bot/persona.go: マークダウンペルソナパーサー
  • cli/spectator.go: Bubbletea v2 観戦 TUI
  • cmd/yatz/battle.go: yatz battle コマンド(quiet/TUI 両対応)
  • personas/: サンプルペルソナ 3種(aggressive, defensive, gambler)

Test plan

  • make check — 既存テスト全通過、ビルド成功
  • go test ./engine/ -run TestStatistical — 期待値計算テスト
  • go test ./engine/ -run TestRunBattle — バトルオーケストレーションテスト
  • yatz battle --players "G:greedy" --players "S:statistical" --quiet — quiet モード動作確認
  • yatz battle --players "G:greedy" --players "S:statistical" --rounds 100 --quiet — 統計モード確認
  • yatz battle --players "G:greedy" --players "S:statistical" --speed 500ms — TUI 観戦確認
  • ANTHROPIC_API_KEY=... yatz battle --players "G:greedy" --players "Claude:llm" — LLM 対戦確認

Implement Strategy interface and three AI strategies (Greedy,
Statistical, LLM) with a battle orchestration system. The
StatisticalStrategy evaluates all 32 hold combinations using
expected value calculation, significantly outperforming Greedy.

- Add Strategy interface and TurnAction type (engine/strategy.go)
- Extract GreedyStrategy from existing bestCategory logic
- Add StatisticalStrategy with expected value calculation
- Add RunBattle orchestrator for automated AI-vs-AI games
- Add `yatz battle` command with quiet and TUI spectator modes
- Add LLMStrategy using anthropic-sdk-go for Claude API calls
- Add persona system for LLM character customization
- Add sample personas (aggressive, defensive, gambler)
- Refactor AIPlayer to support Strategy and hold loop
- Extend AITurnResult with StrategyName and HoldHistory
When a Strategy returns "hold" after all rolls are used, PlayTurn
now falls back to best available category instead of passing an
empty category to Score(). Also deduplicates GreedyStrategy logic
by reusing bestCategoryForDice.
…te LLM hold indices

Add empty-slice fallback in bestCategoryForDice to prevent panic.
Reject LLM hold actions with empty indices to avoid unintended
full rerolls.
…ject docs

Add comprehensive documentation for the AI Battle feature:
- Design spec covering Strategy pattern, battle engine, TUI spectator
- Implementation plan with completed phases and future extensions
- Update CLAUDE.md with bot/, personas/, Strategy pattern, battle commands
- Update README.md with AI Battle section, commands, and persona guide
@edge2992
edge2992 merged commit 3b6078d into main Mar 22, 2026
5 checks passed
@edge2992
edge2992 deleted the feature/ai-battle branch March 22, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant