Esempio n. 1
0
  public static Dice parse(String dt) {
    Dice ret = null;
    try {
      dt = dt.toUpperCase();
      String[] diceparts = dt.split(DICE_TOKEN_S);
      if (diceparts.length == 2) {
        int nThrows = Integer.valueOf(diceparts[0]), nFaces = 0, modifier = 0;
        String nFacesBld = "";
        OperatorType opType = null;
        char[] faceParts = diceparts[1].toCharArray();
        for (char c : faceParts) {
          String current = String.valueOf(c);
          if (Utils.isInteger(current)) {
            nFacesBld = nFacesBld.concat(current);
          } else if ((opType = OperatorType.parseOperator(c)) != null) {
            break;
          } else {
            break;
          }
        }
        nFaces = Integer.valueOf(nFacesBld);

        if (opType != null) {
          String[] bonusParts = diceparts[1].split(opType.toEscapedString());
          if ((bonusParts.length == 2) && Utils.isInteger(bonusParts[1])) {
            modifier = Integer.valueOf(bonusParts[1]);
          }
          ret = new Dice(nThrows, nFaces, opType, modifier);
        } else {
          ret = new Dice(nThrows, nFaces);
        }
      }
    } catch (NumberFormatException e) {
      e.printStackTrace();
      ret = null;
    }
    return ret;
  }