45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
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
|
||
}
|