Exemplo n.º 1
0
  @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;
  }
Exemplo n.º 2
0
 /**
  * Private helper method to deal with taking moves to the right and left to the pawn.
  *
  * <p>This adds the possible move(s) to the passed list in case the passed file valid. Adds
  * nothing if the passed file outside the board.
  *
  * @param moveList The {@code List list} the moves are added to.
  * @param targetFile The target file index.
  * @param isPromoting Flag stating whether we are promoting at the moment. This could be
  *     calculated by the method itself but was calculated in the calling context.
  */
 private void addPossibleTakingMove(
     final List<Move> moveList, final int targetFile, final boolean isPromoting) {
   if (ChessRules.fileWithinBounds(targetFile)) {
     final ChessCoord tarCoord =
         Coords.coord(targetFile, this.getCoord().getRank() + this.rankStep);
     final Piece targetCoordPiece = this.getPos().getPieceAt(tarCoord);
     // Check whether there is an opposing piece at the target
     // coordinate.
     if (targetCoordPiece != null && (!this.getColor().equals(targetCoordPiece.getColor()))) {
       if (isPromoting) {
         addPromotionMoves(moveList, tarCoord);
       } else {
         moveList.add(new SimpleTakingMove(this.getCoord(), tarCoord));
       }
     }
     // Also check for en passant moves.
     else if (this.getCoord().getRank() == this.enPassantRank
         && this.getPos().getEnpassantFile() == targetFile) {
       moveList.add(new EnPassantMove(this.getCoord(), tarCoord));
     }
   }
 }