Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.89 KB | None | 0 0
  1. import java.util.Scanner;   // Imports the stuff to take user input.
  2.  
  3. public class DiceGame
  4. {
  5.   static boolean spam;   // A boolean to keep track of if you want to be spammed with lots of unnecessary information.
  6.   static boolean spamChoice;   // A boolean to see if the user has chose wether or not to be spammed.
  7.   static boolean numberChoice1;   // A boolean to see if the user has chosen the number of dice player 1 will roll.
  8.   static boolean numberChoice2;   // A boolean to see if the user has chosen the number of dice player 2 will roll.
  9.   static boolean sideChoice1;   // A boolean to see if the user has chosen the number of sides player 1's dice have.
  10.   static boolean sideChoice2;   // A boolean to see if the user has chosen the number of sides player 2's dice have.
  11.   static boolean rollChoice;   // A boolean to see if the user has chosen an amount of games to be played.
  12.   static int diceNumber1;   // Creates an int that keeps track of the number of dice player 1 will roll.
  13.   static int diceNumber2;   // Creates an int that keeps track of the number of dice player 2 will roll.
  14.   static int sides1;   // Keeps track of the number of sides of player 1's dice.
  15.   static int sides2;   // Keeps track of the number of sides of player 2's dice.
  16.   static int rollNumber;   // Creates an int that keep tracks of the roll number.
  17.   static int winCount;   // Creates an int that keeps track of player 1's win count.
  18.   static int tieCount;   // Creates an int that keeps track of the number of ties.
  19.   static Scanner a = new Scanner(System.in);   // Creates a new static scanner named a.
  20.  
  21.   public static void main(String[] args)
  22.   {
  23.     do {
  24.       System.out.println("Do you wish to be spammed with the outcomes of all the games?\nYes or No");
  25.       String answer = a.nextLine();
  26.       if (answer.equalsIgnoreCase("Yes")) {   // Checks if the user typed yes.
  27.         System.out.println();
  28.         System.out.println("You shall now be spammed when you run this program.");
  29.         spam = true;   // Makes it so that the outcomes of all the games is shown to the user
  30.         spamChoice = true;
  31.       }
  32.       else if (answer.equalsIgnoreCase("No")) {   // Checks if the user typed no.
  33.         System.out.println("Good Choice. You will no longer be spammed when you run this program.");
  34.         spamChoice = true;
  35.       }
  36.       else {
  37.         System.out.println("You must enter yes or no.");
  38.       }
  39.       System.out.println();
  40.     } while (spamChoice == false);   // does not allow the user to move on if they enter something invalid.
  41.    
  42.     do {
  43.       System.out.println("How many dice should player 1 roll?");
  44.       diceNumber1 = a.nextInt();
  45.       if (diceNumber1 <= 0){
  46.         System.out.println("You must choose a positive integer!");
  47.       }
  48.       else if (diceNumber1 == 1) {
  49.         System.out.println("Player 1 will now roll 1 die.");
  50.         numberChoice1 = true;
  51.       }
  52.       else if (diceNumber1 > 1) {
  53.         System.out.println("Player 1 will now roll " + diceNumber1 + " dice.");
  54.         numberChoice1 = true;
  55.       }
  56.       System.out.println();
  57.     } while (numberChoice1 == false);   // does not allow the user to move on if they enter something invalid.
  58.    
  59.     do {
  60.       System.out.println("How many dice should player 2 roll?");
  61.       diceNumber2 = a.nextInt();
  62.       if (diceNumber2 <= 0){
  63.         System.out.println("You must choose a positive integer!");
  64.       }
  65.       else if (diceNumber2 == 1) {
  66.         System.out.println("Player 2 will now roll 1 die.");
  67.         numberChoice2 = true;
  68.       }
  69.       else if (diceNumber2 > 1) {
  70.         System.out.println("Player 2 will now roll " + diceNumber2 + " dice.");
  71.         numberChoice2 = true;
  72.       }
  73.       System.out.println();
  74.     } while (numberChoice2 == false);   // does not allow the user to move on if they enter something invalid.
  75.    
  76.     do {
  77.       System.out.println("How many sides should player 1's dice have?");
  78.       sides1 = a.nextInt();
  79.       if (sides1 == 1) {
  80.         System.out.println("You must choose a number greater than 1.");
  81.       }
  82.       else if (sides1 == 2) {
  83.         System.out.println("Player 1 will now just flip coins instead of rolling dice.");
  84.         sideChoice1 = true;
  85.       }
  86.       else if (sides1 > 2) {
  87.         System.out.println("Player 1 will now use roll " + sides1 + " sided dice.");
  88.         sideChoice1 = true;
  89.       }
  90.       System.out.println();
  91.     } while (sideChoice1 == false);   // does not allow the user to move on if they enter something invalid.
  92.    
  93.     do {
  94.       System.out.println("How many sides should player 2's dice have?");
  95.       sides2 = a.nextInt();
  96.       if (sides2 == 1) {
  97.         System.out.println("You must choose a number greater than 1.");
  98.       }
  99.       else if (sides2 == 2) {
  100.         System.out.println("Player 2 will now just flip coins instead of rolling dice.");
  101.         sideChoice2 = true;
  102.       }
  103.       else if (sides2 > 2) {
  104.         System.out.println("Player 2 will now use roll " + sides2 + " sided dice.");
  105.         sideChoice2 = true;
  106.       }
  107.       System.out.println();
  108.     } while (sideChoice2 == false);   // does not allow the user to move on if they enter something invalid.
  109.    
  110.     do {
  111.       System.out.println("How many games should be played?");
  112.       rollNumber = a.nextInt();
  113.       if (rollNumber <= 0) {
  114.         System.out.println("Please choose a number that is greater than 0");
  115.       }
  116.       else {
  117.         System.out.println("Ok. There will be " + rollNumber + " games played.");
  118.         rollChoice = true;
  119.       }
  120.       System.out.println();
  121.     } while (rollChoice == false);   // does not allow the user to move on if they enter something invalid.
  122.    
  123.     for (int i = 0; i < rollNumber; i++) {
  124.       Die player1 = new Die(sides1,diceNumber1);   // Creates a new Die called player1 with the side number and dice number specified by the user.
  125.       Die player2 = new Die(sides2,diceNumber2);   // Creates a new Die called player2 with the side number and dice number specified by the user.
  126.      
  127.       int dieroll1 = player1.roll();
  128.       int dieroll2 = player2.roll();
  129.       if (dieroll1 > dieroll2) {   // Checks if player 1 rolled a higher number.
  130.         winCount++;
  131.       }
  132.       else if (dieroll1 == dieroll2) {   // Checks if both players rolled the same number.
  133.         tieCount++;
  134.       }
  135.       if (spam == true) {   // Checks if the user wanted to be spammed with the outcomes fo every game.
  136.         if (sides1 == 2){
  137.           System.out.println("Player 1 got a " + dieroll1);
  138.         }
  139.         else {
  140.           System.out.println("Player 1 rolled a " + dieroll1);
  141.         }
  142.         if (sides2 == 2) {
  143.           System.out.println("Player 2 got a " + dieroll2);
  144.         }
  145.         else {
  146.           System.out.println("Player 2 rolled a " + dieroll2);
  147.         }
  148.       if (dieroll1 > dieroll2) System.out.println("Player 1 wins!");
  149.       else
  150.         if (dieroll2 > dieroll1) System.out.println("Player 2 wins!");
  151.       else System.out.println("It's a tie!");
  152.       }
  153.     }
  154.     System.out.println("\nPlayer 1 won " + winCount + " times!\nPlayer 2 won " + (rollNumber-winCount-tieCount) + " times!\nThere were " + tieCount + " ties!");   // Prints out the total wins for each player and the total ties.
  155.   }
  156. }
  157.  
  158.  
  159. // This goes in a separate file.
  160.  
  161. public class Die
  162. {
  163.   private int sideNumber;   // number of sides on the dice.
  164.   private int diceNumber;   // number of dice.
  165.   private int total;   // The total number rolled.
  166.   int lastroll = 0;
  167.   public Die(int sides, int amount) {   // Custom constructor with variable for sides and number of dice.
  168.     sideNumber = sides;
  169.     diceNumber = amount;
  170.   }
  171.   // methods
  172.   public int roll()
  173.   {  
  174.     for (int i = 0; i < diceNumber; i++) {   // Rolls an amount of times equivalent to the number of dice.
  175.       int x = (int)((Math.random()*sideNumber)+1);
  176.       total += x;   // Adds the amount from each roll the the total.
  177.     }
  178.     return total;
  179.   }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement