binibiningtinamoran

DiceGame4

Oct 22nd, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class DiceGame4 {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int p1Score = 0;
  8.         int p2Score = 0;
  9.         int die1;
  10.         int die2;
  11.         int die3;
  12.         int die4;
  13.         int playerOneSum;
  14.         int playerTwoSum;
  15.  
  16.         do {
  17.             die1 = new Random().nextInt(6) + 1;
  18.             die2 = new Random().nextInt(6) + 1;
  19.  
  20.             System.out.println("Player 1 rolls a " + die1 + " and a " + die2 + ".");
  21.             playerOneSum = die1 + die2;
  22.             p1Score += playerOneSum;
  23.             System.out.println("Player 1 now has " + p1Score);
  24.  
  25.             if (die1 == die2) {
  26.                 System.out.println("Player 1 gets to roll again.");
  27.                 die1 = new Random().nextInt(6) + 1;
  28.                 die2 = new Random().nextInt(6) + 1;
  29.                 System.out.println("Player 1 rolls a " + die1 + " and a " + die2 + ".");
  30.                 playerOneSum = die1 + die2;
  31.                 p1Score += playerOneSum;
  32.                 System.out.println("Player 1 now has " + p1Score);
  33.             }
  34.  
  35.             die3 = new Random().nextInt(6) + 1;
  36.             die4 = new Random().nextInt(6) + 1;
  37.  
  38.             System.out.println("\nPlayer 2 rolls a " + die3 + " and a " + die4 + ".");
  39.             playerTwoSum = die3 + die4;
  40.             p2Score += playerTwoSum;
  41.             System.out.println("Player 2 now has " + p2Score);
  42.  
  43.             if (die3 == die4) {
  44.                 System.out.println("Player 2 gets to roll again.");
  45.                 die3 = new Random().nextInt(6) + 1;
  46.                 die4 = new Random().nextInt(6) + 1;
  47.  
  48.                 System.out.println("\nPlayer 2 rolls a " + die3 + " and a " + die4 + ".");
  49.                 playerTwoSum = die3 + die4;
  50.                 p2Score += playerTwoSum;
  51.                 System.out.println("Player 2 now has " + p2Score);
  52.             }
  53.  
  54.             System.out.println(); // prints empty newline for readability purposes
  55.         } while (p1Score < 75 && p2Score < 75);
  56.  
  57.         // Print winner...
  58.         if (p1Score > p2Score) {
  59.             System.out.println("Player 1 has won with a score of " + p1Score);
  60.         } else if (p1Score == p2Score) {
  61.             System.out.println("No one won.");
  62.         } else {
  63.             System.out.println("Player 2 has won with a score of " + p2Score);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment