Esempio n. 1
0
  private static CheckStatus analyseCheckAfterEnpassent(
      SimpleChessBoardInterface game, boolean whiteInCheck, Move lastMove) {
    final Position kingPos = game.getKingPosition(whiteInCheck);

    final List<Position> attackPositions = new ArrayList<Position>(2);
    final Figure attackFigure = game.getFigure(lastMove.to);

    Position passiveAttacker = getPassiveAttacker(game, kingPos, lastMove.from);
    if (passiveAttacker == null) {
      // an attackpath may have opend over the diagonal of the removed pawn
      Position removedPawnPos = Position.get(lastMove.from.row, lastMove.to.column);
      passiveAttacker = getPassiveAttacker(game, kingPos, removedPawnPos);
    }

    if (attackFigure.isReachable(kingPos, game)) {
      attackPositions.add(attackFigure.getPosition());
    }
    if (passiveAttacker != null) {
      attackPositions.add(passiveAttacker);
    }

    switch (attackPositions.size()) {
      case 0:
        return CheckStatus.NO_CHECK;
      case 1:
        return getPossiblePositions(kingPos, attackPositions.get(0));
      case 2:
        return CheckStatus.DOUBLE_CHECK;
      default:
        throw new IllegalStateException(
            "more than 2 attackers are impossible " + attackPositions.size());
    }
  }
Esempio n. 2
0
  private static CheckStatus analyseCheckAfterPawnTransform(
      SimpleChessBoardInterface game, boolean whiteInCheck, Move lastMove) {
    final Position kingPos = game.getKingPosition(whiteInCheck);
    final Figure transformedPawn = game.getFigure(lastMove.to);
    final List<Position> attackPositions = new ArrayList<Position>(2);
    final Position passiveAttacker = getPassiveAttacker(game, kingPos, lastMove.from);

    if (transformedPawn.isReachable(kingPos, game)) {
      attackPositions.add(lastMove.to);
    }
    if (passiveAttacker != null && !passiveAttacker.equalsPosition(lastMove.to)) {
      attackPositions.add(passiveAttacker);
    }

    switch (attackPositions.size()) {
      case 0:
        return CheckStatus.NO_CHECK;
      case 1:
        return getPossiblePositions(kingPos, attackPositions.get(0));
      case 2:
        return CheckStatus.DOUBLE_CHECK;
      default:
        throw new IllegalStateException(
            "more than 2 attackers are impossible " + attackPositions.size());
    }
  }
Esempio n. 3
0
  private static CheckStatus analyseCheckAfterRochade(
      SimpleChessBoardInterface game, boolean whiteInCheck, Move lastMove) {
    final Position kingPos = game.getKingPosition(whiteInCheck);

    final int rock_row = lastMove.to.row;
    final int rock_column = lastMove.to.column == 2 ? 3 : 5;
    final Position rockPos = Position.get(rock_row, rock_column);
    final Figure rock = game.getFigure(rockPos);

    if (rock.isReachable(kingPos, game)) return getPossiblePositions(kingPos, rockPos);
    return CheckStatus.NO_CHECK;
  }
Esempio n. 4
0
  public static CheckStatus analyseCheck(SimpleChessBoardInterface game, boolean whiteInCheck) {
    final Position kingPos = game.getKingPosition(whiteInCheck);
    final boolean isWhite = game.getFigure(kingPos).isWhite();
    final List<Position> attackPositions = new ArrayList<Position>(2);

    isCheckByBishopOrQueen(game, kingPos, attackPositions, isWhite);
    isCheckByKing(game, kingPos, attackPositions);
    isCheckByKnight(game, kingPos, attackPositions, isWhite);
    isCheckByPawn(game, kingPos, attackPositions, isWhite);
    isCheckByRockOrQueen(game, kingPos, attackPositions, isWhite);

    switch (attackPositions.size()) {
      case 0:
        return CheckStatus.NO_CHECK;
      case 1:
        return getPossiblePositions(kingPos, attackPositions.get(0));
      case 2:
        return CheckStatus.DOUBLE_CHECK;
      default:
        throw new IllegalStateException(
            "more than 2 attackers are impossible " + attackPositions.size());
    }
  }
Esempio n. 5
0
  public static CheckStatus analyseCheck(
      SimpleChessBoardInterface game, boolean whiteInCheck, ExtendedMove lastMove) {
    if (lastMove == null) {
      return analyseCheck(game, whiteInCheck);
    }

    if (lastMove.isEnpassent()) return analyseCheckAfterEnpassent(game, whiteInCheck, lastMove);
    if (lastMove.isRochade()) return analyseCheckAfterRochade(game, whiteInCheck, lastMove);
    if (lastMove.pawnTransformed())
      return analyseCheckAfterPawnTransform(game, whiteInCheck, lastMove);

    final Position kingPos = game.getKingPosition(whiteInCheck);
    final Figure movedFigure = game.getFigure(lastMove.to);
    final List<Position> attackPositions = new ArrayList<Position>(2);

    if (movedFigure.isReachable(kingPos, game)) {
      attackPositions.add(lastMove.to);
    }

    Position passiveAttacker = getPassiveAttacker(game, kingPos, lastMove.from);
    if (passiveAttacker != null) {
      attackPositions.add(passiveAttacker);
    }

    switch (attackPositions.size()) {
      case 0:
        return CheckStatus.NO_CHECK;
      case 1:
        return getPossiblePositions(kingPos, attackPositions.get(0));
      case 2:
        return CheckStatus.DOUBLE_CHECK;
      default:
        throw new IllegalStateException(
            "more than 2 attackers are impossible " + attackPositions.size());
    }
  }