예제 #1
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;
  }
예제 #2
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;
  }