protected int quiescence(Position position, int alpha, int beta, boolean maxTurn)
      throws IllegalMoveException {
    int evaluation = handleTerminal(position, maxTurn).eval;

    if (maxTurn) {
      if (evaluation >= beta) return beta;
      else alpha = evaluation;
    } else {
      if (evaluation <= alpha) return alpha;
      else beta = evaluation;
    }

    MoveValuePair bestMove = new MoveValuePair();
    bestMove.updateMinMax((short) 0, evaluation, maxTurn);
    LinkedList<MoveValuePair> sortedMoves = getCapturingSortedMoves(position, maxTurn);
    for (MoveValuePair movepair : sortedMoves) {
      short move = movepair.move;
      // collect values from further moves
      // get and update transposition table if possible
      position.doMove(move);
      int value = quiescence(position, alpha, beta, !maxTurn);
      bestMove.updateMinMax(move, value, maxTurn);
      position.undoMove();
      // update the alpha beta boundary
      if (maxTurn) alpha = bestMove.eval;
      else beta = bestMove.eval;
      // prune the subtree if needed
      if (alpha >= beta) return bestMove.eval;
    }
    return bestMove.eval;
  }
 @Override
 protected MoveValuePair ABMaxMinValue(
     Position position, int depth, int alpha, int beta, boolean maxTurn)
     throws IllegalMoveException {
   if (depth <= 0 || position.isTerminal()) {
     return handleTerminal(position, maxTurn, alpha, beta);
   } else {
     MoveValuePair bestMove = new MoveValuePair();
     LinkedList<MoveValuePair> sortedMoves = getSortedMoves(position, maxTurn);
     for (MoveValuePair movepair : sortedMoves) {
       short move = movepair.move;
       // collect values from further moves
       // get and update transposition table if possible
       position.doMove(move);
       if (this.p2tte.containsKey(position.getHashCode())
           && (this.p2tte.get(position.getHashCode()).depth >= depth)) {
         // System.out.println("trans table at level: " + depth);
         TransTableEntry tte = p2tte.get(position.getHashCode());
         bestMove.updateMinMax(move, tte.eval, maxTurn);
       } else {
         // recursive method
         MoveValuePair childMove = ABMaxMinValue(position, depth - 1, alpha, beta, !maxTurn);
         bestMove.updateMinMax(move, childMove.eval, maxTurn);
         p2tte.put(position.getHashCode(), new TransTableEntry(childMove.eval, depth, move));
       }
       position.undoMove();
       // update the alpha beta boundary
       if (maxTurn) alpha = bestMove.eval;
       else beta = bestMove.eval;
       // prune the subtree if needed
       if (alpha >= beta) return bestMove;
     }
     return bestMove;
   }
 }
  //    protected MoveValuePair handleTerminal(Position position, boolean maxTurn) {
  //        MoveValuePair finalMove = new MoveValuePair();
  //        if (position.isTerminal() && position.isMate()) {
  //            this.terminalFound = position.isTerminal();
  //            finalMove.eval = (maxTurn ? BE_MATED : MATE);
  //        } else if (position.isTerminal() && position.isStaleMate())
  //            finalMove.eval = 0;
  //        else {
  //            finalMove.eval = (int) ( (position.getMaterial() + position.getDomination()));
  //        }
  ////        System.out.print(finalMove.eval + " ");
  //        return finalMove;
  //    }
  protected LinkedList<MoveValuePair> getCapturingSortedMoves(Position position, boolean maxTurn)
      throws IllegalMoveException {
    LinkedList<MoveValuePair> sortedMoves = new LinkedList<MoveValuePair>();
    short[] moves = position.getAllCapturingMoves();
    MoveValuePair theMove = null;
    ASCENDING = maxTurn ? false : true;

    for (short move : moves) {
      position.doMove(move);
      if (p2tte.containsKey(position.getHashCode())) {
        theMove = new MoveValuePair(move, p2tte.get(position.getHashCode()).eval);
      } else {
        // for max turn, I assign worst values those unvisited positions
        //                theMove = new MoveValuePair(move, maxTurn ? BE_MATED : MATE);
        int eval = (int) ((maxTurn ? -1 : 1) * (position.getMaterial() + position.getDomination()));
        theMove = new MoveValuePair(move, eval);
      }
      position.undoMove();
      sortedMoves.add(theMove);
    }
    Collections.sort(
        sortedMoves,
        new Comparator<MoveValuePair>() {
          @Override
          public int compare(MoveValuePair c1, MoveValuePair c2) {
            //                System.out.println(c1.eval + " vs " + c2.eval);
            return (int) ((ASCENDING ? 1 : -1) * Math.signum(c1.eval - c2.eval)); // use your logic
          }
        });
    return sortedMoves;
  }
Esempio n. 4
0
 public Move getNextMove(int whichLine) {
   short shortMove = m_moves.getMove(m_moves.goForward(m_cur, whichLine));
   if (shortMove == GameMoveModel.NO_MOVE) return null; // =====>
   try {
     m_position.setNotifyListeners(false);
     m_position.doMove(shortMove);
     //            ChMove move = m_position.getLastMove(shortMove);
     Move move = m_position.getLastMove();
     m_position.undoMove();
     m_position.setNotifyListeners(true);
     return move;
   } catch (IllegalMoveException ex) {
     ex.printStackTrace();
     return null;
   }
 }
Esempio n. 5
0
  private boolean goBackInLine(boolean silent) {
    if (DEBUG) System.out.println("goBackInLine");

    int index = m_moves.goBack(m_cur, false);
    if (index != -1) {
      m_cur =
          index; // needs to be set before undoing the move to allow listeners to check for curNode
      m_ignoreNotifications = true;
      if (silent) m_position.setNotifyListeners(false);
      m_position.undoMove();
      if (silent) m_position.setNotifyListeners(true);
      m_ignoreNotifications = false;
      return true;
    } else {
      return false;
    }
  }
Esempio n. 6
0
  private boolean goBack(boolean silent) {
    if (DEBUG) System.out.println("goBack");

    int index = m_moves.goBack(m_cur, true);
    if (index != -1) {
      //        if (m_position.canUndoMove()) {  // do not rely on position since in silent mode it
      // is not updated
      //            m_cur = m_moves.goBack(m_cur, true);
      m_cur = index;
      m_ignoreNotifications = true;
      if (silent) m_position.setNotifyListeners(false);
      m_position.undoMove();
      if (silent) m_position.setNotifyListeners(true);
      m_ignoreNotifications = false;
      return true;
    } else {
      return false;
    }
  }
Esempio n. 7
0
  public Move[] getMainLine() {
    int num = 0;
    int index = m_cur;
    while (m_moves.hasNextMove(index)) {
      index = m_moves.goForward(index);
      num++;
    }

    Move[] moves = new Move[num];
    for (int i = 0; i < num; i++) {
      moves[i] = goForwardAndGetMove(true);
    }

    m_position.setNotifyListeners(false);
    for (int i = 0; i < moves.length; i++) m_position.undoMove();
    m_position.setNotifyListeners(true);

    return moves;
  }
Esempio n. 8
0
 public Move[] getNextMoves() {
   m_position.setNotifyListeners(false);
   Move[] moves = new Move[m_moves.getNumOfNextMoves(m_cur)];
   for (int i = 0; i < moves.length; i++) {
     short move = m_moves.getMove(m_moves.goForward(m_cur, i));
     try {
       m_position.doMove(move);
       //                moves[i] = m_position.getLastMove(move);
       moves[i] = m_position.getLastMove();
       m_position.undoMove();
     } catch (IllegalMoveException ex) {
       m_moves.write(System.out);
       System.out.println("cur = " + m_cur + " move=" + GameMoveModel.valueToString(move));
       ex.printStackTrace();
     }
   }
   m_position.setNotifyListeners(true);
   return moves;
 }