我正在尝试实现一个简单的游戏的消极的游戏,其中球员交替添加一个或两个的运行和。将总数增加到21胜的球员。
我在这里使用伪码:algorithm
人类玩家首先移动,因此计算机应该很容易地通过添加使总一致性为0mod 3的数字而获胜。
我不会做任何动态的移动生成。只需比较将1加到运行和的否定得分与将2加到运行和的消极得分。
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次每次。
发布于 2017-05-03 06:21:23
静态评估函数不正确。
从节点当前玩家的角度来看,algorithm是一个启发式得分,是一个负数节点的返回值。
如果(总== 21),这总是节点当前播放器的损失。因此,否定返回必须是-100。还有其他代码错误,例如,当总计为22时。
发布于 2016-11-10 12:55:43
一个不能移动的玩家显然会输掉比赛,对吧?如果是,那么
if (total == 21) {
return color * 100;
}在我看来是不对的,因为它颠倒了规则。你是说不能移动的玩家赢了!试着重做这三行。
https://stackoverflow.com/questions/40527276
复制相似问题