Question

Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

Please i need helpe with this JAVA code.

Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows:

Problem Statement

One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a

7 or 11, it is called a natural. The player who rolled the dice wins.

2, 3 or 12, it is called . The player who rolled the dice loses.

If the player’s first roll is any other number (4, 5, 6, 8, 9, or 10), the bet is not

immediately decided. Instead, the number that the player rolled becomes the point and he continues to roll the dice until either he rolls the point a 2nd time, in which case he wins, or he rolls a 7, in which case he loses. For example, suppose a player’s 1st roll is a 6. Then 6 becomes the point. He must roll again. If his next roll were a 3, he would have to roll a 3rd time. The point remains 6. He continues to roll the dice until he either gets a 6 (a win) or a 7 (a loss).

When a player wins, he collects the bet, and gets to keep the dice and take another turn. Then the play begins again with rule (1)

When a player loses, his opponent collects the money that was bet and it becomes the opponent’s turn to roll the dice. The opponent starts play again with rule (1)

For this program, simulate a craps game for a total of at most 20 wagers. That is, the first player rolls the dice until he either wins or loses. That is the first wager. The winner then takes the dice, and rolls until the second wager is decided. And so on, for a total of 20 wagers. However, if one of the gamblers loses all of his money before the 20th wager, end the simulation at that point.

Assume that each player starts out with a balance of $1000. Assume also that when player 1 is rolling, he always bets $100 on each wager. However, player 2 believes in luck and uses a strategy of his own creation. When he has the dice and it is his turn to decide how much to bet, he considers how much money he has. If he has at least as much money as he had when the game began, he decides his luck is good and bets $150. If he has less money than when the game began, he decides his luck is bad, and only bets $50 on that particular wager. Player 1 always rolls the dice first in this simulation.

You must use pseudo-random number generator to simulate a dice roll. You can use a method named GenerateRandomNumber to simulate the roll of one die with maximum roll of 6. Since the game is played with two dice, to simulate one roll you will have to generate two numbers by calling GenerateRandomNumber twice and then adding the two numbers together.

Your program must print out a roll-by-roll description of what happens as the game is played on the console and to a file named game.txt.

Note: your program must be user-friendly and intuitive. This is a part of your grade. In other words, even if your program does everything the problem statement states, your grade may be reduced because of difficulty to use it.

Input

This program uses no input

Output

A roll-by-roll description of what happens as the game is played should be print out on the console and should be written to a file as well.

******************************************************************** Wager 1 : Bet is $100
Player 1 is rolling the dice
The roll is 2

That is ! Player 1 loses.
Currently, Player 1 has $900 and Player 2 has $1100.

********************************************************************

Wager 2 : Bet is $150
Player 2 is rolling the dice.
The roll is a 6. The point is 6. Player 2 rolls again. The roll is a 5. The point is 6. Player 2 rolls again. The roll is a 6. That is the point! Player 2 wins. Currently, Player 1 has $750 and Player 2 has $1250.

********************************************************************

Your output need not look exactly like this, but should be at least as easy to read. At a minimum, for each wager you must print which player is rolling the dice, and the amount of the bet. Then simulate rolling the dice, and print the result of each roll, until the winner is determined. At the end of each wager, print who won, and a running total of how much money each player has. Note that it's possible that a gambler could end up with a negative amount of money, if he or she does not have enough money left to pay the full amount of the final bet.

Use of Methods, Parameters, Modularity, Design, etc.

Part of your grade on this and ALL future programming projects in this course will be determined by how well you use multiple functions and parameter passing appropriately and how well you design a modular and functionally cohesive program using the principles discussed in class. Large grade point penalties can be incurred for not setting up a modular, well designed program structure. This emphasizes good program structure, design, and fundamental software engineering principles.

In this project, you must at least define and call the following 4 methods:

public static int GenerateRandomNumber(); public static int RollDice();

public static String DecideWhoWin(int dice_player1, int dice_player2, PrintWriter pw);

public static void WriteToFile(PrintWriter pw, String data);

public static void AnnounceFinalWinner(int money_player1, int money_player2, PrintWriter pw)

This what i have done so far

public class CrapsGame{
   static int balance=1000,p1bet=100,p2bet=150,sum,roll,dice1,dice2,Player1,Player2,sumOfDice;
   static String P1="Player 1",P2="Player 2";
   static File file=new File("game.txt");
   static PrintWriter pw;
   enum Status{CONTINUE,WON,LOST};
   static Status gameStatus;
   public static void main(String[] args) throws IOException{
       int i=GenerateRandomNumber();
       System.out.println("Wager 1: Bet is $"+p1bet);
       System.out.println(P1+" is rolling the dice");
       System.out.println("The roll is "+i);
       sumOfDice=RollDice();
       switch(sumOfDice){
       case 7:
           System.out.println("You win");
           gameStatus=Status.WON;
           break;
       case 11:
           System.out.println("You win");
           gameStatus=Status.WON;
           break;
       case 2:
           System.out.println("That is ! Player loses.");
           gameStatus=Status.LOST;
           break;
       case 3:
           System.out.println("That is ! Player loses.");
           gameStatus=Status.LOST;
           break;
       case 12:
           System.out.println("That is ! Player loses.");
           gameStatus=Status.LOST;
           break;
       default:
           System.out.println("The roll is a "+i+" The point is "+i+". Player 2 rolls again.");
           gameStatus=Status.CONTINUE;
           break;
       }
       while(gameStatus==Status.CONTINUE){
           sumOfDice=RollDice();
       }
      
   }
   public static int GenerateRandomNumber(){
      
       // Generate a random number between 1 to 6
       roll=(int) (1 + Math.random()*6);
   return roll;
   }
   public static int RollDice(){
       dice1=GenerateRandomNumber();
       dice2=1+GenerateRandomNumber();
       sum=dice1+dice2;
       return sum;
      
   }
   public static String DecideWhoWin(int dice_player1,int dice_player2,PrintWriter pw) throws IOException{
       return null;
      
   }
   public static void WriteToFile(PrintWriter pw,String data){
      
      
   }
   public static void AnnounceFinalWinner(int money_player1,int money_player2,PrintWriter pw){
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code will be as below for the given requirements:

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

public class CrapsGame{

enum Status{CONTINUE,WON,LOST};

public static final String P1 = "Player 1", P2 = "Player 2";

public static String currPlayer = "Player 1";

public static final int initial_money = 1000;

public static int money_player1 = initial_money, money_player2 = initial_money;

public static final int bet_player1 = 100, bet1_player2 = 150, bet2_player2 = 50;

  

public static int generateRandomNumber(){

// Generate a random number between 1 to 6

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

return roll;

}

  

public static int rollDice(){

int dice1 = generateRandomNumber();

int dice2 = generateRandomNumber();

int roll = dice1+dice2;

return roll;

  

}

public static String decideWhoWin(int dice_player1,int dice_player2,PrintWriter pw) throws IOException{

int roll = (currPlayer.equals(P1)) ? dice_player1 : dice_player2;

String otherPlayer = (currPlayer.equals(P1)) ? P2 : P1;

Status gameStatus = Status.CONTINUE;

boolean continued = false;

int point = 0;

while(!(gameStatus==Status.WON || gameStatus==Status.LOST)) {

switch(roll){

case 7:

if(continued) {

String data = "That is ! " + currPlayer +" loses.";

WriteToFile(pw, data);

gameStatus=Status.LOST;

}else {

String data = "That is ! " + currPlayer +" wins.";

WriteToFile(pw, data);

gameStatus=Status.WON;

}

break;

case 11:

if(continued) {

roll = getRollAgain(roll, point,pw);

}else {

String data = "That is ! " + currPlayer +" wins.";

WriteToFile(pw, data);

gameStatus=Status.WON;

}

break;

case 2:

if(continued) {

roll = getRollAgain(roll, point,pw);

}else {

String data = "That is ! " + currPlayer +" loses.";

WriteToFile(pw, data);

gameStatus=Status.LOST;

}

break;

case 3:

if(continued) {

roll = getRollAgain(roll, point,pw);

}else {

String data = "That is ! " + currPlayer +" loses.";

WriteToFile(pw, data);

gameStatus=Status.LOST;

}

break;

case 12:

if(continued) {

roll = getRollAgain(roll, point,pw);

}else {

String data = "That is ! " + currPlayer +" loses.";

WriteToFile(pw, data);

gameStatus=Status.LOST;

}

break;

default:

if(roll==point) {

String data = "That is ! " + currPlayer +" wins.";

WriteToFile(pw, data);

gameStatus=Status.WON;

}else {

if(point ==0) {

point = roll;

}

roll = getRollAgain(roll, point,pw);

gameStatus=Status.CONTINUE;

continued = true;

}

break;

}

}

if(gameStatus==Status.WON) {

return currPlayer;

}else {

return otherPlayer;

}

}

  

private static int getRollAgain(int roll,int point,PrintWriter pw) {

String data = "The point is "+ point +". " + currPlayer +" rolls again.";

WriteToFile(pw, data);

roll = rollDice();

data = "The roll is: "+roll;

WriteToFile(pw, data);

return roll;

}

  

public static void WriteToFile(PrintWriter pw,String data){

System.out.println(data);

pw.println(data);

}

  

public static void announceFinalWinner(int money_player1,int money_player2,PrintWriter pw){

String data = null;

if(money_player1>money_player2) {

data = "Player1 is the winner";

}else if(money_player2>money_player1) {

data = "Player2 is the winner";

}else {

data = "This is a tie between Player1 and Player2";

}

WriteToFile(pw, data);

}

  

public static int getBet() {

if(currPlayer.equals(P1)) {

return bet_player1;

}else if(money_player2 < initial_money) {

return bet2_player2;

}else {

return bet1_player2;

}

}

  

  

public static void main(String[] args) throws IOException{

PrintWriter pw = new PrintWriter(new File("game.txt"));

int wagerCount = 1;

int dice_player1 = 0, dice_player2 = 0;

while(wagerCount<=20 && !(money_player1<0 || money_player2<0)) {

int bet = getBet();

String data = "Wager "+ wagerCount + ": Bet is $"+bet;

WriteToFile(pw, data);

data = currPlayer + " is rolling the dice";

WriteToFile(pw, data);

int roll = rollDice();

data = "The roll is: "+roll;

WriteToFile(pw, data);

dice_player1 = (currPlayer.equals(P1)) ? roll : 0;

dice_player2 = (currPlayer.equals(P2)) ? roll : 0;

currPlayer = decideWhoWin(dice_player1, dice_player2, pw);

money_player1 = (currPlayer.equals(P1)) ? money_player1+bet : money_player1-bet;

money_player2 = (currPlayer.equals(P2)) ? money_player2+bet : money_player2-bet;

data = "Currently, Player 1 has $"+ money_player1 + " and Player 2 has $" + money_player2 + ".";

WriteToFile(pw, data);

wagerCount++;

}

announceFinalWinner(money_player1, money_player2, pw);

pw.close();

  

}

}

Output:

Please ask your queries in comments if you have any.

Add a comment
Know the answer?
Add Answer to:
Please i need helpe with this JAVA code. Write a Java program simulates the dice game...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • 9. In the casino dice game Craps, players make wagers on a sequence of rolls of...

    9. In the casino dice game Craps, players make wagers on a sequence of rolls of a pair of dice. A sequence of rolls starts with the "shooter" making an initial roll of two dice called the "come-out” roll. If the sum of the dice on the initial roll is 7 or 11 then a player with a bet on the Pass Line wins. If the initial roll results in a sum of 2, 3, or 12 ("craps") then a...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

  • 2. "Craps" is a game played by rolling two fair dice. To play one round of...

    2. "Craps" is a game played by rolling two fair dice. To play one round of this game, the player rolls the dice and the outcome is determined by the following rules: If the total number of dots is 7 or 11 (a "natural"), then the player wins. If the total number of dots is 2, 3, or 12 C'craps"), then the player loses. If the total number of dots is 4, 5, 6,8,9, or 10, then this number is...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

  • I'm writing a program in game for a dice game. During the game you decide if...

    I'm writing a program in game for a dice game. During the game you decide if you want to roll or pass to the next player, mine so far will just keep rolling until you roll the same number again (besides six) and then move to the next player. How do I get it to manually change players by the player response "y" or "n"? This is what I have for this part of the game simulator and it doesn't...

  • . In the carnival game Under-or-Over-Seven, a pair of fair dice is rolled once, and the...

    . In the carnival game Under-or-Over-Seven, a pair of fair dice is rolled once, and the resulting sum determines whether the player wins or loses his or her bet. For example, the player can bet $1 that the sum will be under 7—that is, 2, 3, 4, 5, or 6. For this bet, the player wins $1 if the result is under 7 and loses $1 if the outcome equals or is greater than 7. Similarly, the player can bet...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT