コード例 #1
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  /**
   * Generate nextMove according to Minimax algorithm
   *
   * @param currPlayer
   * @param globaldepth
   * @return
   */
  public Move getMiniMaxMove(Player currPlayer, int globaldepth) {
    // System.out.println("In getMiniMaxMove ");
    MiniMaxNode rootNode =
        new MiniMaxNode(
            mancalaBoard.getBoard(),
            currPlayer.playerNum,
            0,
            Integer.MIN_VALUE,
            null,
            new Move(-1),
            false);

    MiniMaxTree gameTree = new MiniMaxTree();
    gameTree.insert(rootNode, null);
    gameTree =
        miniMaxHelperWithGO2(
            gameTree, rootNode, currPlayer, mancalaBoard.getBoard(), 0, globaldepth, true);
    // System.out.println("Out");
    MiniMaxNode root = gameTree.getRoot();
    MiniMaxNode retNode = evaluateGameTree(root, 0, 2);

    Move retMove = new Move();
    // Selecting from roots child nodes - Maximizing
    for (MiniMaxNode childNode : retNode.getChildrenList()) {
      // System.out.println(childNode.moveIndex.getMoveIndex() + " : " + childNode.getEvalVal());
      if (retNode.getEvalVal() < childNode.getEvalVal()) {
        retNode.setEvalVal(childNode.getEvalVal());
        retMove = childNode.getMoveIndex();
      }
    }

    // sort and get the minimum value of moveindex
    // System.out.println("the next move is with moveindex : "+  retMove.getMoveIndex());
    return (retMove);
  }
コード例 #2
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  public MiniMaxTree miniMaxHelperWithGO2(
      MiniMaxTree gameTree,
      MiniMaxNode rootNode,
      Player currPlayer,
      int[] tempBoard,
      int tempDepth,
      int globalDepth,
      boolean checkParentsFt) {

    System.out.println();
    // System.out.println("In miniMaxHelperWithGO2 ");
    // TODO : CHECK THE DEPTH
    if (tempDepth >= globalDepth && !checkParentsFt) {
      return gameTree;
    }

    MiniMaxNode currParent = rootNode;

    if (!checkParentsFt) {
      if (currPlayer.equals(player1)) {
        currPlayer = player2;
      } else {
        currPlayer = player1;
      }
    }

    int startIndex = 0;
    int endIndex = mancalaBoard.getPitSize();
    if (currPlayer.playerNum == 2) {
      startIndex = mancalaBoard.getPitSize() + 1;
      endIndex = mancalaBoard.getBoardSize() - 1;
    }

    // boolean checkParentsFt = false;
    int i = startIndex;
    for (i = startIndex; i < endIndex; i++) {

      Move currMove = new Move(i);
      if (isIllegalMove(currMove, tempBoard)) {
        continue;
      } else {
        BoardFTCheck bft = makeTempMiniMaxMoveWithGO2(currMove, currPlayer, tempBoard);

        int mydepth = currParent.depth;
        if (mydepth == 0) {
          mydepth++;
        }

        if (!checkParentsFt) {
          mydepth++;
        }

        int evalFunc;
        // Odds are minimizers
        if (mydepth % 2 == 1) {
          evalFunc = Integer.MAX_VALUE;
        } else {
          evalFunc = Integer.MIN_VALUE;
        }

        // if we reached the leaf find the evaluation right away
        if (mydepth == globalDepth || bft.leadsToGameOver) {
          displayHelper(bft.tempBoard);

          // If I dont get a free turn at last level then only evaluate
          if (!bft.freeTurn) {
            evalFunc = getEvalFunc(currPlayer, bft.tempBoard);
          } else {
            // last level is already toggled
            if (evalFunc == Integer.MAX_VALUE) {
              evalFunc = Integer.MIN_VALUE;
            } else {
              evalFunc = Integer.MAX_VALUE;
            }
          }
        }

        MiniMaxNode newNode =
            new MiniMaxNode(
                bft.tempBoard,
                currPlayer.playerNum,
                mydepth,
                evalFunc,
                currParent,
                currMove,
                bft.freeTurn);

        // displayHelper(bft.tempBoard);
        /*System.out.println("Node to insert is ");
        displayHelper(newNode.nodeBoard);
        System.out.println("£££££££££££££££££££££££££££");
        System.out.println("In insert tree");
        System.out.println("£££££££££££££££££££££££££££");*/

        gameTree.insert(newNode, currParent);

        // gameTree.displayTree();

        /*System.out.println("return from insert tree");
        System.out.println();*/

        if (bft.freeTurn) {
          miniMaxHelperWithGO2(
              gameTree, newNode, currPlayer, bft.tempBoard, mydepth, globalDepth, true);
        } else {
          miniMaxHelperWithGO2(
              gameTree, newNode, currPlayer, bft.tempBoard, mydepth, globalDepth, false);
        }
      }
    }
    return gameTree;
  }