コード例 #1
0
ファイル: Pawn.java プロジェクト: TobiasScharnowski/Chess
 @Override
 public boolean controls(ChessCoord coord) {
   /*
    * The pawn controls the field if it is diagonally in front of him. This
    * means there has to be an absolute distance of 1 in file direction.
    * And one step towards the regular moving direction.
    */
   return this.getCoord().getRank() + this.rankStep == coord.getRank()
       && Math.abs(this.getCoord().getFile() - coord.getFile()) == 1;
 }
コード例 #2
0
ファイル: Pawn.java プロジェクト: TobiasScharnowski/Chess
  @Override
  protected List<Move> _generateMoves() {
    // Collect some information to shorten the code later.
    final List<Move> result = new ArrayList<>();
    final ChessCoord currentCoord = this.getCoord();
    final int currentFile = currentCoord.getFile();
    final int currentRank = currentCoord.getRank();
    final int singleStepTargetRank = currentRank + rankStep;
    final boolean isPromoting = singleStepTargetRank == this.promotionRank;

    // First check for the normal forward moves
    ChessCoord tarCoord = Coords.coord(currentFile, singleStepTargetRank);
    // Single step
    if (this.getPos().getPieceAt(tarCoord) == null) {
      if (isPromoting) {
        this.addPromotionMoves(result, tarCoord);
      } else {
        result.add(new SimpleMove(currentCoord, tarCoord));
      }
    }

    // Double step
    if (currentRank == this.startRank) {
      tarCoord = Coords.coord(currentFile, currentRank + FIRST_MOVE_FACTOR * rankStep);
      if (this.getPos().getPieceAt(tarCoord) == null) {
        // We don't bother about checking for being promoted because
        // that would mean that the pawn has a one-move promotion, which
        // really does not make any sense. This would break this code
        // though.
        result.add(new SimpleMove(currentCoord, tarCoord));
      }
    }

    // Now deal with taking moves
    // Lower file (left)
    addPossibleTakingMove(result, currentFile - 1, isPromoting);
    // Higher file (right)
    addPossibleTakingMove(result, currentFile + 1, isPromoting);

    return result;
  }