0

I have implemented a Gomoku Player class that uses alpha beta pruning to select the best possible move for the computer, but I am having trouble working out how to write the evaluation function to correctly score each position on the board.

I have an 'evaluateBoard' method that evaluates the board position and updates the number of blacks, whites or blank positions in each row, column and diagonal. My 'scoreChange' method scores each position, and this is where I feel my program is making errors as it does not block me when I have a 3 or 4 in a row.

How can I evaluate and score each position correctly?

public int evaluateBoard(Color[][] board, Color computer) {
    int score = 0;
    //Check all the rows
    for (int i = 0; i < 8; ++i) {
        int blank = 0;
        int black = 0;
        int white = 0;
        for (int j = 0; j < 8; ++j) {
            if (board[i][j] == null) {
                blank++;
            } else if (board[i][j] == Color.black) {
                black++;
            } else {
                white++;
            }
        } 
        score+=scoreChange(black, white, computer); 
    }

    //Check all the columns
    for (int j = 0; j < 8; ++j) {
        int blank = 0;
        int black = 0;
        int white = 0;
        for (int i = 0; i < 8; ++i) {
            if (board[i][j] == null) {
                blank++;
            } else if (board[i][j] == Color.black) {
                black++;
            } else {
                white++;
            } 
        }
        score+=scoreChange(black, white, computer);
    }

    int blank = 0;
    int black = 0;
    int white = 0;
    //Check diagonal (first)
    for (int i = 0, j = 0; i < 8; ++i, ++j) {
        if (board[i][j] == Color.black) {
            black++;
        } else if (board[i][j] == Color.white) {
            white++;
        } else {
            blank++;
        }
    }
    score+=scoreChange(black, white, computer);

    blank = 0;
    black = 0;
    white = 0;
    //Check Diagonal (Second)
    for (int i = 7, j = 0; i > -1; --i, ++j) {
        if (board[i][j] == Color.black) {
            black++;
        } else if (board[i][j] == Color.white) {
            white++;
        } else {
            blank++;
        }
    }
    score+=scoreChange(black, white, computer);
    return score;
}

private int scoreChange(int black, int white){
    int change;
    if (black == 5) {
        change = -10000;
    } else if (black == 4 && white == 0) {
        change = -1000;
    } else if (black == 3 && white == 0) {
        change = -100;
    } else if (black == 2 && white == 0) {
        change = -10;
    } else if (black == 1 && white == 0) {
        change = -1;
    } else if (white == 5) {
        change = 10000;
    } else if (white == 4 && black == 0) {
        change = 1000;
    } else if (white == 3 && black == 0) {
        change = 100;
    } else if (white == 2 && black == 0) {
        change = 10;
    } else if (white == 1 && black == 0) {
        change = 1;
    } else {
        change = 0;
    } 
    return change;
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
batussi
  • 55
  • 1
  • 6
  • Years ago, when I wrote a similar program for college, I had the square board in one structure, but there was also another structure with a layer of indirection that the evaluate function used that was a 192x5. So A1..A5 was in the first row, A2..A6 in the second and so forth. That was the structure that the AI used to consider (and yes, a play to A2 would 'look' like a play in three locations (two horizontal, one vertical, one diagonal)). –  Mar 05 '15 at 22:32
  • So should I store all possible winning combinations in a two dimensional array and then see which position is close to making a full row, column or diagonal, and also check if the opponent is close to making one too? – batussi Mar 06 '15 at 07:03

0 Answers0