Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Independent_Example_Programs;
- import java.util.Scanner;
- import java.util.concurrent.ThreadLocalRandom;
- public class DnDice
- {
- public static void main( String args[] )
- {
- Scanner keyboard = new Scanner(System.in);
- int i=0,maxvalueofdice_int,numberofdice_int,numberwithmodifier_int=0,sum=0,sumwithmodifier=0,average,largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE,countuniquemodifiers=0;
- boolean addition=false,subtraction=false,multiplication=false,division=false,modifier=false;
- System.out.println("This program follows basic Dice Notation, Credits to Garry Gygax and Wizards of the Coast (Or whoever owns Dungeons and Dragons)");
- System.out.print("Enter Dice Roll: ");
- String dice=keyboard.next();//Takes the input of (x)d(y) and makes it a string
- if(dice.contains("d")){}
- else{System.out.println(dice+" does not contain correct format");System.exit(0);}//If dice does not contain atleast one d it will crash
- if(dice.contains("+")){addition=true;modifier=true;countuniquemodifiers++;}if(dice.contains("-")){subtraction=true;modifier=true;countuniquemodifiers++;}//Checking if modifiers are present
- if(dice.contains("*")){multiplication=true;modifier=true;countuniquemodifiers++;}if(dice.contains("/")){division=true;modifier=true;countuniquemodifiers++;}//Checking if modifiers are present
- if(countuniquemodifiers>=2){System.out.println(dice+" does not contain correct format");System.exit(0);}//If more than two unique modifiers exist it will crash
- String[] parts = dice.split("d|\\+|\\-|\\*|\\/");// Splits up the string "dice" by finding the d in (x)d(y), and also by splitting by modifier
- if(parts.length>3&&modifier==true){System.out.println(dice+" does not contain correct format");System.exit(0);}//If there was a modifier and there's more than 3 parts to parts it crashes
- if(parts.length>2&&modifier==false){System.out.println(dice+" does not contain correct format");System.exit(0);}//If there was not a modifier and there's more than 2 parts to parts it crashes
- String numberofdice=parts[0];//Part 0 represents the x, making numberofdice=x
- String maxvalueofdice=parts[1];//Part 1 represents the y, making maxvalueofdice=y
- if(modifier==true){//If there is a modifier
- String numberwithmodifier=parts[2];//Parts[2], which is the number after the modifier gets turned into a string
- numberwithmodifier_int=Integer.parseInt(numberwithmodifier);//And the string is then converted to the already intitialized int numberwithmodifer_int
- }
- numberofdice_int=Integer.parseInt(numberofdice);//Converts string to int
- maxvalueofdice_int=Integer.parseInt(maxvalueofdice);//Converts string to int
- int dicevalues[] = new int [(numberofdice_int)];//Makes array so that values may be called back to
- while(i<numberofdice_int)
- {
- int rando = ThreadLocalRandom.current().nextInt(1,(maxvalueofdice_int+1));//System.out.println("rando is "+rando);
- dicevalues[i]=rando;
- sum=sum+dicevalues[i];
- if(dicevalues[i]>=largest){largest=dicevalues[i];}
- if(dicevalues[i]<=smallest){smallest=dicevalues[i];}
- i++;
- }
- if(addition==true){sumwithmodifier=sum+numberwithmodifier_int;}
- if(subtraction==true){sumwithmodifier=sum-numberwithmodifier_int;}
- if(multiplication==true){sumwithmodifier=sum*numberwithmodifier_int;}
- if(division==true){sumwithmodifier=sum/numberwithmodifier_int;}
- average=(sum)/(numberofdice_int);
- if(numberofdice_int>1)
- {
- System.out.print("The dice rolled were ");i=0;
- while(i<numberofdice_int)
- {
- System.out.print(dicevalues[i]);
- if((i+1)==numberofdice_int){}
- else{System.out.print(", ");}
- i++;
- }
- System.out.print("\nAnd all the values added together is "+sum);if(modifier==true){System.out.println(", with a final value of "+sumwithmodifier);}else{System.out.println("");}
- System.out.println("The average is "+average);
- System.out.println("Smallest: "+smallest+" Largest: "+largest);
- }
- else
- {
- System.out.print("The die rolled was "+dicevalues[0]);if(modifier==true){System.out.println(" with a final value of "+sumwithmodifier);}else{System.out.println("");}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement