Android Open Source - androll Roll






From Project

Back to project page androll.

License

The source code is released under:

Apache License

If you think the Android project androll listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.cybersensei.androll;
// w ww .  j a  v  a  2  s.  co  m
import java.util.Random;

public class Roll {
  public static final Random random = new Random();
  private int sides;
  private int[] rolls;
  public static final int[] DIE_SIDES = {2, 3, 4, 6, 8, 10, 12, 20, 100};
  
  public Roll(int sides) {
    this(sides, 1);
  } //constructor
  
  public Roll(int sides, int times) {
    if (sideValid(sides)) {
      this.sides = sides;
      this.rolls = new int[times];
    } //if
  } //constructor
  
  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    int total = 0;
    
    for (int i = 0; i < rolls.length; i++) {
      total += rolls[i];
      builder.append(rolls[i]);
      if (i < rolls.length - 1)
        builder.append(", ");
    } //for
    
    if (rolls.length > 1)
      builder.append(" Total: ").append(total);
    return builder.toString();
  } //toString
  
  private boolean sideValid(int side) {
    for (int i = 0; i < DIE_SIDES.length; i++) {
      if (side == DIE_SIDES[i])
        return true;
    } //for
    return false;
  } //sideValid
  
  public String roll() {
    for (int i = 0; i < rolls.length; i++) {
      rolls[i] = random.nextInt(sides) + 1;
    } //for
    return this.toString();
  } //roll
} //class Roll





Java Source Code List

me.cybersensei.androll.RollFragment.java
me.cybersensei.androll.RollHistoryActivity.java
me.cybersensei.androll.RollListener.java
me.cybersensei.androll.Roll.java
me.cybersensei.androll.RollerDialogFragment.java