首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Negamax用于简单加法游戏

Negamax用于简单加法游戏
EN

Stack Overflow用户
提问于 2016-11-10 12:09:26
回答 2查看 313关注 0票数 0

我正在尝试实现一个简单的游戏的消极的游戏,其中球员交替添加一个或两个的运行和。将总数增加到21胜的球员。

我在这里使用伪码:algorithm

人类玩家首先移动,因此计算机应该很容易地通过添加使总一致性为0mod 3的数字而获胜。

我不会做任何动态的移动生成。只需比较将1加到运行和的否定得分与将2加到运行和的消极得分。

代码语言:javascript
复制
int total = 0;

Console.WriteLine("the current total is " + total);

while (total < 21) {
    Console.WriteLine("add 1 or 2?");
    total += Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("you increased the total to " + total);
    if (total == 21) {
        Console.WriteLine("you win");
        break;
    }

    if (negamax(total + 1, 1) > negamax(total + 2, 1)) total++;
    else total += 2;

    Console.WriteLine("computer increased the total to " + total);
    if (total == 21) {
        Console.WriteLine("computer wins");
        break;
    }
}

否定函数:

代码语言:javascript
复制
static int negamax(int total, int color) {
    if (total == 21) {
        return color * 100;
    }

    int bestValue = -100;

    for (int i = 1; i <= 2; i++) {
        if (total + i <= 21) {
            int v = -1 * negamax(total + i, -1 * color);
            bestValue = max(bestValue, v);
        }
    }
    return bestValue;
}

最大法:

代码语言:javascript
复制
static int max(int a, int b) {
    if (a > b) return a;
    return b;
}

不知道为什么AI只是增加2次每次。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-03 06:21:23

静态评估函数不正确。

从节点当前玩家的角度来看,algorithm是一个启发式得分,是一个负数节点的返回值。

如果(总== 21),这总是节点当前播放器的损失。因此,否定返回必须是-100。还有其他代码错误,例如,当总计为22时。

票数 1
EN

Stack Overflow用户

发布于 2016-11-10 12:55:43

一个不能移动的玩家显然会输掉比赛,对吧?如果是,那么

代码语言:javascript
复制
if (total == 21) {
    return color * 100;
}

在我看来是不对的,因为它颠倒了规则。你是说不能移动的玩家赢了!试着重做这三行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40527276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档