评估功能不起作用

时间:2016-02-28 23:02:54

标签: java heuristics gomoku

我试图评估Gomoku Board(8x8),我必须连续获得5个。我似乎无法弄清楚为什么我的评估功能不起作用。是否有更简单的方法来编写它而不必编写if语句的负载?

public int evaluateBoard(Color[][] board) {
    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 - 5; ++j) {
            if (board[i][j] == Color.black) {
                black++;
                if (board[i][j + 1] == Color.BLACK) {
                    black++;
                    if (board[i][j + 2] == Color.BLACK) {
                        if (board[i][j + 3] == Color.BLACK) {
                            black++;
                            if (board[i][j + 4] == Color.BLACK) {
                                black++;
                            }
                        }
                    }
                }
            } else if (board[i][j] == Color.WHITE) {
                white++;
                if (board[i][j + 1] == Color.WHITE) {
                    white++;
                    if (board[i][j + 2] == Color.WHITE) {
                        white++;
                        if (board[i][j + 3] == Color.WHITE) {
                            white++;
                            if (board[i][j + 4] == Color.WHITE) {
                                white++;
                            }
                        }
                    }
                }
            }
        }
        System.out.println(black);
        score += scoreChange(black, white);
    }

    // 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 - 5; ++i) {
            if (board[i][j] == Color.black) {
                black++;
                if (board[i + 1][j] == Color.BLACK) {
                    black++;
                    if (board[i + 2][j] == Color.BLACK) {
                        black++;
                        if (board[i + 3][j] == Color.BLACK) {
                            black++;
                            if (board[i + 4][j] == Color.BLACK) {
                                black++;
                            }
                        }
                    }
                }
            } else if (board[i][j] == Color.WHITE) {
                white++;
                if (board[i + 1][j] == Color.WHITE) {
                    white++;
                    if (board[i + 2][j] == Color.WHITE) {
                        white++;
                        if (board[i + 3][j] == Color.WHITE) {
                            white++;
                            if (board[i + 4][j] == Color.WHITE) {
                                white++;
                            }
                        }
                    }
                }
            }
        }
        score += scoreChange(black, white);
    }

    int black = 0;
    int white = 0;
    // Check diagonal (Second)
    for (int i = 7, j = 0; i > 3; --i, ++j) {
        if (board[i][j] == Color.black) {
            black++;
            if (board[i - 1][j + 1] == Color.black) {
                black++;
                if (board[i - 2][j + 2] == Color.black) {
                    black++;
                    if (board[i - 3][j + 3] == Color.black) {
                        black++;
                        if (board[i - 4][j + 4] == Color.black) {
                            black++;
                        }
                    }
                }
            }
        } else if (board[i][j] == Color.white) {
            white++;
            if (board[i - 1][j + 1] == Color.white) {
                white++;
                if (board[i - 2][j + 2] == Color.white) {
                    white++;
                    if (board[i - 3][j + 3] == Color.white) {
                        white++;
                        if (board[i - 4][j + 4] == Color.white) {
                            white++;
                        }
                    }
                }
            }
        }
    }
    score += scoreChange(black, white);

    black = 0;
    white = 0;
    // Check Diagonal (First)
    for (int i = 0, j = 0; i < 4; ++i, ++j) {
        if (board[i][j] == Color.black) {
            black++;
            if (board[i + 1][j + 1] == Color.black) {
                black++;
                if (board[i + 2][j + 2] == Color.black) {
                    black++;
                    if (board[i + 3][j + 3] == Color.black) {
                        black++;
                        if (board[i + 4][j + 4] == Color.black) {
                            black++;
                        }
                    }
                }
            }
        } else if (board[i][j] == Color.white) {
            white++;
            if (board[i + 1][j + 1] == Color.white) {
                white++;
                if (board[i + 2][j + 2] == Color.white) {
                    white++;
                    if (board[i + 3][j + 3] == Color.white) {
                        white++;
                        if (board[i + 4][j + 4] == Color.white) {
                            white++;
                        }
                    }
                }
            }
        }
    }
    score += scoreChange(black, white);
    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;
}

0 个答案:

没有答案