Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If player 1 wins, Player 2 still gets a final turn. How to stop this?
- //how to use the 6 gets another go without neverending if statements? If player rolls a second 6 in a row.
- import java.math.*;
- import java.util.*;
- public class DiceGame {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String player1;
- String player2;
- int p1Position = 0;
- int p2Position = 0;
- int diceValue = (int)(Math.random()*6+1);
- System.out.print("Enter name of Player 1: ");
- player1 = scan.nextLine();
- System.out.print("Enter name of Player 2: ");
- player2 = scan.nextLine();
- System.out.println(" ");
- while (p1Position < 20 && p2Position < 20) {
- diceValue = (int)(Math.random()*6+1);
- System.out.println(player1 + " has rolled a " + diceValue + ".");
- if ((p1Position + diceValue) > 20) {
- System.out.println(player1 + " must land on 20 to win. " + player1 + " stays on position " + p1Position + ".\n");
- } else if ((p1Position + diceValue) == 20) {
- System.out.println(player1 + " was on position " + p1Position + ", and is now on position " + (p1Position + diceValue) + ".");
- System.out.println(player1 + " wins!\n");
- p1Position = p1Position + diceValue;
- } else {
- System.out.println(player1 + " was on position " + p1Position + ", and is now on position " + (p1Position + diceValue) + ".\n");
- p1Position = p1Position + diceValue;
- }
- diceValue = (int)(Math.random()*6+1);
- System.out.println(player2 + " has rolled a " + diceValue + ".");
- if ((p2Position + diceValue) > 20) {
- System.out.println(player2 + " must land on 20 to win. " + player2 + " stays on position " + p2Position + ".\n");
- } else if ((p2Position + diceValue) == 20) {
- System.out.println(player2 + " was on position " + p2Position + ", and is now on position " + (p2Position + diceValue) + ".");
- System.out.println(player2 + " wins!\n");
- p2Position = p2Position + diceValue;
- } else {
- System.out.println(player2 + " was on position " + p2Position + ", and is now on position " + (p2Position + diceValue) + ".\n");
- p2Position = p2Position + diceValue;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement