예제 #1
0
  private static boolean isCheckByPawn(
      final BasicChessGameInterface game,
      final Position kingPos,
      final List<Position> attackerPos,
      final boolean isWhite) {
    final int possiblePawnRow = isWhite ? kingPos.row + 1 : kingPos.row - 1;

    if (possiblePawnRow < 0 || possiblePawnRow > 7) return false;
    if (kingPos.column != 0) {
      final Position pos = Position.get(possiblePawnRow, kingPos.column - 1);
      final Figure figure = game.getFigure(pos);
      if (figure != null && figure.isWhite() != isWhite && figure.isPawn()) {
        attackerPos.add(pos);
        return true;
      }
    }

    if (kingPos.column != 7) {
      final Position pos = Position.get(possiblePawnRow, kingPos.column + 1);
      final Figure figure = game.getFigure(pos);
      if (figure != null && figure.isWhite() != isWhite && figure.isPawn()) {
        attackerPos.add(pos);
        return true;
      }
    }

    return false;
  }
예제 #2
0
  private static boolean isCheckByRockOrQueen(
      final BasicChessGameInterface game,
      final Position kingPos,
      final List<Position> attackerPos,
      final boolean isWhite) {
    if (isDoubleHorizontalCheckAfterPawnTransformation(game, kingPos, attackerPos, isWhite))
      return true;

    // da die Bauerntransformation jetzt ausgeschlossen ist,
    // darf nach dem Fund eines Angreifer aufgehört werden
    for (int row = kingPos.row + 1; row < 8; row++) { // vertikale Reihe
      final Position pos = Position.get(row, kingPos.column);
      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isRock() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }
    for (int row = kingPos.row - 1; row >= 0; row--) { // vertikale Reihe
      final Position pos = Position.get(row, kingPos.column);
      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isRock() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }
    for (int column = kingPos.column + 1; column < 8; column++) { // horizontale Reihe
      final Position pos = Position.get(kingPos.row, column);
      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isRock() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }
    for (int column = kingPos.column - 1; column >= 0; column--) { // horizontale Reihe
      final Position pos = Position.get(kingPos.row, column);
      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isRock() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }
    return false;
  }
예제 #3
0
  private static boolean isDoubleHorizontalCheckAfterPawnTransformation(
      final BasicChessGameInterface game,
      final Position kingPos,
      final List<Position> attackerPos,
      final boolean isWhite) {
    // only possible if the king stod in the columnshadow of a pawn which transformed in the last
    // move to rock or queen
    final int groundRow = isWhite ? 0 : 7;
    if (kingPos.row != groundRow) return false;

    Position kingSideAttackerPos = null;

    if (kingPos.column != 0) {
      final Position leftKingPos = Position.get(groundRow, kingPos.column - 1);
      final Figure leftFromKingFigure = game.getFigure(leftKingPos);
      if (leftFromKingFigure != null
          && leftFromKingFigure.isWhite() != isWhite
          && (leftFromKingFigure.isQueen() || leftFromKingFigure.isRock())) {
        kingSideAttackerPos = leftKingPos;
      }
    }

    if (kingPos.column != 7 && kingSideAttackerPos == null) {
      final Position rightKingPos = Position.get(groundRow, kingPos.column + 1);
      final Figure rightFromKingFigure = game.getFigure(rightKingPos);
      if (rightFromKingFigure != null
          && rightFromKingFigure.isWhite() != isWhite
          && (rightFromKingFigure.isQueen() || rightFromKingFigure.isRock())) {
        kingSideAttackerPos = rightKingPos;
      }
    }

    if (kingSideAttackerPos == null) return false;

    final int rowStep = isWhite ? 1 : -1;
    final int toRow = isWhite ? 7 : 0;

    int row = groundRow;
    do {
      row += rowStep;
      final Position pos = Position.get(row, kingPos.column);
      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      // either figure says 'chess' or king is not in danger
      if (figure.isWhite() != isWhite && (figure.isRock() || figure.isQueen())) {
        attackerPos.add(kingSideAttackerPos);
        attackerPos.add(pos);
        return true;
      }
    } while (row != toRow);

    return false;
  }
예제 #4
0
  private static boolean isCheckByKnight(
      final BasicChessGameInterface game,
      final Position kingPos,
      final List<Position> attackerPos,
      final boolean isWhite) {
    final int minRow = Math.max(0, kingPos.row - 2);
    final int maxRow = Math.min(7, kingPos.row + 2);
    final int minColumn = Math.max(0, kingPos.column - 2);
    final int maxColumn = Math.min(7, kingPos.column + 2);

    for (int row = minRow; row <= maxRow; row++) {
      for (int column = minColumn; column <= maxColumn; column++) {
        final int vertical_dif = Math.abs(kingPos.row - row);
        final int horizontal_dif = Math.abs(kingPos.column - column);
        if (vertical_dif + horizontal_dif == 3) {
          final Position pos = Position.get(row, column);
          final Figure figure = game.getFigure(pos);
          if (figure != null && figure.isWhite() != isWhite && figure.isKnight()) {
            attackerPos.add(pos);
            return true;
          }
        }
      }
    }
    return false;
  }
예제 #5
0
  private static boolean isCheckByBishopOrQueen(
      final BasicChessGameInterface game,
      final Position kingPos,
      final List<Position> attackerPos,
      final boolean isWhite) {
    int column, row;

    column = kingPos.column + 1;
    row = kingPos.row + 1;
    while (column < 8 && row < 8) {
      final Position pos = Position.get(row, column);
      row++;
      column++;

      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isBishop() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }

    column = kingPos.column - 1;
    row = kingPos.row + 1;
    while (column >= 0 && row < 8) {
      final Position pos = Position.get(row, column);
      row++;
      column--;

      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isBishop() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }

    column = kingPos.column + 1;
    row = kingPos.row - 1;
    while (column < 8 && row >= 0) {
      final Position pos = Position.get(row, column);
      row--;
      column++;

      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isBishop() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }

    column = kingPos.column - 1;
    row = kingPos.row - 1;
    while (column >= 0 && row >= 0) {
      final Position pos = Position.get(row, column);
      row--;
      column--;

      final Figure figure = game.getFigure(pos);
      if (figure == null) continue;
      if (figure.isWhite() == isWhite) break;
      if (figure.isBishop() || figure.isQueen()) {
        attackerPos.add(pos);
        return true;
      }
      break;
    }
    return false;
  }