-1

编辑:我不知道为什么有人将我的 TicTacToe 链接为我的问题的重复项,其中甚至没有 MinMax-Algorithm。

目前我正在针对应该使用 MinMax-Algorithm 的计算机进行 Connect4 游戏。在此之前,我们编写了一个也使用 MinMax 的井字游戏,但我不确定如何更改我的旧算法以匹配 Connect4-Game :/。在井字游戏中,我用我写的获胜条件评估了每一个可能的移动,它运行良好,但现在它不适用于我的新条件。我的 makeAMove 等工作正常!

这些是我的旧条件和井字游戏的 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;

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

谢谢!

最大限度

4

1 回答 1

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

}
于 2016-07-09T08:45:37.643 回答