import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CrapsGame extends JFrame implements ActionListener
{
private JTextArea jtaGame;
private JButton jbtnPlay, jbtnClear, jbtnExit;
private JScrollPane scrollingResult;
private JPanel jpnlTop = new JPanel();
private JPanel jpnlBottom = new JPanel();
private int point;
private int rollCount;
public CrapsGame()
{
//Set the title and size
setTitle("Craps Game");
//Instantiate JTextArea
jtaGame = new JTextArea(10,1);
scrollingResult = new JScrollPane(jtaGame);
// Instantiate and register the Play button for clicks events:
jbtnPlay = new JButton("Play");
jbtnPlay.addActionListener(this);
// Instantiate and register the Clear button for clicks events:
jbtnClear = new JButton("Clear");
jbtnClear.addActionListener(this);
// Instantiate and register the Exit button for clicks events:
jbtnExit = new JButton("Exit");
jbtnExit.addActionListener(this);
//Assemble JPanels
jpnlTop.setLayout(new GridLayout(1, 1));
jpnlTop.add(scrollingResult);
jpnlBottom.setLayout(new GridLayout(1, 3));
jpnlBottom.add(jbtnPlay);
jpnlBottom.add(jbtnClear);
jpnlBottom.add(jbtnExit);
// Start to add the components to the JFrame:
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(jpnlTop, BorderLayout.NORTH);
pane.add(jpnlBottom, BorderLayout.SOUTH);
// Show the JFrame and set code to respond to the user clicking on the X:
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jpnlTop.setLayout(new GridLayout(1, 1));
jpnlTop.add(scrollingResult);
jpnlBottom.add(jbtnPlay);
jpnlBottom.add(jbtnClear);
jpnlBottom.add(jbtnExit);
// Show the JFrame and set code to respond to the user clicking on the X:
setVisible(true);
setSize(320,480);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void startNewGame()
{
this.point = 0;
this.rollCount = 0;
this.writeMessage(">>>>>>>>>>>>> New Game >>>>>>>>>>>>>");
}
public void play()
{
int die1 = getDiceRoll();
int die2 = getDiceRoll();
this.rollCount++;
int sum = die1 + die2;
this.writeMessage(String.format("You rolled %d + %d = %d", die1, die2, sum));
if(this.rollCount > 0 && this.point == sum)
{
this.writeMessage("You win because your roll matches point ;)");
this.startNewGame();
return;
}
if(sum == 7 || sum == 11)
{
this.writeMessage("You win because you got a natural :)");
this.startNewGame();
return;
}
if(sum == 2 || sum == 3 || sum == 12)
{
this.writeMessage("You lost because you got a crap out :(");
this.startNewGame();
return;
}
if(this.rollCount == 1)
{
this.point = sum;
this.writeMessage("Point is: " + this.point);
return;
}
}
public int getDiceRoll()
{
return (int)(Math.random()*6) +1;
}
public void writeMessage(String sOutput)
{
jtaGame.append(sOutput + "\n");
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtnPlay)
{
this.play();
}
else if(e.getSource() == jbtnExit)
{
System.exit(0);
}
else if(e.getSource() == jbtnClear)
{
this.startNewGame();
}
}
public static void main(String args[])
{
new CrapsGame();
}
}