Exemplo n.º 1
0
  public static LinkedHashMap<Dice, OperatorType> parseMany(String dts) {
    LinkedHashMap<Dice, OperatorType> ret = new LinkedHashMap<Dice, OperatorType>();
    ArrayList<String> parts = Utils.splitEncolosed(dts, OPENED_DICE, CLOSED_DICE);
    Dice dt = null;
    OperatorType ot = null;

    for (int i = 0; (i + 1) <= parts.size(); i += 2) {
      String part1 = parts.get(i);
      String part2 = (i + 1) < parts.size() ? parts.get(i + 1) : "";

      if (isDice(part1)) {
        if (dt == null) {
          dt = Dice.parse(part1);
        } else {
          ret.put(dt, OperatorType.Addition);
          dt = Dice.parse(part1);
          ot = null;
        }
      }

      if (isDice(part2)) {
        ret.put(dt, OperatorType.Addition);
        dt = Dice.parse(part2);
      } else {
        if (OperatorType.isOperator(part2)) {
          ot = OperatorType.getOperator(part2);
        }
        ret.put(dt, ot);
        dt = null;
      }
      ot = null;
    }
    return ret;
  }
Exemplo n.º 2
0
 public static String rollShowResults(String diceExpression) {
   LinkedHashMap<Dice, OperatorType> dice = parseMany(diceExpression);
   StringCompiler sb = new StringCompiler();
   Iterator<Map.Entry<Dice, OperatorType>> it = dice.entrySet().iterator();
   OperatorType op = null;
   Integer res = 0, roll = 0;
   sb.append("{");
   while (it.hasNext()) {
     Map.Entry<Dice, OperatorType> current = it.next();
     if (op != null) {
       roll = current.getKey().roll();
       res = op.doOperation(res, roll);
     } else {
       // First roll
       res = current.getKey().roll();
     }
     op = current.getValue();
     sb.append("[").append(roll).append(":").append(res).append("]");
     if (op != null) {
       sb.append(op.toString());
     }
   }
   sb.append("} = ").append(res);
   return sb.toString();
 }
Exemplo n.º 3
0
 public static Integer rollSum(LinkedHashMap<Dice, OperatorType> dice) {
   Integer ret = 0;
   Iterator<Map.Entry<Dice, OperatorType>> it = dice.entrySet().iterator();
   OperatorType op = null;
   while (it.hasNext()) {
     Map.Entry<Dice, OperatorType> current = it.next();
     if (op != null) {
       ret = op.doOperation(ret, current.getKey().roll());
     } else {
       // First roll
       ret = current.getKey().roll();
     }
     op = current.getValue();
   }
   return ret;
 }
Exemplo n.º 4
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;
  }