Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; // Imports the stuff to take user input.
- public class DiceGame
- {
- static boolean spam; // A boolean to keep track of if you want to be spammed with lots of unnecessary information.
- static boolean spamChoice; // A boolean to see if the user has chose wether or not to be spammed.
- static boolean numberChoice1; // A boolean to see if the user has chosen the number of dice player 1 will roll.
- static boolean numberChoice2; // A boolean to see if the user has chosen the number of dice player 2 will roll.
- static boolean sideChoice1; // A boolean to see if the user has chosen the number of sides player 1's dice have.
- static boolean sideChoice2; // A boolean to see if the user has chosen the number of sides player 2's dice have.
- static boolean rollChoice; // A boolean to see if the user has chosen an amount of games to be played.
- static int diceNumber1; // Creates an int that keeps track of the number of dice player 1 will roll.
- static int diceNumber2; // Creates an int that keeps track of the number of dice player 2 will roll.
- static int sides1; // Keeps track of the number of sides of player 1's dice.
- static int sides2; // Keeps track of the number of sides of player 2's dice.
- static int rollNumber; // Creates an int that keep tracks of the roll number.
- static int winCount; // Creates an int that keeps track of player 1's win count.
- static int tieCount; // Creates an int that keeps track of the number of ties.
- static Scanner a = new Scanner(System.in); // Creates a new static scanner named a.
- public static void main(String[] args)
- {
- do {
- System.out.println("Do you wish to be spammed with the outcomes of all the games?\nYes or No");
- String answer = a.nextLine();
- if (answer.equalsIgnoreCase("Yes")) { // Checks if the user typed yes.
- System.out.println();
- System.out.println("You shall now be spammed when you run this program.");
- spam = true; // Makes it so that the outcomes of all the games is shown to the user
- spamChoice = true;
- }
- else if (answer.equalsIgnoreCase("No")) { // Checks if the user typed no.
- System.out.println("Good Choice. You will no longer be spammed when you run this program.");
- spamChoice = true;
- }
- else {
- System.out.println("You must enter yes or no.");
- }
- System.out.println();
- } while (spamChoice == false); // does not allow the user to move on if they enter something invalid.
- do {
- System.out.println("How many dice should player 1 roll?");
- diceNumber1 = a.nextInt();
- if (diceNumber1 <= 0){
- System.out.println("You must choose a positive integer!");
- }
- else if (diceNumber1 == 1) {
- System.out.println("Player 1 will now roll 1 die.");
- numberChoice1 = true;
- }
- else if (diceNumber1 > 1) {
- System.out.println("Player 1 will now roll " + diceNumber1 + " dice.");
- numberChoice1 = true;
- }
- System.out.println();
- } while (numberChoice1 == false); // does not allow the user to move on if they enter something invalid.
- do {
- System.out.println("How many dice should player 2 roll?");
- diceNumber2 = a.nextInt();
- if (diceNumber2 <= 0){
- System.out.println("You must choose a positive integer!");
- }
- else if (diceNumber2 == 1) {
- System.out.println("Player 2 will now roll 1 die.");
- numberChoice2 = true;
- }
- else if (diceNumber2 > 1) {
- System.out.println("Player 2 will now roll " + diceNumber2 + " dice.");
- numberChoice2 = true;
- }
- System.out.println();
- } while (numberChoice2 == false); // does not allow the user to move on if they enter something invalid.
- do {
- System.out.println("How many sides should player 1's dice have?");
- sides1 = a.nextInt();
- if (sides1 == 1) {
- System.out.println("You must choose a number greater than 1.");
- }
- else if (sides1 == 2) {
- System.out.println("Player 1 will now just flip coins instead of rolling dice.");
- sideChoice1 = true;
- }
- else if (sides1 > 2) {
- System.out.println("Player 1 will now use roll " + sides1 + " sided dice.");
- sideChoice1 = true;
- }
- System.out.println();
- } while (sideChoice1 == false); // does not allow the user to move on if they enter something invalid.
- do {
- System.out.println("How many sides should player 2's dice have?");
- sides2 = a.nextInt();
- if (sides2 == 1) {
- System.out.println("You must choose a number greater than 1.");
- }
- else if (sides2 == 2) {
- System.out.println("Player 2 will now just flip coins instead of rolling dice.");
- sideChoice2 = true;
- }
- else if (sides2 > 2) {
- System.out.println("Player 2 will now use roll " + sides2 + " sided dice.");
- sideChoice2 = true;
- }
- System.out.println();
- } while (sideChoice2 == false); // does not allow the user to move on if they enter something invalid.
- do {
- System.out.println("How many games should be played?");
- rollNumber = a.nextInt();
- if (rollNumber <= 0) {
- System.out.println("Please choose a number that is greater than 0");
- }
- else {
- System.out.println("Ok. There will be " + rollNumber + " games played.");
- rollChoice = true;
- }
- System.out.println();
- } while (rollChoice == false); // does not allow the user to move on if they enter something invalid.
- for (int i = 0; i < rollNumber; i++) {
- Die player1 = new Die(sides1,diceNumber1); // Creates a new Die called player1 with the side number and dice number specified by the user.
- Die player2 = new Die(sides2,diceNumber2); // Creates a new Die called player2 with the side number and dice number specified by the user.
- int dieroll1 = player1.roll();
- int dieroll2 = player2.roll();
- if (dieroll1 > dieroll2) { // Checks if player 1 rolled a higher number.
- winCount++;
- }
- else if (dieroll1 == dieroll2) { // Checks if both players rolled the same number.
- tieCount++;
- }
- if (spam == true) { // Checks if the user wanted to be spammed with the outcomes fo every game.
- if (sides1 == 2){
- System.out.println("Player 1 got a " + dieroll1);
- }
- else {
- System.out.println("Player 1 rolled a " + dieroll1);
- }
- if (sides2 == 2) {
- System.out.println("Player 2 got a " + dieroll2);
- }
- else {
- System.out.println("Player 2 rolled a " + dieroll2);
- }
- if (dieroll1 > dieroll2) System.out.println("Player 1 wins!");
- else
- if (dieroll2 > dieroll1) System.out.println("Player 2 wins!");
- else System.out.println("It's a tie!");
- }
- }
- 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.
- }
- }
- // This goes in a separate file.
- public class Die
- {
- private int sideNumber; // number of sides on the dice.
- private int diceNumber; // number of dice.
- private int total; // The total number rolled.
- int lastroll = 0;
- public Die(int sides, int amount) { // Custom constructor with variable for sides and number of dice.
- sideNumber = sides;
- diceNumber = amount;
- }
- // methods
- public int roll()
- {
- for (int i = 0; i < diceNumber; i++) { // Rolls an amount of times equivalent to the number of dice.
- int x = (int)((Math.random()*sideNumber)+1);
- total += x; // Adds the amount from each roll the the total.
- }
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement