Advertisement
Guest User

DnDice V1.2

a guest
Sep 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. package Independent_Example_Programs;
  2. import java.util.Scanner;
  3. import java.util.concurrent.ThreadLocalRandom;
  4. public class DnDice
  5. {
  6. public static void main( String args[] )
  7. {
  8. Scanner keyboard = new Scanner(System.in);
  9. 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;
  10. boolean addition=false,subtraction=false,multiplication=false,division=false,modifier=false;
  11. System.out.println("This program follows basic Dice Notation, Credits to Garry Gygax and Wizards of the Coast (Or whoever owns Dungeons and Dragons)");
  12. System.out.print("Enter Dice Roll: ");
  13. String dice=keyboard.next();//Takes the input of (x)d(y) and makes it a string
  14. if(dice.contains("d")){}
  15. else{System.out.println(dice+" does not contain correct format");System.exit(0);}//If dice does not contain atleast one d it will crash
  16. if(dice.contains("+")){addition=true;modifier=true;countuniquemodifiers++;}if(dice.contains("-")){subtraction=true;modifier=true;countuniquemodifiers++;}//Checking if modifiers are present
  17. if(dice.contains("*")){multiplication=true;modifier=true;countuniquemodifiers++;}if(dice.contains("/")){division=true;modifier=true;countuniquemodifiers++;}//Checking if modifiers are present
  18. 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
  19. String[] parts = dice.split("d|\\+|\\-|\\*|\\/");// Splits up the string "dice" by finding the d in (x)d(y), and also by splitting by modifier
  20. 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
  21. 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
  22. String numberofdice=parts[0];//Part 0 represents the x, making numberofdice=x
  23. String maxvalueofdice=parts[1];//Part 1 represents the y, making maxvalueofdice=y
  24. if(modifier==true){//If there is a modifier
  25. String numberwithmodifier=parts[2];//Parts[2], which is the number after the modifier gets turned into a string
  26. numberwithmodifier_int=Integer.parseInt(numberwithmodifier);//And the string is then converted to the already intitialized int numberwithmodifer_int
  27. }
  28. numberofdice_int=Integer.parseInt(numberofdice);//Converts string to int
  29. maxvalueofdice_int=Integer.parseInt(maxvalueofdice);//Converts string to int
  30. int dicevalues[] = new int [(numberofdice_int)];//Makes array so that values may be called back to
  31. while(i<numberofdice_int)
  32. {
  33. int rando = ThreadLocalRandom.current().nextInt(1,(maxvalueofdice_int+1));//System.out.println("rando is "+rando);
  34. dicevalues[i]=rando;
  35. sum=sum+dicevalues[i];
  36. if(dicevalues[i]>=largest){largest=dicevalues[i];}
  37. if(dicevalues[i]<=smallest){smallest=dicevalues[i];}
  38. i++;
  39. }
  40. if(addition==true){sumwithmodifier=sum+numberwithmodifier_int;}
  41. if(subtraction==true){sumwithmodifier=sum-numberwithmodifier_int;}
  42. if(multiplication==true){sumwithmodifier=sum*numberwithmodifier_int;}
  43. if(division==true){sumwithmodifier=sum/numberwithmodifier_int;}
  44. average=(sum)/(numberofdice_int);
  45. if(numberofdice_int>1)
  46. {
  47. System.out.print("The dice rolled were ");i=0;
  48. while(i<numberofdice_int)
  49. {
  50. System.out.print(dicevalues[i]);
  51. if((i+1)==numberofdice_int){}
  52. else{System.out.print(", ");}
  53. i++;
  54. }
  55. 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("");}
  56. System.out.println("The average is "+average);
  57. System.out.println("Smallest: "+smallest+" Largest: "+largest);
  58. }
  59. else
  60. {
  61. 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("");}
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement