Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. //If player 1 wins, Player 2 still gets a final turn. How to stop this?
  2. //how to use the 6 gets another go without neverending if statements? If player rolls a second 6 in a row.
  3.  
  4.  
  5. import java.math.*;
  6. import java.util.*;
  7. public class DiceGame {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scan = new Scanner(System.in);
  11.         String player1;
  12.         String player2;
  13.         int p1Position = 0;
  14.         int p2Position = 0;
  15.         int diceValue = (int)(Math.random()*6+1);
  16.        
  17.         System.out.print("Enter name of Player 1: ");
  18.         player1 = scan.nextLine();
  19.         System.out.print("Enter name of Player 2: ");
  20.         player2 = scan.nextLine();
  21.        
  22.         System.out.println(" ");
  23.        
  24.         while (p1Position < 20 && p2Position < 20) {
  25.             diceValue = (int)(Math.random()*6+1);
  26.             System.out.println(player1 + " has rolled a " + diceValue + ".");
  27.             if ((p1Position + diceValue) > 20) {
  28.                 System.out.println(player1 + " must land on 20 to win. " + player1 + " stays on position " + p1Position + ".\n");
  29.             } else if ((p1Position + diceValue) == 20) {
  30.                 System.out.println(player1 + " was on position " + p1Position + ", and is now on position " + (p1Position + diceValue) + ".");
  31.                 System.out.println(player1 + " wins!\n");
  32.                 p1Position = p1Position + diceValue;
  33.             } else {
  34.                
  35.                 System.out.println(player1 + " was on position " + p1Position + ", and is now on position " + (p1Position + diceValue) + ".\n");
  36.                 p1Position = p1Position + diceValue;           
  37.             }
  38.            
  39.             diceValue = (int)(Math.random()*6+1);
  40.             System.out.println(player2 + " has rolled a " + diceValue + ".");
  41.             if ((p2Position + diceValue) > 20) {
  42.                 System.out.println(player2 + " must land on 20 to win. " + player2 + " stays on position " + p2Position + ".\n");
  43.             } else if ((p2Position + diceValue) == 20) {
  44.                 System.out.println(player2 + " was on position " + p2Position + ", and is now on position " + (p2Position + diceValue) + ".");
  45.                 System.out.println(player2 + " wins!\n");
  46.                 p2Position = p2Position + diceValue;   
  47.        
  48.             } else {
  49.  
  50.                 System.out.println(player2 + " was on position " + p2Position + ", and is now on position " + (p2Position + diceValue) + ".\n");
  51.                 p2Position = p2Position + diceValue;           
  52.             }
  53.            
  54.         }
  55.  
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement