Java Connect 4 MinMax算法

时间:2016-06-21 08:22:47

标签: java algorithm minmax negamax

编辑:我不知道为什么有人把我的TicTacToe作为我的问题的副本链接,其中甚至没有MinMax算法。

目前我正在开发针对应该使用MinMax算法的计算机的Connect4游戏。 在此之前,我们写了一个也使用MinMax的TicTacToe,但我不知道如何更改我的旧算法以匹配Connect4-Game:/。 在TicTacToe中,我用我写的win条件评估了每一个可能的移动,它运行良好,但现在它不适用于我的新条件。 我的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;

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

谢谢!

最高

1 个答案:

答案 0 :(得分:0)

我不确定您找到胜利者的测试是否正确。试试这个(你需要稍微改一下,但至少我确定它是正确的):

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;
    }

}