0
我是一名初學者,我試圖通過應用minimax算法來開發Connect4遊戲,我被困在決定它是最小玩家回合還是最大玩家回合的條件下。我有這種感覺,但我已經想了兩天,試圖找出答案。 有什麼幫助嗎?Java:正確使用條件語句
private int evaluatePlayerMove(int depth, int maxDepth, int col, int alpha, int beta) {
boardsAnalyzed++;
int evaluatedMove=0; // For evaluating min player move or max player move
int min = Integer.MAX_VALUE, minScore = 0; // For min player
int max = Integer.MIN_VALUE, maxScore = 0; // For max player
if (col != -1) {
// Check whether it's min player turn or max player turn
// If it's min player turn then evaluate min move:
if(//it's min player turn){
minScore = board.getHeuristicScore(Board.MARK_BLACK, col, depth, maxDepth);
if(board.blackWinFound()) {
blackWinFound = true;
return minScore;
}
if (depth == maxDepth) {
return minScore;
}
for (int c = 0; c < Board.COLUMNS; c++) {
if (board.isColumnAvailable(c)) {
board.mark(c, Board.MARK_RED);
int value = evaluatePlayerMove(depth + 1, maxDepth, c, alpha, beta);
board.unset(c);
if (value < min) {
min = value;
if (depth == 0) {
column = c;
}
}
if (value < beta) {
beta = value;
}
if (alpha >= beta) {
return beta;
}
}
}
if (min == Integer.MAX_VALUE) {
return 0;
}
evaluatedMove = min;
}
// If it's max player turn then evaluate max move:
if(//it's max player turn) {
maxScore = board.getHeuristicScore(Board.MARK_RED, col, depth, maxDepth);
if (board.redWinFound()) {
redWinFound = true;
return maxScore;
}
if (depth == maxDepth) {
return maxScore;
}
for (int c = 0; c < Board.COLUMNS; c++) {
if (board.isColumnAvailable(c)) {
board.mark(c, Board.MARK_BLACK);
int value = evaluatePlayerMove(depth + 1, maxDepth, c, alpha, beta);
board.unset(c);
if (value > max) {
max = value;
if (depth == 0) {
column = c;
}
}
if (value > alpha) {
alpha = value;
}
if (alpha >= beta) {
return alpha;
}
}
}
if (max == Integer.MIN_VALUE) {
return 0;
}
evaluatedMove= max;
}
}
return evaluatedMove;
}