Example #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;
  }
Example #2
0
 public static ArrayList<Integer> roll(Dice... dice) {
   ArrayList<Integer> ret = new ArrayList<Integer>();
   for (Dice die : dice) {
     ret.add(die.roll());
   }
   return ret;
 }
Example #3
0
 public void actionPerformed(ActionEvent e) {
   if ("roll".equals(e.getActionCommand())) {
     if (DiceCalc.lcd.getText().endsWith("d")
         || DiceCalc.lcd.getText().endsWith("+")
         || DiceCalc.lcd.getText().endsWith("-")
         || DiceCalc.lcd.getText().isEmpty()) {
       nope();
       return;
     }
     if (displayingResult) {
       /* If the result is already being displayed,
       re-roll the expression from memory. */
       result = Dice.parse(expression, "", 0);
       DiceCalc.lcd.setText(Integer.toString(result));
       return;
     }
     expression = DiceCalc.lcd.getText();
     writingDie = false;
     displayingResult = true;
     result = Dice.parse(DiceCalc.lcd.getText(), "", 0);
     DiceCalc.lcd.setText(Integer.toString(result));
     return;
   } else if ("clr".equals(e.getActionCommand())) {
     writingDie = false;
     displayingResult = false;
     DiceCalc.lcd.setText("");
     return;
   } else if ("d".equals(e.getActionCommand())) {
     if (DiceCalc.lcd.getText().endsWith("d")
         || DiceCalc.lcd.getText().endsWith("+")
         || DiceCalc.lcd.getText().endsWith("-")
         || DiceCalc.lcd.getText().isEmpty()
         || displayingResult
         || writingDie) {
       nope();
       return;
     }
     DiceCalc.lcd.setText(DiceCalc.lcd.getText() + 'd');
     writingDie = true;
     return;
   } else if ("+".equals(e.getActionCommand())
       || "-".equals(e.getActionCommand())
       || "0".equals(e.getActionCommand())) {
     if (DiceCalc.lcd.getText().endsWith("d")
         || DiceCalc.lcd.getText().endsWith("+")
         || DiceCalc.lcd.getText().endsWith("-")
         || DiceCalc.lcd.getText().isEmpty()
         || displayingResult) {
       nope();
       return;
     } else {
       writingDie = false;
       DiceCalc.lcd.setText(DiceCalc.lcd.getText() + e.getActionCommand());
       return;
     }
   } else { // doesn't meet any other condition, so it must be a digit
     // clear the screen if a result is being displayed
     if (displayingResult) DiceCalc.lcd.setText("");
     writingDie = false;
     displayingResult = false;
     DiceCalc.lcd.setText(DiceCalc.lcd.getText() + e.getActionCommand());
     return;
   }
 }
  // method that rolls the 3 dices and returns the amount of winning.
  public double play() {
    String diceFace = ""; // holds the dice symbol for each throw of the dice.
    Dice dice = new Dice();
    clearResultDisplay(); // Clear the StringBuilder object that stores the results of each dice
                          // throw.

    // rolls 3 dice 1x for a single bet game, 2x for a double bet game and 3x for a triple bet game.
    for (int x = 0; x < rollNumber; x++) {
      // counters to keep track of the number of times a symbol appeared on a dice face.
      int crownCtr = 0, anchorCtr = 0, diamondCtr = 0, heartCtr = 0, spadeCtr = 0, clubCtr = 0;
      // accumulators to store winnings for a particular bet on a dice symbol.
      double crownWin = 0d,
          anchorWin = 0,
          diamondWin = 0,
          heartWin = 0d,
          spadeWin = 0d,
          clubWin = 0;
      // holds an asterisk mark (*) for the symbols that appeared on the dice face.
      String crownCheck = "",
          anchorCheck = "",
          diamondCheck = "",
          heartCheck = "",
          spadeCheck = "",
          clubCheck = "";
      buildResultDisplay("\nDice Rolled...");
      buildResultDisplay(
          "\n-----------------------------------------------------------------------------------\n"
              + "          |Crown     |Anchor    |Diamond   |Heart     |Spade     |Club\n"
              + "-----------------------------------------------------------------------------------\n");
      // roll a dice object 3x to simulate the roll of 3 dices.
      for (int y = 0; y <= 2; y++) {
        dice.throwDice();
        diceFace = dice.getDice();
        switch (diceFace) {
          case "crown":
            crownCtr++;
            crownCheck = crownCheck + "*";
            if (getCrownBet()
                > 0) { // calculate winning if the a bet was placed on the crown symbol.
              switch (crownCtr) {
                case 1: // crown symbol appeared for the first time.
                  crownWin += (getCrownBet() * 2);
                  break;
                case 2: // crown symbol appeared for the second time.
                  crownWin += getCrownBet();
                  break;
                case 3: // crown symbol appeared for the third time.
                  crownWin += getCrownBet();
                  break;
              } // end case
            } // end if
            break;
          case "anchor":
            anchorCtr++;
            anchorCheck = anchorCheck + "*";
            if (getAnchorBet() > 0) {
              switch (anchorCtr) {
                case 1: // anchor symbol appeared for the first time.
                  anchorWin += (getAnchorBet() * 2);
                  break;
                case 2: // anchor symbol appeared for the second time.
                  anchorWin += getAnchorBet();
                  break;
                case 3: // anchor symbol appeared for the third time.
                  anchorWin += getAnchorBet();
                  break;
              } // end case
            } // end if
            break;
          case "diamond":
            diamondCtr++;
            diamondCheck = diamondCheck + "*";
            if (getDiamondBet() > 0) {
              switch (diamondCtr) {
                case 1: // diamond symbol appeared for the first time.
                  diamondWin += (getDiamondBet() * 2);
                  break;
                case 2: // diamond symbol appeared for the second time.
                  diamondWin += getDiamondBet();
                  break;
                case 3: // diamond symbol appeared for the third time.
                  diamondWin += getDiamondBet();
                  break;
              } // end case
            } // end if
            break;
          case "heart":
            heartCtr++;
            heartCheck = heartCheck + "*";
            if (getHeartBet() > 0) {
              switch (heartCtr) {
                case 1: // heart symbol appeared for the first time.
                  heartWin += (getHeartBet() * 2);
                  break;
                case 2: // heart symbol appeared for the second time.
                  heartWin += getHeartBet();
                  break;
                case 3: // heart symbol appeared for the third time.
                  heartWin += getHeartBet();
                  break;
              } // end case
            } // end if
            break;
          case "spade":
            spadeCtr++;
            spadeCheck = spadeCheck + "*";
            if (getSpadeBet() > 0) {
              switch (spadeCtr) {
                case 1: // spade symbol appeared for the first time.
                  spadeWin += (getSpadeBet() * 2);
                  break;
                case 2: // spade symbol appeared for the second time.
                  spadeWin += getSpadeBet();
                  break;
                case 3: // spade symbol appeared for the third time.
                  spadeWin += getSpadeBet();
                  break;
              } // end case
            } // end if
            break;
          case "club":
            clubCtr++;
            clubCheck = clubCheck + "*";
            if (getClubBet() > 0) {
              switch (clubCtr) {
                case 1: // club symbol appeared for the first time.
                  clubWin += (getClubBet() * 2);
                  break;
                case 2: // club symbol appeared for the second time.
                  clubWin += getClubBet();
                  break;
                case 3: // club symbol appeared for the third time.
                  clubWin += getClubBet();
                  break;
              } // end case
            } // end if
            break;
        } // end switch
      } // end for

      // show the player's bets.
      buildResultDisplay(
          String.format(
              "%-9s %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f",
              "Bet/s  -> |",
              getCrownBet(),
              getAnchorBet(),
              getDiamondBet(),
              getHeartBet(),
              getSpadeBet(),
              getClubBet()));
      buildResultDisplay(
          "\n-----------------------------------------------------------------------------------\n");

      // Remove bets for dice face symbols that did not win and roll bets for symbols that did.
      if (crownCtr == 0) { // the crown symbol never appeared at all.
        if (getCrownBet() > 0) { // clear bet.
          setCrownBet(0d);
        }
      } else { // the crown symbol appeared at least once.
        if (getCrownBet() > 0) { // roll bet.
          setCrownBet(crownWin);
        }
      } // end if

      if (anchorCtr == 0) {
        if (getAnchorBet() > 0) { // clear bet.
          setAnchorBet(0d);
        }
      } else {
        if (getAnchorBet() > 0) { // roll bet.
          setAnchorBet(anchorWin);
        }
      } // end if

      if (diamondCtr == 0) {
        if (getDiamondBet() > 0) { // clear bet.
          setDiamondBet(0d);
        }
      } else {
        if (getDiamondBet() > 0) { // roll bet.
          setDiamondBet(diamondWin);
        }
      } // end if

      if (heartCtr == 0) {
        if (getHeartBet() > 0) { // clear bet.
          setHeartBet(0d);
        }
      } else {
        if (getHeartBet() > 0) { // roll bet.
          setHeartBet(heartWin);
        }
      } // end if

      if (spadeCtr == 0) {
        if (getSpadeBet() > 0) { // clear bet.
          setSpadeBet(0d);
        }
      } else {
        if (getSpadeBet() > 0) { // roll bet.
          setSpadeBet(spadeWin);
        }
      } // end if

      if (clubCtr == 0) {
        if (getClubBet() > 0) { // clear bet.
          setClubBet(0d);
        }
      } else {
        if (getClubBet() > 0) { // roll bet.
          setClubBet(clubWin);
        }
      } // end if

      // show symbols that appeared on the dice face.
      buildResultDisplay(
          String.format(
              "%-9s %-10s %-10s %-10s %-10s %-10s %-10s",
              "Result -> |",
              crownCheck,
              anchorCheck,
              diamondCheck,
              heartCheck,
              spadeCheck,
              clubCheck));
      buildResultDisplay(
          "\n-----------------------------------------------------------------------------------\n");
      // show the bet/winnings.
      buildResultDisplay(
          String.format(
              "%-9s %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f",
              "Win    -> |",
              getCrownBet(),
              getAnchorBet(),
              getDiamondBet(),
              getHeartBet(),
              getSpadeBet(),
              getClubBet()));
      buildResultDisplay(
          "\n-----------------------------------------------------------------------------------\n");
    } // end for

    return (getCrownBet()
        + getAnchorBet()
        + getDiamondBet()
        + getHeartBet()
        + getSpadeBet()
        + getClubBet());
  } // end play