示例#1
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));
  }
示例#2
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;
   }
 }
示例#3
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;
    }
  }
示例#4
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;
    }
  }
示例#5
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;
  }
示例#6
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;
 }
示例#7
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());
    }
  }
示例#8
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;
  }
示例#9
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;
 }
示例#10
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);
 }
示例#11
0
 public Move getLastMove() {
   return m_position.getLastMove();
 }
示例#12
0
 public int getNextMoveNumber() {
   return (m_position.getPlyNumber() + 2) / 2;
 }
示例#13
0
 public int getCurrentMoveNumber() {
   return (m_position.getPlyNumber() + 1) / 2;
 }
示例#14
0
 public int getCurrentPly() {
   return m_position.getPlyNumber();
 }
示例#15
0
 private void setPosition(Position position) {
   m_position = position;
   m_position.addPositionChangeListener(this);
   m_cur = 0;
 }