Java gomoku minimax

时间:2015-06-01 09:07:03

标签: java minimax gomoku

我试图制定一个与人类或其他游戏Gomoku策略相抗衡的策略。我已经有了某种极小极大功能,虽然我不太明白它是如何工作的,而且我还有一个获取功能,它应该将最佳分数发送回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文件夹中构建这两个jar文件的路径。 注意:我只能编辑gomoku.strategies包中的文件。

1 个答案:

答案 0 :(得分:0)

stacktrace显示异常。添加调试打印或使用调试器运行

function axleees() {
        $sql = "SELECT ti.axle_nr, ti.train_id, ti.bogie_nr, uti.axle_id, uti.train_id, uti.axle, uti.distance
                FROM bogie as ti
                JOIN axle as uti
                ON ti.train_id = uti.train_id
                WHERE ti.train_id = :train_id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":train_id", $_GET["train_id"], PDO::PARAM_INT);
        $sth->execute();
        return $sth->fetchAll();
    }