コード例 #1
0
ファイル: Moves.java プロジェクト: Tha1n/ESIR2
  /**
   * 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
      }
    }
  }