Ejemplo n.º 1
0
  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;
  }
Ejemplo n.º 2
0
 protected MoveValuePair handleTerminal(Position position, boolean maxTurn, int alpha, int beta)
     throws IllegalMoveException {
   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 = quiescence(position, alpha, beta, !maxTurn);
   }
   //        System.out.print(finalMove.eval + " ");
   return finalMove;
 }
Ejemplo n.º 3
0
  public void gotoPosition(ImmutablePosition pos, boolean silent) {
    if (m_position.equals(pos)) return; // =====>

    int curNode = getCurNode();
    gotoStart(true);
    do {
      if (m_position.equals(pos)) {
        int posNode = getCurNode();
        gotoNode(curNode, true);
        gotoNode(posNode, silent);
        return; // =====>
      }
    } while (goForward(true));
  }
Ejemplo 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;
   }
 }
Ejemplo n.º 5
0
 /**
  * Paint all chess pieces according to the given Chesspresso Position.
  *
  * @param chessGame
  */
 void paintChessPieces(Position chessGame) {
   for (int row = 0; row < 8; ++row) {
     for (int col = 0; col < 8; ++col) {
       paintChessPiece(row, col, chessGame.getStone(row * 8 + col));
     }
   }
 }
Ejemplo n.º 6
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;
    }
  }
Ejemplo n.º 7
0
  //    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;
  }
Ejemplo n.º 8
0
 @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;
   }
 }
Ejemplo n.º 9
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;
 }
Ejemplo n.º 10
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;
  }
Ejemplo n.º 11
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;
    }
  }
Ejemplo n.º 12
0
 /**
  * Paint all chess pieces according to the given Chesspresso Position.
  *
  * @param chessGame
  */
 void paintChessPieces(Position chessGame) {
   if (chessSet.hasMovablePieces()) {
     chessSet.syncToPosition(chessGame, this);
   } else {
     for (int row = 0; row < 8; ++row) {
       for (int col = 0; col < 8; ++col) {
         paintChessPiece(row, col, chessGame.getStone(row * 8 + col));
       }
     }
   }
 }
Ejemplo n.º 13
0
  private void setModel(GameModel gameModel) {
    m_model = gameModel;
    m_header = gameModel.getHeaderModel();
    m_moves = gameModel.getMoveModel();

    String fen = m_header.getTag(PGN.TAG_FEN);
    if (fen != null) {
      setPosition(new Position(fen, false));
    } else {
      setPosition(Position.createInitialPosition());
    }
  }
Ejemplo n.º 14
0
  private boolean goForward(int whichLine, boolean silent) {
    if (DEBUG) System.out.println("goForward " + whichLine);

    int index = m_moves.goForward(m_cur, whichLine);
    short shortMove = m_moves.getMove(index);
    if (DEBUG) System.out.println("  move = " + Move.getString(shortMove));
    if (shortMove != GameMoveModel.NO_MOVE) {
      try {
        m_cur = index;
        m_ignoreNotifications = true;
        if (silent) m_position.setNotifyListeners(false);
        m_position.doMove(shortMove);
        if (silent) m_position.setNotifyListeners(true);
        m_ignoreNotifications = false;
        return true;
      } catch (IllegalMoveException ex) {
        ex.printStackTrace();
      }
    } else {
      //            new Exception("Forward at end of line").printStackTrace();
    }
    return false;
  }
Ejemplo n.º 15
0
 /**
  * Returns whether the given position occurs in the main line of this game.
  *
  * @param position the position to look for, must not be null
  * @return whether the given position occurs in the main line of this game
  */
 public boolean containsPosition(ImmutablePosition position) {
   boolean res = false;
   int index = getCurNode();
   gotoStart(true);
   for (; ; ) {
     if (m_position.getHashCode() == position.getHashCode()) {
       res = true;
       break;
     }
     if (!hasNextMove()) break;
     goForward(true);
   }
   gotoNode(index, true);
   return res;
 }
Ejemplo n.º 16
0
 @Override
 public short getMove(Position position) throws IllegalMoveException {
   long start = System.currentTimeMillis();
   short result = minimaxIDS(position, Config.IDS_DEPTH);
   long elapsedTime = System.currentTimeMillis() - start;
   try {
     FileOutputStream timecompete = new FileOutputStream("timecompete.txt", true);
     timecompete.write((elapsedTime / 1000. + "\t").getBytes());
     timecompete.close();
   } catch (FileNotFoundException ex) {
     System.out.println("FileNotFoundException : " + ex);
   } catch (IOException ioe) {
     System.out.println("IOException : " + ioe);
   }
   System.out.println("ABPTOQS  making move " + elapsedTime / 1000. + "\t");
   Config.tryBreakTie(position.getToPlay(), result);
   return result;
 }
Ejemplo n.º 17
0
 public int getCurrentPly() {
   return m_position.getPlyNumber();
 }
Ejemplo n.º 18
0
 private void setPosition(Position position) {
   m_position = position;
   m_position.addPositionChangeListener(this);
   m_cur = 0;
 }
Ejemplo n.º 19
0
 public int getNextMoveNumber() {
   return (m_position.getPlyNumber() + 2) / 2;
 }
Ejemplo n.º 20
0
 public Move getLastMove() {
   return m_position.getLastMove();
 }
Ejemplo n.º 21
0
 /**
  * Method to traverse the game in postfix order (first the lines, then the main line). This method
  * is used by {@link chesspresso.pgn.PGN}.
  *
  * @param listener the listener to receive event when arriving at nodes
  * @param withLines whether or not to include lines of the current main line.
  */
 public void traverse(GameListener listener, boolean withLines) {
   int index = getCurNode();
   gotoStart(true);
   traverse(listener, withLines, m_position.getPlyNumber(), 0);
   gotoNode(index, true);
 }
Ejemplo n.º 22
0
 public int getCurrentMoveNumber() {
   return (m_position.getPlyNumber() + 1) / 2;
 }