在java中使用alpha beta修剪的tic tac toe

时间:2010-02-16 10:33:56

标签: java artificial-intelligence evaluation tic-tac-toe

我正在尝试使用迭代Alpha-Beta prunning来玩tic tac toe, 我有一个移动的第二个限制,但由于某种原因 不好用。

我修改了常规的alpha-beta代码,而不是返回 alpha或beta,它返回一个状态(下一步移动的板)

每当我创建孩子时,我都会更新他们的深度。

但是由于某种原因,我一直在输,我看到了 我的alpha beta并没有看到最好的举动。

这是我的代码:

外循环:

while (watch.get_ElapsedMilliseconds() < 900 && d <= board.length * board[0].length - 1)
        {
            s = maxiMin(beginSt, d, watch);
            if (s.getNextMove().getIsWin() == true)
            {
                break;
            }
            d++;
        }
        return new location(s.getNextMove().getRow(), s.getNextMove().getCol());

alpha beta:

public State maxiMin(State s, int depth, Stopwatch timer)
    {
        if (s.getDepth() == 7)
        {
            Console.WriteLine();
        }
        if (timer.get_ElapsedMilliseconds() > 850 || s.getDepth() == depth || goalTest(s.getBoard()) != 0)
        {
            s.evaluationFunc(line_length, PlayerShape);
            s.setAlpha(s.getEvaluation());
            s.setBeta(s.getEvaluation());
            return s;
        }
        LinkedList<State> children = createChildren(s, true);
        // No winner, the board is full
        if (children.get_Count() == 0)
        {
            s.evaluationFunc(line_length, PlayerShape);
            s.setAlpha(s.getEvaluation());
            s.setBeta(s.getEvaluation());
            return s;
        }
        while (children.get_Count() > 0)
        {
            State firstChild = children.get_First().get_Value();
            children.RemoveFirst();
            State tmp = miniMax(firstChild, depth, timer);
            int value = tmp.getBeta();
            if (value > s.getAlpha())
            {
                s.setAlpha(value);
                s.setNextMove(tmp);
            }
            if (s.getAlpha() >= s.getBeta())
            {
                return s;
            }
        }
        return s;
    }

    public State miniMax(State s, int depth, Stopwatch timer)
    {
        if (s.getDepth() == 7)
        {
            Console.WriteLine();
        }
        if (timer.get_ElapsedMilliseconds() > 850 || s.getDepth() == depth || goalTest(s.getBoard()) != 0)
        {
            s.evaluationFunc(line_length, PlayerShape);
            s.setAlpha(s.getEvaluation());
            s.setBeta(s.getEvaluation());
            return s;
        }
        LinkedList<State> children = createChildren(s, false);
        // No winner, the board is full
        if (children.get_Count() == 0)
        {
            s.evaluationFunc(line_length, PlayerShape);
            s.setAlpha(s.getEvaluation());
            s.setBeta(s.getEvaluation());
            return s;
        }
        while (children.get_Count() > 0)
        {
            State firstChild = children.get_First().get_Value();
            children.RemoveFirst();
            State tmp = maxiMin(firstChild, depth, timer);
            int value = tmp.getAlpha();
            if (value < s.getBeta())
            {
                s.setBeta(value);
                s.setNextMove(tmp);
            }
            if (s.getAlpha() >= s.getBeta())
            {
                return s;
            }
        }
        return s;
    }

如果有人能告诉我是否有问题,那么会非常高兴。我怀疑也许 它与我有关,我正在返回“s”而不是常规的alpha beta 返回评估,但我没有设法找到错误。

提前致谢,

海伦

1 个答案:

答案 0 :(得分:5)

首先,tic-tac-toe是一个非常简单的游戏,我相信它可以用更简单的代码解决,主要是因为我们知道总是有一个平局选项,并且状态总数小于3 ^ 9(包括对称和许多不可能的状态。)

至于你的代码我相信你的一个问题是你似乎没有增加递归调用的深度。

你的代码中也存在许多不良风格的问题,你将miniMax和MaxiMin分成两个函数,尽管它们基本相同。通过从中删除元素来迭代集合,而不是使用for-each或迭代器(甚至是int迭代器)。

相关问题