【问题标题】:Negamax for simple addition gameNegamax 用于简单的加法游戏
【发布时间】:2016-11-10 12:09:26
【问题描述】:

我正在尝试为一个简单的游戏实现 negamax,其中玩家交替将一或两个添加到运行总和中。将总数增加到 21 的玩家获胜。

我在这里使用伪代码:https://en.wikipedia.org/wiki/Negamax#Negamax_base_algorithm

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

我没有进行任何动态移动生成。只需将运行总和加 1 的 negamax 分数与运行总和加 2 的 negamax 分数进行比较。

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;
    }
}

负最大函数:

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;
}

最大方法:

static int max(int a, int b) {
    if (a > b) return a;
    return b;
}

不知道为什么 AI 每次都加 2。

【问题讨论】:

  • 你能展示你的max方法吗?
  • 已将其添加到帖子中

标签: c# alpha-beta-pruning negamax


【解决方案1】:

静态评估函数不正确。

https://en.wikipedia.org/wiki/Negamax#Negamax_base_algorithm negamax 节点的返回值是一个启发式分数从节点当前玩家的角度来看

如果 (total == 21),对于节点的当前玩家来说总是输。所以 negamax 回报必须是 -100。还有其他代码错误,比如total为22时。

【讨论】:

  • 我认为我在一个或多个地方的观点是错误的。谢谢。我搞定了
【解决方案2】:

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

if (total == 21) {
    return color * 100;
}

在我看来是错误的,因为它颠倒了规则。你是说不能移动的玩家获胜!尝试修改这 3 行。

【讨论】:

  • 这部分只是静态评估函数。如果/当总数变为 21 时,它只会返回赢/输。
  • 我认为我们从来没有达到玩家无法移动的地步。只要击中 21,就会发生中断,并且每当击中 21 时,negamax 函数都会返回一个值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多