0

我正在尝试为五子棋游戏创建一种与人类或其他策略对抗的策略。我已经有了某种 minimax 函数,虽然我不太明白它是如何工作的,而且我还有一个 getscore 函数,它应该将最好的分数发回给 minimax ?但问题是我的 getScore 函数无法识别一行/列/对角线是否有 4 个或 5 个圆圈。

这是我的代码:

public class JarmoStrategyV1 implements ComputerStrategy {

    public static int Lrow = 0;
    public static int Lcol = 0;
    public static int Drow = 0;
    public static int Dcol = 0;
    public static final int E = 0;
    public static final int X = 1; // black
    public static final int O = -1; // white
    public static final int WINSCORE = 100;
    private static final int WINCOUNT = 5;


    public Location getMove(SimpleBoard board, int player) {
        // let's operate on 2-d array
        int[][] b = board.getBoard();
        System.out.println(getScore(b, player));
        for (int row = 0; row < b.length; row++) {
            for (int col = 0; col < b[0].length; col++) {
                if (b[row][col] == SimpleBoard.EMPTY) {
                    // first empty location
                    return new Location(row, col);
                }
            }
        }
        return null;
    }


    @Override
    public String getName() {
        return "Student name";
    }
    public static int minimax(int[][] board, int player, int depth) {

        if (getScore(board, player) == WINSCORE) {
            //
            return WINSCORE;
        }

        if (depth == 2) {
            return getScore(board, player);
        }

        int max = Integer.MIN_VALUE;
        if (player == -1){
            max = Integer.MAX_VALUE;
        }

        System.out.println(max);

        List<Location> possibleMoves = getPossibleMoves(board);
        for (Location loc : possibleMoves) { 
            board[loc.getRow()][loc.getColumn()] = player;
            int newplayer = 0 - player;
            if(newplayer == 1){
                int value = minimax(board, newplayer,depth + 1);
                if(value < max) {
                    max = value;
                }
            }
            if (newplayer == -1){
                int value = minimax(board, newplayer, depth + 1);
                if (value > max) {
                    max = value;
                }
            }

            board[loc.getRow()][loc.getColumn()] = E;
        }

        return max;
    }
    public static int getScore(int[][] board, int muutuja) {

        //int yks = 0;

        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                if (board[row][col] == muutuja) {
                    if (row <= (board.length - 5)) {
                        if (col <= board.length && getCount(board, row, col, 0, 1, muutuja, WINCOUNT) >= (WINCOUNT - 1) && getCount(board, row, (col + 4), 0, 1, E, 1 ) >= 1) return 1; // - 4 in a row
                        if (row >= 1 && getCount(board, row, col, 1, 0, muutuja, WINCOUNT) >= (WINCOUNT -1) && getCount(board, (row - 1), col, 1, 0, E, 1) == 1) return 1;


                        if (getCount(board, row, col, 1, 0, muutuja, WINCOUNT) >= WINCOUNT) return 100; // | 5 in a row
                        if (col <= WINCOUNT && getCount(board, row, col, 1, 1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // \
                        if (col >= WINCOUNT && getCount(board, row, col, 1, -1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // /                        
                    }
                    if (col <= WINCOUNT && getCount(board, row, col, 0, 1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // - 
                }
            }
        }
        return 0;
    }

    public static int getCount(int[][] board, int row, int col, int rowd, int cold, int player, int test) {

        int count = 0;
        for (int i = 0; i < test; i++) {
            if (board[row + i * rowd][col + i * cold] == player) count++;
            else break;
        }
        return count;
    }

    public static ArrayList<Location> getPossibleMoves(int[][] board) {
        ArrayList<Location> availableMoves = new ArrayList<Location>();
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                if (board[row][col] == E) {
                    availableMoves.add(new Location(row, col));
                }
            }
        }
        return availableMoves;
    }


}

getScore 似乎存在某种问题,当我运行此代码时,我只能玩一会儿,直到我的 Gomoku 应用程序崩溃。

如果您想自己尝试一下,那么您可以通过 Eclipse 打开它。下载项目文件:http ://www68.zippyshare.com/v/feWl2QwC/file.html 导入到eclipse项目/工作区。而且您还必须在 lib 文件夹中构建这 2 个 jar 文件的路径。注意:我只能编辑 gomoku.strategies 包中的文件。

4

1 回答 1

0

堆栈跟踪显示异常。添加调试打印或使用调试器运行

java.lang.IllegalArgumentException: It's computer's turn, pass the player to makeMove()
        at gomoku.Game.makeMove(Game.java:476)
于 2015-06-01T09:33:14.500 回答