Question
The question is related to problem 6.33 from Java how to program 9th edition. with a twist. (Craps Game Modification) Modify the craps program of
The question is related to problem 6.33 from Java how to program 9th edition. with a twist.
(Craps Game Modification) Modify the craps program of Fig. 6.8 to allow wagering. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to bankBalance, and if its not, have the user reenter wager until a valid wager is entered. Then, run one game of craps. If the player wins, increase bankBalance by wager and display the new bankBalance. If the player loses, decrease bankBalance by wager, display the new bank- Balance, check whether bankBalance has become zero and, if so, display the message "Sorry. You busted!" As the game progresses, display various messages to create some chatter, such as "Oh, you're going for broke, huh?" or "Aw c'mon, take a chance!" or "You're up big. Now's the time to cash in your chips!". Implement the chatter as a separate method that randomly chooses the string to display.
The addition is the following:
Add a method called wager() so that you could ask for the wager and the program will return the wager amount.
create an overloaded method balance(), that takes either one or two parameters: (balance) (balance, wager). Balance() returns a type Boolean (True/False), if Balance is > 0 then returns True else False. If Balance - wager > 0 then return True else False.
I have the following code and am not getting it yet. Please help. Thank you.
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Craps extends JApplet implements ActionListener { boolean firstRoll = true; int dieSum; int myPoint; int gameStatus; int bankBalance, wager;
JLabel die1Label, die2Label, sumLabel, pointLabel, betLabel; JTextField firstDie, secondDie, sum, point, better, chatter; JButton roll; final int WON = 0, LOST = 1, CONTINUE = 2;
public void init() { bankBalance = 1000; gameStatus = CONTINUE;
betLabel = new JLabel( "bet:" ); better = new JTextField( "100", 10 ); die1Label = new JLabel( "Die 1" ); firstDie = new JTextField( 10 ); firstDie.setEditable( false ); die2Label = new JLabel( "Die 2" ); secondDie = new JTextField( 10 ); secondDie.setEditable( false ); sumLabel = new JLabel( "Sum is" ); sum = new JTextField( 10 ); sum.setEditable( false ); roll = new JButton( "Roll Dice" ); roll.addActionListener( this ); pointLabel = new JLabel( "Point is" ); point = new JTextField( 10 ); point.setEditable( false ); chatter = new JTextField( 25 ); chatter.setEditable( false );
Container container = getContentPane(); container.setLayout( new FlowLayout() ); container.add( die1Label ); container.add( firstDie ); container.add( die2Label ); container.add( secondDie ); container.add( sumLabel ); container.add( sum ); container.add( pointLabel ); container.add( point ); container.add( betLabel ); container.add( better ); container.add( roll ); container.add( chatter ); }
public void play() { if ( firstRoll ) { dieSum = rollDice();
switch ( dieSum ) {
case 7: case 11: gameStatus = WON;
point.setText( "" ); firstRoll = true; break;
case 2: case 3: case 12: gameStatus = LOST; point.setText( "" ); firstRoll = true; break;
default: gameStatus = CONTINUE; myPoint = dieSum; point.setText( Integer.toString( myPoint ) ); firstRoll = false; break; } }
else { dieSum = rollDice();
if ( dieSum == myPoint ) gameStatus = WON;
else if ( dieSum == 7 ) gameStatus = LOST; }
if ( gameStatus == CONTINUE ) showStatus( "($ " + bankBalance + ") Roll again." );
else {
if ( gameStatus == WON ) { bankBalance += wager; showStatus( "($ " + bankBalance + ") Player wins. " + "Click Roll Dice to play again." ); }
else { bankBalance -= wager; checkBalance(); showStatus( "($ " + bankBalance + ") Player loses. " + "Click Roll Dice to play again." ); }
better.setEditable( true ); firstRoll = true; } }
void checkBalance() { if ( bankBalance <= 0 ) { System.out.println( "Sorry. You busted!" ); System.exit( 0 ); } }
public void actionPerformed( ActionEvent e ) { int wage = Integer.parseInt( better.getText() );
if ( wage > bankBalance || wage < 0 ) showStatus( "( $" + bankBalance + " ) " + "Enter a valid wager!" ); else { wager = wage; better.setEditable( false ); play(); }
chatter.setText( chatter() ); }
public int rollDice() { int die1, die2, workSum;
die1 = 1 + ( int ) ( Math.random() * 6 ); die2 = 1 + ( int ) ( Math.random() * 6 ); workSum = die1 + die2;
firstDie.setText( Integer.toString( die1 ) ); secondDie.setText( Integer.toString( die2 ) ); sum.setText( Integer.toString( workSum ) );
return workSum; }
public String chatter() { String statement = null;
switch ( ( int ) ( Math.random() * 5 ) ) { case 0: statement = "Oh, you're going for broke huh?"; break;
case 1: statement = "Aw cmon, take a chance!"; break;
case 2: statement = "You're up big. Now's the " + "time to cash in your chips!"; break;
case 3: statement = "You're way too lucky! I think you're a cheat!!!"; break;
case 4: statement = "I'm betting all my money on you."; break; }
return statement; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started