minimax评价方法

wljmcqd8  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(285)

我必须为我正在创建的游戏创建一个极大极小算法和一个评估方法,这也是一个项目。我认为最好的评估方法是,算法查看的每一步都基于两个因素得到一个分数。它捕获了多少个工件,以及基于阵列(posval)在板上的位置有多有利。评估的工作方式是调用evaluate方法来评估玩家或算法可以做出的所有下一步动作。然后它模拟一个棋盘,在棋盘上移动,然后继续观察对手可以做的动作,依此类推。终止这个“模拟”的条件是一个变量检查,基本上就是我要评估的深度。

public static int[] evaluate(char[] state, char evaluation) {

    int result = 0;
    int resultIndex = 0;
    if(fakeDepth <= inDepth) {
        ArrayList<Integer> values = new ArrayList<>();       
        ArrayList<Integer> positionsPtr = new ArrayList<>(); 
        ArrayList<Integer> direction = new ArrayList<>();

        int[] offset = {-9, -8, -7, -1, 1, 7, 8, 9};         
        char notEvaluation;

        if (evaluation == '1') {
            notEvaluation = '2';
        } else {
            notEvaluation = '1';
        }

        for (int i = 0; i < state.length; i++) {
            int value = 0;

            if (state[i] != evaluation && state[i] != notEvaluation) {

                for (int n : offset) {
                    int index = i + n;
                    boolean inBound = (index > 0) && (index < state.length);

                    if (inBound) {

                        if (state[index] == '0') {
                            continue;
                        } else if (state[index] == evaluation) {
                            int temp = index;

                            while (state[temp] == evaluation) {
                                value++;
                                temp = temp + n;
                                inBound = (temp > 0) && (temp < state.length);
                                if (!inBound) {
                                    temp -= n;
                                    break;
                                }
                            }

                            if (state[temp] == notEvaluation) {

                                if (fakeDepth + 1 % 2 == 0) {
                                    value = (value * 2) * -1;
                                    value = value + posValue[i] * -1;

                                } else {
                                    value *= 2;
                                    value += posValue[i];
                                }
                                values.add(value);
                                positionsPtr.add(i);
                                direction.add(n);
                            }
                        }
                    }
                }
            }
        }
        fakeDepth++;
        evalValues.addAll(values);
        for(int choice: positionsPtr){
            char[] temp = Arrays.copyOf(state, state.length);
            simulateFlip(temp, choice, direction.get(positionsPtr.indexOf(choice)), notEvaluation);
        }
        result = minimax(0, 0, true, values, MAX, MIN);
        resultIndex = positionsPtr.get(result);
    }
    fakeDepth = 0;
    return new int[]{result, resultIndex};
}

private static void simulateFlip(char[] state, int index, int direction, char toPlace) {
    boolean inBounds = (index + direction > 0) && (index + direction < state.length);
    if (inBounds){
        int temp = index;
        while(state[temp] != toPlace){
            state[temp] = toPlace;
            temp += direction;
            inBounds = (temp > 0) && (temp < state.length);
            if (!inBounds){
                break;
            }
        }
    }
    evaluate(state, toPlace);
}

我遇到的问题是,当使用resultindex=positionsptr.get(result)回溯时;我得到-1

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题