0

Java の tic tac toe ゲームで、アルファ ベータ プルーニングを使用して minmax アルゴリズムを実装しようとしています。コーディングを終えるとすぐに例外を見つけたArrayIndexOutOfBoundsので、自分でエラーを見つけるためにターミナル出力をいくつか入れようとしましたが、最終的な戻り値の間違った結果が原因であることがわかりました: アルゴリズムは最終的に[-1][-1]スコア-2147483646を返し、それはコードの残りの部分が移動を試みて座標をフィールドに入れようとすると、例外が発生します。いくつかの動きと考えられるツリーをシミュレートするスキームを作成しましたが、バグが見つかりません。

   /*
   * int field[][] is the board array, it may contains 0(empty), 1(opponent's seed), 2(computer's seed)
   * nComputer = 2 (computer's seed)
   * nPlayer = 1 (opponent's seed)
   * computerMove = new int[3]
   * remainingMoves has been calculated before the main call
   */

   // Main call
   computerMove = cMove(remainingMoves, nComputer,Integer.MIN_VALUE + 1, Integer.MAX_VALUE - 1);
   field[computerMove[1]][computerMove[2]] = nComputer; // This line cause the exception!!
   // MinMax alpha-beta pruning algorithm
   private static int[] cMove(int depth, int player, int alpha, int beta) {

       int[][] moveList = new int[3][10];
       moveList = generateMoves(field); // See below for details

       int temp;
       int score;
       int bestR = -1;
       int bestC = -1;

       // check function retunrns 1(opponent wins), 2(computer wins), 0(draw) or -1(nothing)
       if(moveList[0][0] == 0 || depth == 0) {
           score = cScore(player);

           return new int[] { score, bestR, bestC };
       } else {
           for (int i = 1;i < moveList[0][0]; i++) {
               // Trying to make a move
               field[moveList[1][i]][moveList[2][i]] = player;

               if(player == nComputer) { // Maximazing player
                   score = cMove(depth -1, nPlayer, alpha, beta)[0];
                   if(score > alpha) {
                       alpha = score;
                       bestR = moveList[1][i];
                       bestC = moveList[2][i];
                   } 
               } else { // Minimizing player
                   score = cMove(depth -1, nComputer, alpha, beta)[0];
                   if(score < beta) {
                       beta = score;
                       bestR = moveList[1][i];
                       bestC = moveList[2][i];
                   } 
               }

               field[moveList[1][i]][moveList[2][i]] = 0; // Undo move
               if(alpha >= beta) i = 10; // Cut-off
           }

           if(player == nComputer) temp = alpha; 
           else temp = beta;

           return new int[] { temp, bestR, bestC };

       }
   }

   /*
   * generateMoves function returns an array 3x10 where [0][0] is the number
   * of possible moves and [0,1,2][1-9] are the score and the
   * coordinates(rows and columns) of all the possible moves
   */
   private static int[][] generateMoves(int[][] field) {
       int[][] result = new int[3][10];
       int k = 0;

       if(check(4) != -1) {
           return result;
       }

       for (int i = 0; i < field.length; i++) {
           for (int j = 0; j < field[0].length; j++) {
               if (field[i][j] == 0) {
                   k++;
                   result[1][k] = i;
                   result[2][k] = j;
               }
           }
       }

       result[0][0] = k;

       return result;
   }

   // cScore function assign a score for the actual node with an heuristic evaluation
private static int cScore(int p) {
    int score = 0;
    score += cRow(p, 0, 0, 0, 1, 0, 2);
    score += cRow(p, 1, 0, 1, 1, 1, 2);
    score += cRow(p, 2, 0, 2, 1, 2, 2);
    score += cRow(p, 0, 0, 1, 0, 2, 0);
    score += cRow(p, 0, 1, 1, 1, 2, 1);
    score += cRow(p, 0, 2, 1, 2, 2, 2);
    score += cRow(p, 0, 0, 1, 1, 2, 2);
    score += cRow(p, 0, 2, 1, 1, 2, 0);
    return score;
 }

private static int cRow(int player, int rOne, int cOne, int rTwo, int cTwo, int rThr, int cThr) {
    int score = 0;

    if (field[rOne][cOne] == nComputer) {
        score = 1;
    } else if (field[rOne][cOne] == nPlayer) {
        score = -1;
    }

    if (field[rTwo][cTwo] == nComputer) {
        if (score == 1) {
            score = 10;
        } else if (score == -1) {
            return 0;
        } else {
            score = 1;
        }
    } else if (field[rTwo][cTwo] == nPlayer) {
        if (score == -1) {
            score = -10;
        } else if (score == 1) {
            return 0;
        } else {
            score = -1;
        }
    }

    if (field[rThr][cThr] == nComputer) {
        if (score > 0) {
            score *= 10;
        } else if (score < 0) {
            return 0;
        } else {
            score = 1;
        }
    } else if (field[rThr][cThr] == nPlayer) {
        if (score < 0) {
            score *= 10;
        } else if (score > 1) {
            return 0;
        } else {
            score = -1;
        }
    }

    return score;
}

私はこの問題に 1 週​​間立ち往生していて、気が狂いそうです! 事前に感謝し、英語が下手で申し訳ありませんが、それは私の主な言語ではなく、ゆっくりと学習しようとしています

-------------------------------------------------- - - - - - - - -編集 - - - - - - - - - - - - - - - - - --------------------------

要求に応じてチェック機能を追加する:

// check function first check the state of 5 cells that needs to be filled to won([0,0][0,1][0,2][1,0][2,0])
public static int check(int nMove) {
    int state = -1;

    if(field[0][0] != 0) {
        state = col(0,1);
        if(state == 1 || state == 2) return state; // Win on first col
        state = row(0,1);
        if(state == 1 || state == 2) return state; // Win on first row
        state = diagonal(1);
        if(state == 1 || state == 2) return state; // Win on first diagonal
    }
    if (field[0][1] != 0) {
        state = col(1,2);
        if(state == 1 || state == 2) return state; // Win on second col
    }
    if (field[0][2] != 0) {
        state = col(2,3);
        if(state == 1 || state == 2) return state; // Win on third col
        state = diagonal(2);
        if(state == 1 || state == 2) return state; // Win on second diagonal
    }
    if (field[1][0] != 0) {
        state = row(1,2);
        if(state == 1 || state == 2) return state; // Win on second row
    }
    if (field[2][0] != 0) {
        state = row(2,3);
        if(state == 1 || state == 2) return state; // Win on third row
    }

    if(nMove == 8) return 0; // Draw

    return state;
}
// Check if the entire row is filled (check rows from starting to n points)
private static int row(int start, int n) {
    int s = -1;
    int k = 0;
    int h = 0;

    for (int i = start; i < n; i++) {
        for (int j = 0; j < (field[0]).length; j++) {
            if(field[i][j] == 1) {
                k++;
                if(k==3) s = 1;
            } else if(field[i][j] == 2) {
                    h++;
                    if(h==3) s = 2;
            }
        }
        k=0;
        h=0;
    }

    return s;
}
// Check if the entire col is filled (check cols from starting to n points)
private static int col(int start, int n) {
    int s = -1;
    int k = 0;
    int h = 0;

    for (int i = start; i < n; i++) {
        for (int j = 0; j < (field).length; j++) {
            if(field[j][i] == 1) {
                k++;
                if(k==3) s = 1;
            } else if(field[j][i] == 2) {
                    h++;
                    if(h==3) s = 2;
            }
        }
        k=0;
        h=0;
    }

    return s;
}
// Check if the entire diagonal is filled (check first diagonal if n=1 and second diagonal if n=2)
private static int diagonal(int n) {
    int s = -1;
    int k = 0;
    int h = 0;

    if(n == 1) {
        for (int i = 0; i < (field).length; i++) {
            int j = i;
            if(field[i][j]== 1) {
                k++;
                if(k==3) s = 1;
            } else if(field[i][j] == 2) {
                h++;
                if(h==3) s = 2;
            }
        }
    } else if (n == 2) {
        int j = 2;
        for (int i = 0; i < (field).length; i++) {
            if(field[i][j] == 1) {
                k++;
                if(k==3) s = 1;
            }
            else if(field[i][j] == 2) {
                h++;
                if(h==3) s = 2;
            }
            j--;
        }
    } else { }

    return s;
}
4

1 に答える 1

-1

ボードが 3x3 よりも大きいと仮定すると、それ以外の場合、勝利条件として 4 を使用したチクタクトゥはあまり意味がなく、ここで例外が発生します。

for (int i = 0; i < field.length; i++) {
       for (int j = 0; j < field[0].length; j++) {
           if (field[i][j] == 0) {
               k++;
               result[1][k] = i; // out of bounds
               result[2][k] = j; // out of bounds
           }
       }
   }

サイズ A x B のフィールドの場合、ボードが空の場合、k は と同じ大きさになりA*B - 1ます。これはA = B = 7、 で許可されている最大インデックスである 9 よりも大きい 48 になりresult[i]ます。

// ======================= 編集 ======================= =========
何を最適化しようとしているのか (コンピューターまたはプレーヤーのベスト スコアは?)、私は少し混乱していてよくわかりませんが、結果を説明するものを見つけました。

各再帰呼び出しには変数がありますplayer
その値に基づいて、alphaまたはを更新しますbeta
再帰ステップの最後に、の値に基づいてalphaorを返します。 ただし、次の場合に更新しますbetaplayer
alphaplayer == 2

if(player == 2) { // Maximazing player
    score = cMove(depth -1, nPlayer, alpha, beta)[0];
    if(score > alpha) {

ただし、次のbeta場合は返しますplayer == 2

if(player == 1) temp = alpha;
    else temp = beta;

したがって、常にMIN_VALUE + 1foralphaまたはMAX_VALUE - 1for を返しますbeta
したがって、基本ケースを呼び出さないすべてのステップでif(score < alpha) {orは常に false になります。if(score < beta) {再帰は次のようになります。

  • 深さ = 4、呼び出しの深さ = 3
    • 深さ = 3、呼び出しの深さ = 2
      • 深さ = 2、プレイヤー 1 が勝ち、スコアは 42
    • 深さ = 3、アルファ = 42 を更新、ベータ =MAX_VALUE - 1をスコアとして返す
  • MAX_VALUE - 1depth = 4、私のベータ版であるcall3 が返されたため、何も変更されずbestRbestC-1 のままです
于 2016-03-08T18:12:47.957 に答える