Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class DiceGame4 {
- public static void main(String[] args) {
- int p1Score = 0;
- int p2Score = 0;
- int die1;
- int die2;
- int die3;
- int die4;
- int playerOneSum;
- int playerTwoSum;
- do {
- die1 = new Random().nextInt(6) + 1;
- die2 = new Random().nextInt(6) + 1;
- System.out.println("Player 1 rolls a " + die1 + " and a " + die2 + ".");
- playerOneSum = die1 + die2;
- p1Score += playerOneSum;
- System.out.println("Player 1 now has " + p1Score);
- if (die1 == die2) {
- System.out.println("Player 1 gets to roll again.");
- die1 = new Random().nextInt(6) + 1;
- die2 = new Random().nextInt(6) + 1;
- System.out.println("Player 1 rolls a " + die1 + " and a " + die2 + ".");
- playerOneSum = die1 + die2;
- p1Score += playerOneSum;
- System.out.println("Player 1 now has " + p1Score);
- }
- die3 = new Random().nextInt(6) + 1;
- die4 = new Random().nextInt(6) + 1;
- System.out.println("\nPlayer 2 rolls a " + die3 + " and a " + die4 + ".");
- playerTwoSum = die3 + die4;
- p2Score += playerTwoSum;
- System.out.println("Player 2 now has " + p2Score);
- if (die3 == die4) {
- System.out.println("Player 2 gets to roll again.");
- die3 = new Random().nextInt(6) + 1;
- die4 = new Random().nextInt(6) + 1;
- System.out.println("\nPlayer 2 rolls a " + die3 + " and a " + die4 + ".");
- playerTwoSum = die3 + die4;
- p2Score += playerTwoSum;
- System.out.println("Player 2 now has " + p2Score);
- }
- System.out.println(); // prints empty newline for readability purposes
- } while (p1Score < 75 && p2Score < 75);
- // Print winner...
- if (p1Score > p2Score) {
- System.out.println("Player 1 has won with a score of " + p1Score);
- } else if (p1Score == p2Score) {
- System.out.println("No one won.");
- } else {
- System.out.println("Player 2 has won with a score of " + p2Score);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment