2017-05-10 2 views
0

알파 베타 제거 기능을 사용하여 Minimax를 구현하려고합니다. 이제 내 문제는 위치를 평가하고 반복에서 다음 이동으로 역 추적 (한 레벨 위로)하면 "currentBoard는"makeMove와 removeFigure 모두가 새로운 보드를 반환에도 불구하고, 초기 보드하지만 평가 잎에서 아닙니다. Minimax : 백 트랙킹을위한 보드 사본 저장

그래서 어떻게 올바른 되돌아을 위해 기존의 보드를 "저장"할 수 있습니까?

시 : 보드가 단순한 해시 맵이기 때문에 이동을 취소하는 대신 복사를 사용하고 싶습니다. 따라서이 방법이 더 쉬울 것 같아요.

여기에 있습니다.

public int alphaBeta(Board currentBoard, int depth, int alpha, int beta, boolean maximisingPlayer) { 
    int score; 
    if (depth == 0) { 
     return Evaluator.evaluateLeaf(whichColorAmI, currentBoard); 
    } 
    else if (maximisingPlayer) { 
     ArrayList<Move> possibleMoves= new ArrayList<Move>(); 
     possibleMoves=getPossibleMoves(whichColorAmI, currentBoard); 
     for (Move iterMoveForMe : possibleMoves) { 
      if(currentBoard.figureAt(iterMoveForMe.to)!=null){ 
       currentBoard = currentBoard.removeFigure(iterMoveForMe.to); 
      } 
      currentBoard= currentBoard.moveFigure(iterMoveForMe.from, iterMoveForMe.to); 
      score = alphaBeta(currentBoard, depth-1, alpha, beta, false); 
      if(score>=alpha){ 
       alpha=score; 
       if(depth==initialDepth){ 
        moveToMake=iterMoveForMe; 
       } 
      } 
      if (alpha>=beta) { 
       break; 
      } 
     } 
     return alpha; 
    } 
    else {[Minimizer...] 

}

+0

체스에 인공 지능을 쓰는 동안 나는 비슷한 문제가있었습니다. 내 솔 루션이 정확한지 확실하지 않지만 보드의 요소에 대한 방어적인 사본을 만들었습니다. –

답변

0

내가 할 수있는 방법을 발견 추측 : 내가 지금까지 가지고있는 코드입니다. 적어도 그것은 작동하는 것처럼 보입니다. 핵심은 for 루프 바로 다음에 복사본을 만들고 나중에이 복사본을 currentBoard 대신 사용하여 루프의 currentBoard가 수정되지 않도록하는 것입니다.

public int alphaBeta(Board currentBoard, int depth, int alpha, int beta, boolean maximisingPlayer) { 
    Display dis = new ConsoleDisplay(); 

    int score; 
    if (depth == 0) { 
     int evaluatedScore = Evaluator.evaluateLeaf(whichColorAmI, currentBoard); 
     return evaluatedScore; 
    } 
    else if (maximisingPlayer) { 
     ArrayList<Move> possibleMoves= new ArrayList<Move>(); 
     possibleMoves=getPossibleMoves(whichColorAmI, currentBoard); 
     for (Move iterMoveForMe : possibleMoves) { 
      Board copy = new Board(currentBoard.height, currentBoard.width,currentBoard.figures()); 
      if(copy.figureAt(iterMoveForMe.to)!=null){ 
       copy = currentBoard.removeFigure(iterMoveForMe.to); 
      } 
       copy= copy.moveFigure(iterMoveForMe.from, iterMoveForMe.to); 
      score = alphaBeta(copy, depth-1, alpha, beta, false); 

      if(score>=alpha){ 
       alpha=score; 
       if(depth==maxDepth){ 
        moveToMake=iterMoveForMe; 
       } 
      } 
      if (alpha>=beta) { 
        break; 
      } 
     } 
     return alpha; 
    } 
    else {