Computer Science, asked by BrainlyProgrammer, 3 months ago

New Challenge for programmers!!

WAP in JAVA make a game of dice between 2 players.
On each player's turn check sum of total number outcomes becomes 20 or not.
If any one of the two player gets sum=20 first then that player won.
_____
Difficulty Level:- Hard but Easy
All the Best!!
#BeBrainly.

Answers

Answered by Anonymous
0

Explanation:

Input: s = “3662123”, X = 6

Output: 5

First player rolls and gets 3.

Second player rolls and gets 6, 6 and 2.

Third player rolls and gets 1.

Fourth player rolls and gets 2.

Fifth player rolls and gets 3.

Input: s = “1234223”, X = 2

Output: 4

Answered by atrs7391
2

package com.company;

import java.util.Scanner;

public class DiceGame {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       int infi = 1;

       while (infi%2 == 1) {

           int p1 = 0, p2 = 0;

           int d1, d2;

           System.out.println("------------------------------------------------------------- ");

           System.out.println("Dice game between two players. First to reach 20 points wins! ");

           System.out.println("------------------------------------------------------------- ");

           System.out.println(" ");

           System.out.println("Press 'enter' to start");

           sc.nextLine();

           System.out.println(" ");

           System.out.println(" ");

           System.out.println(" ");

           while (p1 < 20 && p2 < 20) {

               System.out.println("1st Player's turn, press 'enter' to roll the dice....");

               sc.nextLine();

               d1 = (int) (Math.random() * 6) + 1;

               p1 = d1 + p1;

               System.out.println("Dice Rolled Number: " + d1);

               System.out.println("Player 1 score: " + p1);

               System.out.println(" ");

               System.out.println(" ");

               System.out.println("2nd Player's turn, press 'enter' to roll the dice....");

               sc.nextLine();

               d2 = (int) (Math.random() * 6) + 1;

               p2 = d2 + p2;

               System.out.println("Dice Rolled Number: " + d2);

               System.out.println("Player 2 score: " + p2);

               System.out.println(" ");

               System.out.println(" ");

           }

           System.out.println(" ");

           System.out.println(" ");

           System.out.println(" ");

           System.out.println("Results:");

           System.out.println(" ");

           if (p1 >= 20) {

               System.out.println("Player 1 is winner! Congrats...");

           } else {

               System.out.println("Player 2 is winner! Congrats...");

           }

           System.out.println(" ");

           System.out.println(" ");

           System.out.println(" ");

           System.out.println("Enjoyed it! Wanna play again...");

           System.out.println("Press 1 (/odd numbers) to play again.");

           System.out.println("Press 2 (/even numbers) to quit the game.");

           infi = sc.nextInt();

           System.out.println(" ");

           System.out.println(" ");

           System.out.println(" ");

       }

   }

}

Similar questions