Write a simple pressyourluck dice game The game works as fol
Solution
PressYouLuckDiceGame.java
import java.util.Random;
 import java.util.Scanner;
public class PressYouLuckDiceGame {
   public static void main(String[] args) {
       
        //Declaring variables
        int number,sum,total_score=0,player = 0, pc_sum,old_score,pc_old_score,player1win=0,player2win=0,flag1=0,flag2=0;
         char ch;int pc_total_score =0;
       
        //Scanner class object is sued to read the inputs entered by the user
        Scanner sc=new Scanner(System.in);
       
        //This loop continues to execute until user enters character other than \'y\' or \'Y\'
        while(true)
        {
            //Calling the method to decide which player starts the game
            player=whoGoesFirst();
              
            //This loop continue to execute until either player#1 wins or player#2 wins
                while(true)
                {
                   
                    sum=0;
                      
                    //Player#1 rolling the dice
                    if(player==1)
                    {
                        //Throwing the dice for 2 times
                        for(int i=0;i<2;i++)
                        {
                    flag1=1;
                   
                    //Adding the sum
                        sum+=rollDie();
                        }
                       
                        //Displaying dice score when each time player#1 rolled the dice
                    System.out.println(\"\ Player#1 rolled \"+sum);
                   
                    //Calling the method to get the total score of player#1
                        total_score=newScore(total_score,sum);
                       
                        //Displaying the Player#1 total score
                        System.out.println(\"Player#1 Total Score :\"+total_score);
                       
                        //If the total score is -1 the display message that player#2 wins the game
                    if(total_score==-1)
                    {
                        System.out.println(\":: Player#1 Lost the game ::\");
                        System.out.println(\":: Player#2 Wins the Game ::\");
                        player2win++;
                        break;
                    }
                   
                    //Prompting whether player#1 want to roll again
                        ch=keepRolloing();
                        if(ch==\'y\' ||ch==\'Y\')
                        {
                        sum=0;
                        player=1;
                        continue;
                        }
                       
                        /* Checking whether player#1 total score is greater than player#2 total score or not
                        * If yes displaying player#1 wins the game
                        */
                    else if (flag2==1 && total_score>pc_total_score)
                    {
                        System.out.println(\":: Player 1 wins the game ::\");
                        player1win++;
                    break;
                    }
                        else
                        {
                        player=2;  
                        continue;
                        }
                    }
                    else if(player==2)
                    {
                        pc_sum = 0;
                       //Player #2 rolling the dice for 2 times
                        for(int i=0;i<2;i++)
                        {
                            flag2=1;
                        pc_sum+=rollDie();
                        }
                       
                        //Displaying dice score when each time player#2 rolled the dice
                        System.out.println(\"\ Player#2 rolled \"+pc_sum);
                       
                        //Calling the method to
                    pc_total_score = newScore(pc_total_score,pc_sum);
                   
                    //Displaying the player#2 total score
                    System.out.println(\"Player#2 Total Score :\"+pc_total_score);
                   
                    //If the total score is -1 the display message that player#1 wins the game
                    if(pc_total_score==-1)
                    {
                        System.out.println(\":: player#2 Lost the game ::\");
                        System.out.println(\":: Player#1 wins the game ::\");
                        player1win++;
                        break;
                    }
                    /* Checking whether player#2 total score is greater than player#1 total score or not
                        * If yes displaying player#2 wins the game
                        */
                    else if (flag1==1 && pc_total_score>total_score)
                    {
                        System.out.println(\":: Player 2 wins the game ::\");
                        player2win++;
                    break;
                    }
                    else
                    //Prompting whether player#2 want to roll again
                    ch=keepRolloing();
                        if(ch==\'y\' ||ch==\'Y\')
                        {
                        sum=0;
                        player=2;
                        continue;
                        }
                        else
                        {
                        player=1;  
                        continue;
                        }
                       
                    }
               
                }  
               
                //Displaying no of times player#1 wins the game
                System.out.println(\"\ No of times player#1 wins \"+player1win);
               
                //Displaying no of times player#2 wins the game
                System.out.println(\"No of times player#2 wins \"+player2win);
               
                //Getting the character from the user \'Y\' or \'y\' or \'N\' or \'n\'
                System.out.print(\"\ Do you want to play again (Y/N) ::\");
                char ch1 = sc.next(\".\").charAt(0);
                if(ch1==\'Y\'||ch1==\'y\')
                {
                total_score=0;
                pc_total_score=0;
                flag1=0;
                flag2=0;
                continue;  
                }  
                else
                {
                System.out.println(\":: Program Exit ::\");
                break;
                }
}
      
    }
   //This method decides which player starts the game
    private static int whoGoesFirst() {
        //Creating an Random Class Object
        Random r = new Random();
        //Rolling the first dice
          
        return r.nextInt(2) + 1;
    }
   //This method adds the dice score to the old score and return
    private static int newScore(int old_score, int sum) {
        int val=0;
        if(sum==2 ||sum==7 ||sum==12)
        {
            val=-1;
        }
        else
        val=old_score+sum;
        return val;
    }
   //This method prompts user whether he want to continue the game or not
    private static char keepRolloing() {
        Scanner sc=new Scanner(System.in);
        System.out.print(\"Do you want to continue(Y/N) ::\");
        char ch = sc.next(\".\").charAt(0);
        return ch;
    }
   //This method rolling the dice and return the dice score
    private static int rollDie() {
        int dice;
       
        //Creating an Random Class Object
        Random r = new Random();
       
        //Rolling the first dice
        dice= r.nextInt(6) + 1;
       
        return dice;
    }
}
______________________________________
Output:
 Player#2 rolled 6
 Player#2 Total Score :6
 Do you want to continue(Y/N) ::y
Player#2 rolled 7
 Player#2 Total Score :-1
 :: player#2 Lost the game ::
 :: Player#1 wins the game ::
No of times player#1 wins 1
 No of times player#2 wins 0
Do you want to play again (Y/N) ::y
Player#2 rolled 9
 Player#2 Total Score :9
 Do you want to continue(Y/N) ::y
Player#2 rolled 6
 Player#2 Total Score :15
 Do you want to continue(Y/N) ::y
Player#2 rolled 7
 Player#2 Total Score :-1
 :: player#2 Lost the game ::
 :: Player#1 wins the game ::
No of times player#1 wins 2
 No of times player#2 wins 0
Do you want to play again (Y/N) ::y
Player#2 rolled 10
 Player#2 Total Score :10
 Do you want to continue(Y/N) ::y
Player#2 rolled 8
 Player#2 Total Score :18
 Do you want to continue(Y/N) ::y
Player#2 rolled 7
 Player#2 Total Score :-1
 :: player#2 Lost the game ::
 :: Player#1 wins the game ::
No of times player#1 wins 3
 No of times player#2 wins 0
Do you want to play again (Y/N) ::y
Player#1 rolled 5
 Player#1 Total Score :5
 Do you want to continue(Y/N) ::y
Player#1 rolled 10
 Player#1 Total Score :15
 Do you want to continue(Y/N) ::n
Player#2 rolled 4
 Player#2 Total Score :4
 Do you want to continue(Y/N) ::y
Player#2 rolled 7
 Player#2 Total Score :-1
 :: player#2 Lost the game ::
 :: Player#1 wins the game ::
No of times player#1 wins 4
 No of times player#2 wins 0
Do you want to play again (Y/N) ::y
Player#1 rolled 6
 Player#1 Total Score :6
 Do you want to continue(Y/N) ::y
Player#1 rolled 7
 Player#1 Total Score :-1
 :: Player#1 Lost the game ::
 :: Player#2 Wins the Game ::
No of times player#1 wins 4
 No of times player#2 wins 1
Do you want to play again (Y/N) ::y
Player#2 rolled 5
 Player#2 Total Score :5
 Do you want to continue(Y/N) ::y
Player#2 rolled 11
 Player#2 Total Score :16
 Do you want to continue(Y/N) ::y
Player#2 rolled 7
 Player#2 Total Score :-1
 :: player#2 Lost the game ::
 :: Player#1 wins the game ::
No of times player#1 wins 5
 No of times player#2 wins 1
Do you want to play again (Y/N) ::y
Player#1 rolled 3
 Player#1 Total Score :3
 Do you want to continue(Y/N) ::y
Player#1 rolled 8
 Player#1 Total Score :11
 Do you want to continue(Y/N) ::y
Player#1 rolled 7
 Player#1 Total Score :-1
 :: Player#1 Lost the game ::
 :: Player#2 Wins the Game ::
No of times player#1 wins 5
 No of times player#2 wins 2
Do you want to play again (Y/N) ::y
Player#2 rolled 8
 Player#2 Total Score :8
 Do you want to continue(Y/N) ::y
Player#2 rolled 7
 Player#2 Total Score :-1
 :: player#2 Lost the game ::
 :: Player#1 wins the game ::
No of times player#1 wins 6
 No of times player#2 wins 2
Do you want to play again (Y/N) ::y
Player#1 rolled 6
 Player#1 Total Score :6
 Do you want to continue(Y/N) ::y
Player#1 rolled 10
 Player#1 Total Score :16
 Do you want to continue(Y/N) ::y
Player#1 rolled 10
 Player#1 Total Score :26
 Do you want to continue(Y/N) ::n
Player#2 rolled 9
 Player#2 Total Score :9
 Do you want to continue(Y/N) ::y
Player#2 rolled 6
 Player#2 Total Score :15
 Do you want to continue(Y/N) ::y
Player#2 rolled 10
 Player#2 Total Score :25
 Do you want to continue(Y/N) ::y
Player#2 rolled 4
 Player#2 Total Score :29
 :: Player 2 wins the game ::
No of times player#1 wins 6
 No of times player#2 wins 3
Do you want to play again (Y/N) ::n
 :: Program Exit ::
______________







