Ejemplo n.º 1
0
  /**
   * Method to set all moves from String with validation test (usefoul for network game)
   *
   * @param moves String to set in String like PGN with full-notation format
   */
  public void setMoves(String moves) {
    int from = 0;
    int to = 0;
    int n = 1;
    ArrayList<String> tempArray = new ArrayList();
    int tempStrSize = moves.length() - 1;
    while (true) {
      from = moves.indexOf(" ", from);
      to = moves.indexOf(" ", from + 1);
      try {
        tempArray.add(moves.substring(from + 1, to).trim());
      } catch (StringIndexOutOfBoundsException exc) {
        LOG.error("setMoves/StringIndexOutOfBoundsException: error parsing file to load: " + exc);
        break;
      }
      if (n % 2 == 0) {
        from = moves.indexOf(".", to);
        if (from < to) {
          break;
        }
      } else {
        from = to;
      }
      n += 1;
      if (from > tempStrSize || to > tempStrSize) {
        break;
      }
    }
    for (String locMove : tempArray) // test if moves are written correctly
    {
      if (!Moves.isMoveCorrect(locMove.trim())) // if not
      {
        JOptionPane.showMessageDialog(this.game, Settings.lang("invalid_file_to_load") + move);
        return; // show message and finish reading game
      }
    }
    boolean canMove = false;
    for (String locMove : tempArray) {
      if (Castling.isCastling(locMove)) // if castling
      {
        int[] values = new int[4];
        if (locMove.equals(Castling.LONG_CASTLING.getSymbol())) {
          if (this.game.getActivePlayer().getColor() == Colors.BLACK) // if black turn
          {
            values = new int[] {4, 0, 2, 0}; // move value for castling (King move)
          } else {
            values = new int[] {4, 7, 2, 7}; // move value for castling (King move)
          }
        } else if (locMove.equals(Castling.SHORT_CASTLING.getSymbol())) // if short castling
        {
          if (this.game.getActivePlayer().getColor() == Colors.BLACK) // if black turn
          {
            values = new int[] {4, 0, 6, 0}; // move value for castling (King move)
          } else {
            values = new int[] {4, 7, 6, 7}; // move value for castling (King move)
          }
        }
        canMove = this.game.simulateMove(values[0], values[1], values[2], values[3]);

        if (!canMove) // if move is illegal
        {
          JOptionPane.showMessageDialog(this.game, Settings.lang("illegal_move_on") + locMove);
          return; // finish reading game and show message
        }
        continue;
      }
      from = 0;
      int num = locMove.charAt(from);
      if (num <= 90 && num >= 65) {
        from = 1;
      }
      int xFrom =
          9; // set to higher value than chessboard has fields, to cause error if piece won't be
             // found
      int yFrom = 9;
      int xTo = 9;
      int yTo = 9;
      boolean pieceFound = false;
      if (locMove.length() <= 3) {
        Square[][] squares = this.game.getChessboard().getSquares();
        xTo = locMove.charAt(from) - 97; // from ASCII
        yTo = Chessboard.getBottom() - (locMove.charAt(from + 1) - 49); // from ASCII
        for (int i = 0; i < squares.length && !pieceFound; i++) {
          for (int j = 0; j < squares[i].length && !pieceFound; j++) {
            if (squares[i][j].piece == null
                || this.game.getActivePlayer().getColor()
                    != squares[i][j].getPiece().getPlayer().getColor()) {
              continue;
            }
            Set<Square> pieceMoves = squares[i][j].getPiece().getAllMoves();
            for (Object square : pieceMoves) {
              Square currSquare = (Square) square;
              if (currSquare.getPozX() == xTo && currSquare.getPozY() == yTo) {
                xFrom = squares[i][j].getPiece().getSquare().getPozX();
                yFrom = squares[i][j].getPiece().getSquare().getPozY();
                pieceFound = true;
              }
            }
          }
        }
      } else {
        xFrom = locMove.charAt(from) - 97; // from ASCII
        yFrom = Chessboard.getBottom() - (locMove.charAt(from + 1) - 49); // from ASCII
        xTo = locMove.charAt(from + 3) - 97; // from ASCII
        yTo = Chessboard.getBottom() - (locMove.charAt(from + 4) - 49); // from ASCII
      }
      canMove = this.game.simulateMove(xFrom, yFrom, xTo, yTo);
      if (!canMove) // if move is illegal
      {
        JOptionPane.showMessageDialog(this.game, Settings.lang("illegal_move_on") + locMove);
        this.game.getChessboard().resetActiveSquare();
        return; // finish reading game and show message
      }
    }
  }
Ejemplo n.º 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);
    }
  }