Java Connect 4 MinMax算法

编辑:我不知道为什么有人将我的TicTacToe链接为我的问题的重复,甚至没有MinMax算法。

目前,我正在对应使用MinMax算法的计算机进行Connect4游戏。 在此之前,我们写了一个TicTacToe,它也使用MinMax,但我不知道如何改变我的旧算法以匹配Connect4-Game:/。 在TicTacToe中,我评估了每一个可能的举措,我写的胜利条件,它运作良好,但现在它不会与我的新条件。 我makeAMove等工作正常!

这些是我的旧条件和TicTacToe的MinMax:

//玩家1获胜

static boolean has1Won(int[][] array) {
gameBoard = array;
//Diagonal
if ((gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard[2][2] && gameBoard[0][0] == 1)
|| (gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[2][0] && gameBoard[0][2] == 1)) {

    return true;
}
//Spalten/Zeilen
for (int i = 0; i < 3; ++i) {
if (((gameBoard[i][0] == gameBoard[i][1] && gameBoard[i][0] == gameBoard[i][2] && gameBoard[i][0] == 1) 
|| (gameBoard[0][i] == gameBoard[1][i] && gameBoard[0][i] == gameBoard[2][i] && gameBoard[0][i] == 1))) {
        return true;
    }
}
return false;

}

//玩家2获胜

static boolean has2Won(int[][] array) {
gameBoard = array;
    //Diagonal
    if ((gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard[2][2] && gameBoard[0][0] == 2)
    || (gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[2][0] && gameBoard[0][2] == 2)) {

        return true;
    }
    //Spalten/Zeilen
    for (int i = 0; i < 3; ++i) {
        if (((gameBoard[i][0] == gameBoard[i][1] && gameBoard[i][0] == gameBoard[i][2] && gameBoard[i][0] == 2) 
        || (gameBoard[0][i] == gameBoard[1][i] && gameBoard[0][i] == gameBoard[2][i] && gameBoard[0][i] == 2))) {

        return true;
        }
    }
    return false;

}

正如我所说的,我将这些条件用于我的MinMax,如下所示:

public static int minimax(int depth, int turn) {

    if (Board.has1Won(Board.gameBoard)){
        return +1; // Der Computer gewinnt
    }
    if (Board.has2Won(Board.gameBoard)){
        return -1; // Der Spieler gewinnt
    }

    List<GameMove> gameMovesAvailable = GameMove.getAvailableGameMoves();
    if (gameMovesAvailable.isEmpty()){
        return 0; // Das Spiel endet unentschieden
    }

...

我不确定我如何才能使用我的新条件:

我想我必须写一个评估函数来检查这个例子(这是我对行的wincondition):

boolean getWinnerInRow (Playboard brd){

    int count = 0;

    for (int i = 0; i < 6; i++){
        for (int j = 0; j < 7; j++){
            if (brd.gameBoard[i][j] != 0 && brd.gameBoard[i][j] ==   brd.gameBoard[i][j+1]){
                count++;
        } else {
                count = 1;
        }
        if (count >= 4){
          return true;
      }
    }
  }
    return false;

我知道这是很多文字,但也许有人可以给我一些有用的提示:)

谢谢!

马克斯


我不确定你的测试结果是否正确。 试试这个(你需要稍微改变一下,但至少我确定它是正确的):

public static boolean testWinner(int[][] game, int lastColumn, Integ e) {

    int lastRow = 0; 
    while (lastRow < 6 && game[lastRow][lastColumn] == 0) {
        lastRow++; 
    }
    lastRow = lastRow; 

    int i = 0; 
    int j = 0; 
    int currentPlayer = game[lastRow][lastColumn]; 
    e.setI(currentPlayer);
    int sequence = 0; 

    i = lastRow; 
    boolean b = i < 3
            && game[i][lastColumn] == currentPlayer 
            && game[i+1][lastColumn] == currentPlayer
            && game[i+2][lastColumn] == currentPlayer
            && game[i+3][lastColumn] == currentPlayer; 
    if(b) {
        return true; 
    }

    sequence = 0; 
    j = lastColumn; 
    do {
        j--;
    } while(0 < j && game[lastRow][j] == currentPlayer); 
    if(j < 0 || game[lastRow][j] != currentPlayer) {
        j++; 
    }
    while(j <= 6 && game[lastRow][j] == currentPlayer) {
        j++; 
        sequence++; 
    }
    if (sequence >= 4) {
        return true; 
    }


    sequence = 0; 
    i = lastRow; 
    j = lastColumn; 
    do {
        i--; 
        j--; 
    } while(0 < i && 0 < j && game[i][j] == currentPlayer); 
    if(i < 0 || j < 0 || game[i][j] != currentPlayer) {
        i++; 
        j++; 
    }
    while(i <= 5 && j <= 6 && game[i][j] == currentPlayer) {
        i++; 
        j++; 
        sequence++; 
    }
    if (sequence >= 4) {
        return true; 
    }


    sequence = 0; 
    i = lastRow; 
    j = lastColumn; 
    do {
        i++; 
        j--; 
    } while(i < 5 && 0 < j && game[i][j] == currentPlayer); 
    if (5 < i || j < 0 || game[i][j] != currentPlayer) {
        i--; 
        j++; 
    }
    while(0 <= i && j <= 6 && game[i][j] == currentPlayer) {
        i--; 
        j++; 
        sequence++; 
    }
    if (sequence >= 4) {
        return true; 
    }

    return false; 
}

Integ只是一个带整数的类。 我创建它是因为包装器不是一个真正的对象(不能通过引用传递)。

private static class Integ {

    private int i; 

    public Integ() {
        this.i = 0; 
    }

    public void increment() {
        this.i = this.i + 1; 
    }

    public int getI() {
        return this.i;
    }

    public void setI(int i) {
        this.i = i;
    }

}
链接地址: http://www.djcxy.com/p/56387.html

上一篇: Java Connect 4 MinMax Algorithm

下一篇: About randomness and minmax algorithm with alpha beta pruning