Java Connect 4 MinMax Algorithm

EDIT: I don't know why somebody links me a TicTacToe as duplicate for my question, there isn't even a MinMax-Algorithm in it.

Currently i'm working on a Connect4 game against the computer which should use the MinMax-Algorithm. Before that, we wrote a TicTacToe which also uses the MinMax, but i'm not sure how to change my old algorithm to match the Connect4-Game :/. In TicTacToe i evaluated each possible move with the win-conditions i wrote, it worked fine, but now it won't work with my new conditions. My makeAMove etc. works fine!

These are my old conditions and the MinMax for TicTacToe:

//Player 1 wins

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;

}

// Player 2 wins

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;

}

As I said, i used these conditions for my MinMax like this:

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
    }

...

I'm not sure how i can get this to work with my new conditions:

I think i have to write an evaluating function which checks this for example (this is my wincondition for Rows):

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;

I know it's a lot of text, but maybe somebody can give me some useful tips :)

Thanks!

Max


I'm not sure that your test to find the winner is correct. Try this (you'll need to change it a little bit, but at least I'm sure that it's correct):

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 is just a class with an integer. I've created it because the wrapper is not really an object (can't pass by reference).

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/56388.html

上一篇: 用minmax算法的Java tictactoe问题

下一篇: Java Connect 4 MinMax算法