Android Open Source - aSimpleDiceRoller Nerdmode Activity






From Project

Back to project page aSimpleDiceRoller.

License

The source code is released under:

GNU General Public License

If you think the Android project aSimpleDiceRoller 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

/*
 *  aSimpleDiceRoller - Simple dice roller application for Android.
 *  Copyright  2012 Elad Alfassa <elad@fedoraproject.org>
 * //from w  w  w  . ja  v  a  2  s  .  c  o m
 *  This file is part of aSimpleDiceRoller.
 *
 *   aSimpleDiceRoller is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   aSimpleDiceRoller is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aSimpleDiceRoller.  If not, see <http://www.gnu.org/licenses/>.

 */

package com.elad.diceroller;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.content.Intent;
import android.util.Log;
import com.elad.diceroller.StringUtils;


public class NerdmodeActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nerdmode);
  }
  public void processFormula(View view) {
    EditText editText = (EditText) findViewById(R.id.editText1);
    String formula = editText.getText().toString(); //Input
    
    /* Algorithm:
     * 1) Figure out which die to roll (find the first d, take the number after it and before it)
     * 1.1) Roll
     * 2) Replace the name of the die with the result of the roll
     * 3) Separate numbers using StringUtils.separate
     * 4) Run multiplication+division, then addition and subtraction
     * 6) Display the result
     */
    if (formula.length()==0) { //Check input
      return;
    }
    formula.toLowerCase();
    int diceSize=0, diceCount=0, diceRoll=0;
    String formula_arr[] = StringUtils.separate(formula);
    if (formula_arr[1].equals("d")) {
      diceCount=Integer.parseInt(formula_arr[0], 10);
      diceSize=Integer.parseInt(formula_arr[2], 10);
      for(int d=1; d <= diceCount; d++){  
        diceRoll += DiceRollerShared.rollDice(diceSize); 
      }
    }
    if (formula_arr[0].equals("d")) {
      diceSize=Integer.parseInt(formula_arr[1], 10);
      diceRoll=DiceRollerShared.rollDice(diceSize); 
    }
    //Display DiceRoll
  }

}




Java Source Code List

com.elad.diceroller.ASimpleDiceRollerActivity.java
com.elad.diceroller.DiceListActivity.java
com.elad.diceroller.DiceRollerShared.java
com.elad.diceroller.NerdmodeActivity.java
com.elad.diceroller.StringUtils.java