-
December 3rd, 2012, 07:25 PM
#1
the craps game why isnt it working?
why isnt my program working?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Craps3 extends JFrame implements ActionListener
{
final int WON = 0,LOST =1, CONTINUE = 2;
boolean firstRoll = true;
int sumOfDice = 0;
int myPoint = 0;
int gameStatus = CONTINUE;
JLabel die1Label, die2Label, sumLabel, pointLabel, setBetLabel;
JTextField firstDie, secondDie, sum, point, setBet, display;
JButton roll;
GridLayout grid;
public Craps3()
{
Container c = getContentPane();
grid = new GridLayout(6,2,2,2); // 1
c.setLayout(grid);
setBetLabel = new JLabel(" Set your bet");
c.add(setBetLabel);
setBet = new JTextField();
setBet.addActionListener(this); // 2
c.add(setBet);
die1Label = new JLabel(" Die1");
c.add(die1Label);
firstDie = new JTextField();
firstDie.setEditable(false);
c.add(firstDie);
die2Label = new JLabel(" Die2");
c.add(die2Label);
secondDie = new JTextField();
secondDie.setEditable(false);
c.add(secondDie);
sumLabel = new JLabel(" Sum is");
c.add(sumLabel);
sum = new JTextField();
sum.setEditable(false);
c.add(sum);
pointLabel = new JLabel(" Point is");
c.add(pointLabel);
point = new JTextField();
point.setEditable(false);
c.add(point);
roll = new JButton(" Roll Dice");
roll.addActionListener(this);
c.add(roll);
display = new JTextField();
c.add(display);
setSize(200,300);
setLocation(300,200);
show();
}// end Craps3()
public void actionPerformed(ActionEvent e)
{
play();
}//end actionPerformed(ActionEvent e)
public void play()
{
if(firstRoll)
{
sumOfDice = rollDice();
switch(sumOfDice)
{
case 7: case 11:
gameStatus = WON;
point.setText("");
break;
case 2: case 3: case 12:
gameStatus = LOST;
point.setText("");
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
point.setText(Integer.toString(myPoint));
firstRoll = false;
break;
} // end switch
} // end if(firstRoll)
else
{
sumOfDice = rollDice();
if(sumOfDice == myPoint) gameStatus = WON;
if(sumOfDice == 7) gameStatus = LOST;
} //end else
if(gameStatus == CONTINUE)
display.setText("Roll Again");
if(gameStatus == WON)
{
display.setText(" Win!. Roll\n to play again");
firstRoll = true;
}
if(gameStatus == LOST)
{
display.setText(" Player Loses\n Play again");
firstRoll = true;
}
} // end play
public int rollDice()
{
int die1, die2, workSum;
die1 = 1 + (int)(6*Math.random());
die2 = 1 + (int)(6*Math.random());
workSum = die1 + die2;
firstDie.setText(Integer.toString(die1));
secondDie.setText(Integer.toString(die2));
sum.setText(Integer.toString(workSum));
return workSum;
} // end rollDice()
} // end Craps3
-
December 5th, 2012, 05:42 AM
#2
Re: the craps game why isnt it working?
Help us help you - post the code using the [code][/code] tags in order to preserve formatting, tell if it compiles or not, if there are any error messages, what they are saying, etc... Also, don't just say "it doesn't work" - that could mean anything - again, does it compile, if it does, tell us what do you want it to do, what did you expect and what happened instead.
Nobody is gonna bother reading your code or help you when you just say "solve this for me" and paste a wall of text. We normally aren't acting as human compilers here, nor do we have a crystal ball. Elaborate on the problem a bit.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|