/**
   * Helper method to fetch all moves for given direction. Useful for Bishop, Rook and Queen.
   *
   * @param moveX X direction to move in
   * @param moveY Y diretion to move in
   * @return ArrayList with possible moves
   */
  protected Set<Square> getMovesForDirection(int moveX, int moveY) {
    Set<Square> list = new HashSet<>();

    for (int h = piece.getSquare().getPozX(), i = piece.getSquare().getPozY();
        !piece.isOut(h, i);
        h += moveX, i += moveY) // right-down
    {
      if (piece.getSquare().getPozX() == h && piece.getSquare().getPozY() == i) {
        continue; // omit same square
      }
      Square sq = piece.getChessboard().getSquare(h, i);
      if (null == sq.getPiece()
          || piece.getPlayer() != sq.getPiece().getPlayer()) // if on this sqhuare isn't piece
      {
        list.add(piece.getChessboard().getSquare(h, i));

        if (piece.otherOwner(h, i)) {
          break;
        }
      } else {
        break; // we've to break becouse we cannot go beside other piece!!
      }
    }
    return list;
  }
Exemple #2
0
  public void addMove(
      Square begin,
      Square end,
      boolean registerInHistory,
      Castling castlingMove,
      boolean wasEnPassant,
      Piece promotedPiece) {
    boolean wasCastling = castlingMove != Castling.NONE;
    String locMove = begin.getPiece().getSymbol();

    if (game.getSettings().isUpsideDown()) {
      locMove +=
          Character.toString(
              (char)
                  ((Chessboard.getBottom() - begin.getPozX())
                      + 97)); // add letter of Square from which move was made
      locMove +=
          Integer.toString(begin.getPozY() + 1); // add number of Square from which move was made
    } else {
      locMove +=
          Character.toString(
              (char) (begin.getPozX() + 97)); // add letter of Square from which move was made
      locMove +=
          Integer.toString(8 - begin.getPozY()); // add number of Square from which move was made
    }

    if (end.piece != null) {
      locMove += "x"; // take down opponent piece
    } else {
      locMove += "-"; // normal move
    }

    if (game.getSettings().isUpsideDown()) {
      locMove +=
          Character.toString(
              (char)
                  ((Chessboard.getBottom() - end.getPozX())
                      + 97)); // add letter of Square to which move was made
      locMove += Integer.toString(end.getPozY() + 1); // add number of Square to which move was made
    } else {
      locMove +=
          Character.toString(
              (char) (end.getPozX() + 97)); // add letter of Square to which move was made
      locMove += Integer.toString(8 - end.getPozY()); // add number of Square to which move was made
    }

    if (begin.getPiece().getSymbol().equals("")
        && begin.getPozX() - end.getPozX() != 0
        && end.piece == null) {
      locMove += "(e.p)"; // pawn take down opponent en passant
      wasEnPassant = true;
    }
    if ((!this.enterBlack && this.game.getChessboard().getKingBlack().isChecked())
        || (this.enterBlack
            && this.game.getChessboard().getKingWhite().isChecked())) { // if checked

      if ((!this.enterBlack
              && this.game.getChessboard().getKingBlack().isCheckmatedOrStalemated() == 1)
          || (this.enterBlack
              && this.game.getChessboard().getKingWhite().isCheckmatedOrStalemated()
                  == 1)) { // check if checkmated
        locMove += "#"; // check mate
      } else {
        locMove += "+"; // check
      }
    }
    if (castlingMove != Castling.NONE) {
      this.addCastling(castlingMove.getSymbol());
    } else {
      this.move.add(locMove);
      this.addMove2Table(locMove);
    }
    this.scrollPane.scrollRectToVisible(new Rectangle(0, this.scrollPane.getHeight() - 2, 1, 1));

    if (registerInHistory) {

      AbstractMove moveToAdd =
          new Move(
              this.game.getChessboard(),
              new Square(begin),
              new Square(end),
              begin.piece,
              end.piece,
              castlingMove,
              wasEnPassant,
              promotedPiece);
      this.moveBackStack.add(moveToAdd);
    }
  }