Files
nl-game-api/internal/ai/chess_rule.go
2026-08-14 13:17:03 +08:00

45 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ai
import (
"math/rand"
"nl-game-api-gin/internal/gamecore/xiangqi"
)
// ruleChessAI 本地规则象棋 AI极大极小搜索深度随难度提升
type ruleChessAI struct {
difficulty string // 难度easy=1层+随机扰动 medium=2层 hard=3层
name string // 显示名
}
// Name AI 显示名
func (a *ruleChessAI) Name() string { return a.name }
// searchDepth 难度对应的搜索深度
func (a *ruleChessAI) searchDepth() int {
switch a.difficulty {
case DiffEasy:
return 1
case DiffHard:
return 3
default:
return 2
}
}
// DecideMove 走子:搜索最优着法;简单难度 40% 概率走随机合法着法(模拟新手失误)
func (a *ruleChessAI) DecideMove(b *xiangqi.Board, side int) (xiangqi.Move, string) {
if a.difficulty == DiffEasy && rand.Float64() < 0.4 {
if m, ok := b.RandomMove(side); ok {
return m, ""
}
}
move, score := b.BestMove(side, a.searchDepth())
say := ""
// 分数大幅领先时补一句台词增加氛围
if score > 800 {
say = "这步棋你可要小心了。"
}
return move, say
}