public void rethrow() {
   if (canRethrow())
     for (Die die : getDice()) {
       if (!isSelected(die)) die.roll();
     }
   myCurrentAttempts++;
 }
示例#2
0
  /** sets the amoebes to random squares, two for each player */
  private void setAmoebesToSquares() {
    Die die = dieProvider.get();
    for (int i = 0; i < this.players.length; i++) {
      Game.Color color = this.players[i].getColor();
      boolean statusOK =
          false; // AK this sounds a bit over-dramatic, "freeSquare" seems more appropriate
      int randomRow, randomCol;
      do {
        randomRow =
            die.roll(1, 5)
                - 1; // AK could you maybe implement a `choose(List<Square> from) : Square` function
                     // that
        randomCol =
            die.roll(1, 5)
                - 1; // guarantees that even if I'm unlucky (or have badly mocked), always halts?
        statusOK = Board.board[randomRow][randomCol].getAmoebesList().size() == 0;
      } while (!statusOK);
      Amoebe amo1 = AmoebeFactory.get(this.compassProvider, color);
      board[randomRow][randomCol].enterSquare(amo1);

      statusOK =
          false; // AK don't repeat yourselves, you could have done the same with a simple for-loop
                 // here.
      do { // remember that more code is more place for error
        randomRow = die.roll(1, 5) - 1;
        randomCol = die.roll(1, 5) - 1;
        statusOK = Board.board[randomRow][randomCol].getAmoebesList().size() == 0;
      } while (!statusOK);
      Amoebe amo2 = AmoebeFactory.get(this.compassProvider, color);
      board[randomRow][randomCol].enterSquare(amo2);
    }
  }
示例#3
0
 public static void main(String [] args) {
     Scanner sc = new Scanner(System.in);
     System.out.print("Number of sides: ");
     int sides = sc.nextInt()
     Die d = new Die(sides);
     System.out.println("Alea iacta est: " + d.roll());
 }
示例#4
0
  /** The beginnings of a super cool dungeon hack simulator */
  public static void main(String[] args) {
    Weapon s = new Weapon("Broad Sword", "slashes", 10);
    Weapon c = new Weapon("Arm", "Claws", 6);
    Actor player = new Actor("Conan the Barbarian", 5, 10, s);
    Actor monster = new Actor("Grumpy Troll", 5, 8, c);
    Die d20 = new Die(20);

    // player gets first attack :-)
    int roll = d20.roll();
    System.out.printf("[Roll=%d|AC=%d] ", roll, monster.armorClass());
    System.out.printf(
        "%s %s his %s at the %s and ",
        player.name(), player.weapon().action(), player.weapon().name(), monster.name());
    if (roll >= monster.armorClass()) {
      int damage = player.weapon().hitDamage();
      monster.takeDamage(damage);
      System.out.printf("hits for %d damage", damage);
      if (monster.isDead()) {
        System.out.printf(", killing it");
      }
    } else {
      System.out.printf("misses");
    }
    System.out.println("!");
  }
示例#5
0
 @Test
 public void test_dice_can_be_marked() {
   Die die = new Die();
   assertFalse(die.isMarked());
   die.mark();
   assertTrue(die.isMarked());
 }
示例#6
0
文件: Client.java 项目: catph/base1
  public static void main(String[] args) {
    die1 = Die.getInstance(1);
    die2 = Die.getInstance(2);

    die1.dice();
    die2.dice();
  }
示例#7
0
  /** Takes the dieSet stored in the rollResult object and rolls its constituent dice */
  public void rollDice() {
    // initialize variables
    int temp = 0;
    int result = 0;
    ArrayList<Integer> results = new ArrayList<Integer>();

    // for each die in the die set
    for (Die i : rolled.getDice()) {
      // for each iteration under the constraint of the coefficient
      for (int j = 0; j < i.getCoefficient(); j++) {
        // get the value of the die and generate a random number
        // between 1 and its value
        temp = gen.nextInt(i.getValue()) + 1;

        // add the randomly generated result to the result
        result += temp;

        // and to the array
        results.add(temp);
      }
    }

    // after all the dice have been iterated through, add the modifier
    result += rolled.getModifier();

    // store the local variables into the instance variables
    this.result = result;
    this.results = results;
  }
示例#8
0
  private void initComponents(String title) {
    getContentPane().setLayout(new BorderLayout());

    JPanel mainPane = new JPanel(new BorderLayout());
    mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    JLabel text = new JLabel(title, JLabel.CENTER);
    text.setFont(FONT);
    text.setOpaque(true);
    text.setBackground(Color.orange);
    //		text.setFont(UIManager.getFont("Label.font"));
    mainPane.add(text, "North");

    JPanel panel = new JPanel(new GridLayout(1, 6));
    for (int i = 1; i <= 6; i++) {
      Die die = template.getCopy();
      die.setFace(i);
      FaceButton button = new FaceButton(die, i);
      panel.add(button);
    }
    mainPane.add(panel, "South");

    if (icon != null) {
      JLabel iconLabel = new JLabel(icon);
      iconLabel.setBorder(BorderFactory.createEtchedBorder());
      mainPane.add(iconLabel, "Center");
    }

    getContentPane().add(mainPane, "Center");
    setResizable(false);
    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    pack();
  }
示例#9
0
 @Test
 public void testRollDiceIsRandom() {
   RandomMock r = new RandomMock();
   Die d = new Die(r);
   r.setFace(2);
   d.roll();
   assertEquals(2, d.getValue());
   r.setFace(3);
   d.roll();
   assertEquals(3, d.getValue());
 }
示例#10
0
 public static void main(String[] args) {
   int antal;
   Scanner scan = new Scanner(System.in);
   System.out.print("Number of sides: ");
   antal = scan.nextInt();
   Die Dice = new Die(antal);
   Die Dice2 = new Die(antal);
   System.out.println("slag ifrån tärning1:" + Dice.roll());
   System.out.println("slag ifrån tärning2:" + Dice2.roll());
   System.out.println(Dice);
   System.out.println(Dice2);
 }
  // Draws this die when rolling with a random number of dots
  private void drawRolling(Graphics g) {
    int x = xCenter - dieSize / 2 + (int) (3 * Math.random()) - 1;
    int y = yCenter - dieSize / 2 + (int) (3 * Math.random()) - 1;
    g.setColor(Color.RED);

    if (x % 2 != 0) g.fillRoundRect(x, y, dieSize, dieSize, dieSize / 4, dieSize / 4);
    else g.fillOval(x - 2, y - 2, dieSize + 4, dieSize + 4);

    Die die = new Die();
    die.roll();
    drawDots(g, x, y, die.getNumDots());
  }
 // Starts this die rolling
 public void roll() {
   super.roll();
   int width = tableRight - tableLeft;
   int height = tableBottom - tableTop;
   xCenter = tableLeft;
   yCenter = tableTop + height / 2;
   xSpeed = width * (Math.random() + 1) * speedFactor;
   ySpeed = height * (Math.random() - .5) * 2. * speedFactor;
 }
示例#13
0
  public static void main(String[] args) {
    Die die = new Die();
    int p1 = 0;
    int p2 = 0;

    while (p1 <= 100 && p2 <= 100) {
      die.roll();
      p1 += die.getResult();
      if (p1 <= 100) {
        p2 += die.getResult();
      }
    }
    if (p1 > 100) {
      System.out.println("Spelare ett vann!");
    } else {
      System.out.println("Spelare två vann!");
    }
  }
示例#14
0
 public static void main(String[] args) {
   Scanner userInput = new Scanner(System.in);
   Die die1 = new Die();
   Die die2 = new Die();
   die1.roll();
   die1.print();
   die2.roll();
   die2.print();
 }
示例#15
0
 public void throwDie(Die die) {
   int currentThrow;
   presskey = new Scanner(System.in);
   System.out.print("Press Enter to throw your die!");
   try {
     System.in.read();
   } catch (IOException e) {
     e.printStackTrace();
   }
   presskey.nextLine();
   currentThrow = die.roll();
   //		this.point += currentThrow;
   this.roundPoint = currentThrow;
   System.out.println("You get " + currentThrow + " points.");
   System.out.println();
 }
示例#16
0
  public static void rollDice(Player player) {
    // roll dice
    player.setLastRoll(dice[0].Roll(), dice[1].Roll());

    System.out.println("Dice rolling...");
    Util.pause((long) (Util.getRandom() * 3000 + 1000));
    System.out.println("Dice settling...");
    Util.pause((long) (Util.getRandom() * 1500 + 500));

    System.out.println("*" + player.getName() + " rolled " + player.getLastTotalRoll() + "*\n");

    Die.display(dice);

    System.out.println();
    if (dice[0].getLastRoll() == dice[1].getLastRoll()) {
      System.out.println("*" + player.getName() + " rolled " + "a double!*");
    }

    System.out.println();
  }
示例#17
0
  /**
   * Method to actually do the calculations of the attack. First grab the dice that were used, do
   * the computations against the operations presented and then deal out damage. Then if an opponent
   * dies, generate a new opponent or if there is none go to the reward screen.
   *
   * @param view view of the application
   */
  public void attackClicked(View view) {
    currentElement = "Light";
    // Generate all of the stuff we need to calculate damage
    // Create the list of the dice in the first row
    ArrayList<Die> firstRow = new ArrayList<Die>();
    // Create the list of dice in the second row
    ArrayList<Die> secondRow = new ArrayList<Die>();
    // Find which dice was used in the first row first position
    ImageButton firstRowFirstDice = (ImageButton) findViewById(R.id.firstRowFirstDice);
    // Find which dice was used in the first row second position
    ImageButton firstRowSecondDice = (ImageButton) findViewById(R.id.firstRowSecondDice);
    // Find which dice was used in the second row first slot
    ImageButton secondRowFirstDice = (ImageButton) findViewById(R.id.secondRowFirstDice);
    // Find which dice was used in the second row second slot
    ImageButton secondRowSecondDice = (ImageButton) findViewById(R.id.secondRowSecondDice);
    if ((firstRowFirstDice.getTag() == null)
        || (firstRowSecondDice.getTag() == null)
        || (secondRowFirstDice.getTag() == null)
        || (secondRowSecondDice.getTag() == null)) {
      Intent healthLow = new Intent(this, HealthTooLowActivity.class);
      healthLow.putExtra("player", player);
      healthLow.putExtra("dice", true);
      startActivityForResult(healthLow, 200);
    } else {

      // Get the value of the dice as a string
      String frfd = firstRowFirstDice.getTag().toString();
      String frsd = firstRowSecondDice.getTag().toString();
      String srfd = secondRowFirstDice.getTag().toString();
      String srsd = secondRowSecondDice.getTag().toString();
      // Find the text view for the first answer
      TextView firstRowAns = (TextView) findViewById(R.id.firstRowFirstAns);
      // Find the text view for the second answer
      TextView secondRowAns = (TextView) findViewById(R.id.secondRowAns);
      TextView firstRowSecondOp = (TextView) findViewById(R.id.firstRowSecondOp);
      TextView secondRowSecondOp = (TextView) findViewById(R.id.secondRowSecondOp);
      boolean isDead = false;
      diceUsed = player.getDiceUsed();
      playerProgressBar = (ProgressBar) findViewById(R.id.playerProgressBar);
      opProgressBar = (ProgressBar) findViewById(R.id.oppProgressBar);
      generateOps = false;
      TextView firstRowFirstOp = (TextView) findViewById(R.id.firstRowFirstOp);
      TextView secondRowFirstOp = (TextView) findViewById(R.id.secondRowFirstOp);
      int firstRowFirstDiceRoll = 0;
      int firstRowSecondDiceRoll = 0;
      int secondRowFirstDiceRoll = 0;
      int secondRowSecondDiceRoll = 0;

      // Loop through the dice used array list to figure out which dice was used in which row
      for (int i = 0; i < diceUsed.size(); i++) {
        // If the dice selected equals the string value of the first row first position dice and the
        // first row array list is less than 2, add it to the first row list
        if (diceUsed.get(i).getDiceType().equals(frfd) && firstRow.size() < 2) {
          firstRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the first row second position and the first row
          // array list is less than 2, add it to the first row list
        } else if (diceUsed.get(i).getDiceType().equals(frsd) && firstRow.size() < 2) {
          firstRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the second row first dice and that list is less than
          // two, add it to the second row list
        } else if (diceUsed.get(i).getDiceType().equals(srfd) && secondRow.size() < 2) {
          secondRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the second row second dice and that list is less
          // than two, add it to the second row list
        } else if (diceUsed.get(i).getDiceType().equals(srsd) && secondRow.size() < 2) {
          secondRow.add(diceUsed.get(i));
        }
      }
      int firstRowTotal = 0;
      int secondRowTotal = 0;

      // For every dice in the first row list, have them roll dice and add it to the total
      for (Die d : firstRow) {
        if (firstRow.indexOf(d) == 0) {
          firstRowFirstDiceRoll = d.rollDice();
          firstRowTotal += firstRowFirstDiceRoll;
        } else {
          if (firstRowFirstOp.getText().equals("+")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal += firstRowSecondDiceRoll;
          } else if (firstRowFirstOp.getText().equals("-")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal - firstRowSecondDiceRoll;

          } else if (firstRowFirstOp.getText().equals("*")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal * firstRowSecondDiceRoll;
          } else {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal / firstRowSecondDiceRoll;
          }
        }
      }
      // For every dice in the second row list, have them roll dice and add it to the total
      for (Die d : secondRow) {
        if (secondRow.indexOf(d) == 0) {
          secondRowFirstDiceRoll = d.rollDice();
          secondRowTotal += secondRowFirstDiceRoll;
        } else {
          if (secondRowFirstOp.getText().equals("+")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal += secondRowSecondDiceRoll;
          } else if (secondRowFirstOp.getText().equals("-")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal - secondRowSecondDiceRoll;

          } else if (secondRowFirstOp.getText().equals("*")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal * secondRowSecondDiceRoll;
          } else {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal / secondRowSecondDiceRoll;
          }
        }
      }
      // If the selected element is fire, add + 1 to each row
      if (currentElement.equals("Fire")) {
        firstRowTotal++;
        secondRowTotal++;
      }
      Log.i("firstrowdm", "first row damage : " + firstRowTotal);
      Log.i("firstrowdmg", "second rwo damage : " + secondRowTotal);
      if (firstRowTotal < 0) {
        firstRowTotal = 0;
      }
      if (secondRowTotal < 0) {
        secondRowTotal = 0;
      }
      this.player.updateMaxSingleDamage(firstRowTotal);
      this.player.updateMaxSingleDamage(secondRowTotal);
      this.player.updateMaxTotalDamage(firstRowTotal + secondRowTotal);
      int firstRowAttack = 0;
      int secondRowAttack = 0;
      String firstRowUser = "";
      String secondRowUser = "";
      double totalRolls = 0;
      double totalHits = 0;
      boolean isDeadOnFirstRoll = false;
      // Now check damage calulations
      // If the first row total from the dice is greater or equal to the answer
      if (firstRowTotal >= Integer.parseInt(firstRowAns.getText().toString())) {
        // do attack,
        firstRowAttack = firstRowTotal - Integer.parseInt(firstRowAns.getText().toString());
        currOpponet.takeDamage((int) (firstRowAttack * player.getDamageRate()));
        firstRowUser = "******";
        // recalc health,
        int gj2 = currOpponet.getCurrentHealth();
        opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
        opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
        this.player.updateTotalHits();
        this.player.updateTotalRolls();
        totalRolls++;
        totalHits++;
        // Check op health if dead rewards screen,
        if (currOpponet.getCurrentHealth() <= 0) {
          player.swapDiceBackToInv();
          opponents.remove(0);
          generateOps = true;
          isDeadOnFirstRoll = true;
          isDead = true;
          this.player.updateHighestAcc(totalHits / totalRolls * 100);
          generateResults(
              firstRowFirstDiceRoll,
              firstRowSecondDiceRoll,
              secondRowFirstDiceRoll,
              secondRowSecondDiceRoll,
              firstRowFirstOp.getText().toString(),
              secondRowFirstOp.getText().toString(),
              firstRowSecondOp.getText().toString(),
              secondRowSecondOp.getText().toString(),
              firstRowAns.getText().toString(),
              secondRowAns.getText().toString(),
              firstRowAttack,
              firstRowUser,
              secondRowAttack,
              secondRowUser,
              isDeadOnFirstRoll,
              isDead,
              currOpponet.getAttack());
          generateNextOpponent();
        }
        // Else if it is less than the answer
      } else {
        // do enemy attack,
        firstRowAttack = Integer.parseInt(firstRowAns.getText().toString()) - firstRowTotal;
        player.takeDamage(firstRowAttack + currOpponet.getAttack());
        firstRowUser = "******";
        // recalc health,
        playerHealth.setText("Player Health: " + player.getCurrentHealth());
        int d = player.getPercentHealthLeft();
        playerProgressBar.setProgress(player.getPercentHealthLeft());
        this.player.updateTotalRolls();
        totalRolls++;
      }

      // IF the second row total from the dice is greater or equal to the answer
      if ((secondRowTotal >= Integer.parseInt(secondRowAns.getText().toString()))
          && !(currOpponet.getCurrentHealth() < 0)
          && !(isDead)) {
        // do attack,
        secondRowAttack = secondRowTotal - Integer.parseInt(secondRowAns.getText().toString());
        currOpponet.takeDamage((int) (secondRowAttack * player.getDamageRate()));
        secondRowUser = "******";
        // recalc health,
        int o1 = currOpponet.getCurrentHealth();
        opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
        opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
        this.player.updateTotalHits();
        this.player.updateTotalRolls();
        totalRolls++;
        totalHits++;
        // Check op health if dead rewards screen,
        if (currOpponet.getCurrentHealth() <= 0) {
          player.swapDiceBackToInv();
          if (opponents.size() > 0) opponents.remove(0);
          generateOps = true;
          isDead = true;
          player.swapDiceBackToInv();
          generateResults(
              firstRowFirstDiceRoll,
              firstRowSecondDiceRoll,
              secondRowFirstDiceRoll,
              secondRowSecondDiceRoll,
              firstRowFirstOp.getText().toString(),
              secondRowFirstOp.getText().toString(),
              firstRowSecondOp.getText().toString(),
              secondRowSecondOp.getText().toString(),
              firstRowAns.getText().toString(),
              secondRowAns.getText().toString(),
              firstRowAttack,
              firstRowUser,
              secondRowAttack,
              secondRowUser,
              false,
              isDead,
              currOpponet.getAttack());
          generateNextOpponent();
        }
        // Else if it is less than the answer
      } else if (!(currOpponet.getCurrentHealth() < 0) && !(isDead)) {
        // do enemy attack,
        secondRowAttack = Integer.parseInt(secondRowAns.getText().toString()) - secondRowTotal;
        player.takeDamage(secondRowAttack + currOpponet.getAttack());
        secondRowUser = "******";
        // recalc health,
        playerHealth.setText("Player Health: " + player.getCurrentHealth());
        playerProgressBar.setProgress(player.getPercentHealthLeft());
        this.player.updateTotalRolls();
        totalRolls++;
      }

      // If at this point we have not gotten pushed to the rewards screen, lets see if the player is
      // dead
      if (player.getCurrentHealth() <= 0) {
        player.swapDiceBackToInv();
        generateResults(
            firstRowFirstDiceRoll,
            firstRowSecondDiceRoll,
            secondRowFirstDiceRoll,
            secondRowSecondDiceRoll,
            firstRowFirstOp.getText().toString(),
            secondRowFirstOp.getText().toString(),
            firstRowSecondOp.getText().toString(),
            secondRowSecondOp.getText().toString(),
            firstRowAns.getText().toString(),
            secondRowAns.getText().toString(),
            firstRowAttack,
            firstRowUser,
            secondRowAttack,
            secondRowUser,
            false,
            false,
            currOpponet.getAttack());
        generateDeathScreen();
      }
      // Else the player is alive and so is the monster, so reset the dice locations to null
      // Generate new answers
      // Generate new operations
      // Swap the dice from the players usedDice array to the inventory and keep fighting
      else if (generateOps == false) {
        // reset operations and dice
        player.swapDiceBackToInv();
        generateResults(
            firstRowFirstDiceRoll,
            firstRowSecondDiceRoll,
            secondRowFirstDiceRoll,
            secondRowSecondDiceRoll,
            firstRowFirstOp.getText().toString(),
            secondRowFirstOp.getText().toString(),
            firstRowSecondOp.getText().toString(),
            secondRowSecondOp.getText().toString(),
            firstRowAns.getText().toString(),
            secondRowAns.getText().toString(),
            firstRowAttack,
            firstRowUser,
            secondRowAttack,
            secondRowUser,
            false,
            false,
            currOpponet.getAttack());
        resetDicePics();
        generateOperations(currOpponet.getOp());
        generateAns(level);

        ImageButton frfdTag = (ImageButton) findViewById(R.id.firstRowFirstDice);
        frfdTag.setTag(null);
        ImageButton frsdTag = (ImageButton) findViewById(R.id.firstRowSecondDice);
        frsdTag.setTag(null);
        ImageButton srfdTag = (ImageButton) findViewById(R.id.secondRowFirstDice);
        srfdTag.setTag(null);
        ImageButton srsdTag = (ImageButton) findViewById(R.id.secondRowSecondDice);
        srsdTag.setTag(null);
      }
    }
  }
示例#18
0
 public String toString() {
   return "Die 1: " + die1.getFaceValue() + " Die 2: " + die2.getFaceValue();
 }
示例#19
0
 public int sum() {
   return die1.getFaceValue() + die2.getFaceValue();
 }
示例#20
0
 public int roll() {
   return die1.roll() + die2.roll();
 }
示例#21
0
 public void setDie2FaceValue(int faceValue) {
   die2.setFaceValue(faceValue);
 }
示例#22
0
 public int getDie2FaceValue() {
   return die2.getFaceValue();
 }
示例#23
0
 public int getDie1FaceValue() {
   return die1.getFaceValue();
 }
示例#24
0
 @Test
 public void testRollDiceNotOverSix() {
   Die die = new Die();
   assertTrue(7 > die.getValue());
 }
示例#25
0
 @Test
 public void testDicePositiveNumber() {
   Die die = new Die();
   assertTrue(die.getValue() > 0);
 }