Question 3 20 marks Write a simple pressyourluck dice game T

Question 3. (20 marks) Write a simple press-your-luck dice game. The game works as follows:

Player 1 starts with a score of zero and rolls two dice.
o If the player rolls 7, 2, or 12 their turn is over and they lose.
o If the player rolls any other sum it is added to their total score.
o The player can stop rolling at any time – remember if their last roll was a
7, 2 or 12 they lose and Player 2 wins.
After Player 1 stops rolling, Player 2 rolls the two dice
o Player 2 continues to roll the dice until they roll a 7, 2 or 12 (Player 2
loses) or they have a total higher than Player 1 (Player 2 wins).

Your solution should make use of methods with the following headers:
public static int rollDie()
o return a random valid die value between 1 and 6
public static int newScore(int oldScore, int rollValue)
o determine if the current roll should be added to the score. If the
player rolled 7, 2 or 12 return -1 to show that the player just lost.
Otherwise return the updated score.
public static int whoGoesFirst()
o randomly return 1 if player 1 goes first or 2 if player 2 goes first
public static String winner(int playerNumber)
o print a message indicating who won the game
public static char keepRolling()
o A method that prompts the player to ask if they want to keep rolling.
Return y or n as needed. Note: Player 2 ~could~ stop rolling if they
wanted to but if they haven’t achieved a higher score they will lose.
main()
o this is your main method that coordinates all of the other methods as
necessary. Collect all input and produce all output from this method.

Your game should be able to play a complete human vs. human game.

No sample output. Be creative (but kind to your users).

(5 marks) You should be able to play human vs. computer AND play multiple games without quitting the program AND keep
track of how many games each player has won.

Solution

import java.util.Scanner;

public class Test{
   public static int rollDie(){
       return (int)(Math.random() * 6) + 1;
   }
   public static int newScore(int oldScore, int rollValue){
       if(rollValue == 7 || rollValue == 2 || rollValue == 12){
           return -1;
       }
       else{
           oldScore += rollValue;
           return oldScore;
       }
   }
   public static int whoGoesFirst(){
       return (int)(Math.random() * 2) + 1;
   }
   public static String winner(int playerNumber){
       return \"Player \" + playerNumber + \" Won!!\";
   }
   public static char keepRolling(){
       Scanner in = new Scanner(System.in);
       System.out.print(\"Do you want to continue? (1 for yes): \");
       int ans = in.nextInt();
       if(ans == 1) return \'y\';
       return \'n\';
   }
   public static void main(String[] args){
       int winCount[] = {0, 0};
       while(true){
           int scores[] = {0, 0};
           int turn = whoGoesFirst();
           int status;
           while(true){
               int roll = rollDie();
               System.out.println(\"Player \" + (turn % 2 + 1) + \" roll: \" + roll);
               status = newScore(scores[turn % 2], roll);
               if(status == -1){
                   System.out.println(\"Player \" + (turn % 2 + 1) + \" lose\");
                   winCount[(turn + 1) % 2]++;
                   break;
               }
               else{
                   scores[turn % 2] += roll;
               }
               turn++;
               char again = keepRolling();
               if(again == \'n\') break;
           }
           if(status != -1){
               if(scores[0] > scores[1]){
                   winCount[0]++;
                   System.out.println(winner(0));
               }
               else if(scores[0] < scores[1]){
                   winCount[1]++;
                   System.out.println(winner(1));
               }
               else System.out.println(\"Its a tie\");
           }
           char again = keepRolling();
           if(again == \'n\') break;
       }
       System.out.println(\"Player 1 won \" + winCount[0] + \" times\");
       System.out.println(\"Player 1 won \" + winCount[1] + \" times\");
   }
}

Question 3. (20 marks) Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score of zero and rolls two dice. o If the pl
Question 3. (20 marks) Write a simple press-your-luck dice game. The game works as follows: Player 1 starts with a score of zero and rolls two dice. o If the pl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site