Write a sample pressyourluck dice game The game works as fol

Write a sample press-your-luck dice game. The game works as follows: Player 1 starts with a score of zero and rolls two dice. If the player rolls 7, 2, or 12 their turn is over and they lose. If the player rolls any other sum it 1s added to their total score. The player can stop rolling at any time - remember if their last rolls was a 7, 2 or 12 they lose and player 2 wins. After player 1 stops rolling, player 2 rolls the two dice 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() return a random valid die value between 1 and 6 public static int newScore(int oldScore, int rollValue) 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. Your game should be able to play a complete human vs. Human game. No sample output. Be creative (but kind to your users). 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

Human VS Human Game Code:
===============================================
import java.util.*;
public class PressYourLuck{

public static int rollDie()
{
Random ran = new Random();
int x = ran.nextInt(6)+1;
return x;
}
public static int newScore(int oldScore,int rollValue)
{
if ((rollValue==7)||(rollValue==2)||(rollValue==12)){
return -1;
}
else{
return (oldScore+rollValue);
}
}
public static int whoGoesFirst()
{
Random ran = new Random();
int x = ran.nextInt(2)+1;
return x;
}
public static String winner(int playerNumber)
{
return (\"Player \"+playerNumber+\" wins the game\");
}
public static char keepRolling()
{
System.out.print(\"Do you want to continue to Roll (y or n) : \");
Scanner scan = new Scanner(System.in);
char s = scan.next().trim().charAt(0);
if((s==\'y\')||(s==\'n\')){
return s;
}
else{
System.out.print(\"Please enter correct option\");
return keepRolling();
}

}
public static int gamePlay(int oldScore,int playerNumber)
{
int firstDiceScore = rollDie();
int secondDiceScore = rollDie();
int totalDiceScore = firstDiceScore + secondDiceScore;
System.out.println(\"Player \"+playerNumber+\" current Dice score:\"+totalDiceScore);
int updatedScore = newScore(oldScore,totalDiceScore);
if (updatedScore==-1){
System.out.println(\"Player \"+playerNumber+\" just lost\");
return (oldScore+totalDiceScore);
}
else{
char c = keepRolling();
if (c==\'y\'){
return gamePlay(updatedScore,playerNumber);
}
else{
return updatedScore;
}
}
}
public static void main(String []args)
{
int firstPlayer = whoGoesFirst();
int secondPlayer;
if (firstPlayer==1){
secondPlayer = 2;
}
else{
secondPlayer = 1;
}
System.out.println(\"Player \"+firstPlayer+\" starts game\");
int firstPlayerScore = gamePlay(0,firstPlayer);
System.out.println(\"Player \"+firstPlayer+\" total Score \"+firstPlayerScore);
System.out.println(\"Player \"+secondPlayer+\" starts game\");
int secondPlayerScore = gamePlay(0,secondPlayer);
System.out.println(\"Player \"+secondPlayer+\" total Score \"+secondPlayerScore);
if(firstPlayerScore>secondPlayerScore){
System.out.println(winner(firstPlayer));
}
else if (firstPlayerScore<secondPlayerScore){
System.out.println(winner(secondPlayer));
}
else {
System.out.println(\"Game Draw\");
}
}
}
=========================================================

 Write a sample press-your-luck dice game. The game works as follows: Player 1 starts with a score of zero and rolls two dice. If the player rolls 7, 2, or 12 t
 Write a sample press-your-luck dice game. The game works as follows: Player 1 starts with a score of zero and rolls two dice. If the player rolls 7, 2, or 12 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site