1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class CrapsGame extends JFrame implements ActionListener
  7. {
  8.  
  9.  
  10.     private JTextArea jtaGame;
  11.     private JButton jbtnPlay, jbtnClear, jbtnExit;
  12.     private JScrollPane scrollingResult;
  13.     private JPanel jpnlTop = new JPanel();
  14.     private JPanel jpnlBottom = new JPanel();
  15.     private int point;
  16.     private int rollCount;
  17.    
  18.     public CrapsGame()
  19.     {
  20.         //Set the title and size
  21.         setTitle("Craps Game");
  22.  
  23.         //Instantiate JTextArea
  24.         jtaGame = new JTextArea(10,1);
  25.         scrollingResult = new JScrollPane(jtaGame);
  26.  
  27.         // Instantiate and register the Play button for clicks events:
  28.         jbtnPlay = new JButton("Play");
  29.         jbtnPlay.addActionListener(this);
  30.  
  31.         // Instantiate and register the Clear button for clicks events:
  32.         jbtnClear = new JButton("Clear");
  33.         jbtnClear.addActionListener(this);
  34.  
  35.         // Instantiate and register the Exit button for clicks events:
  36.         jbtnExit = new JButton("Exit");
  37.         jbtnExit.addActionListener(this);
  38.  
  39.         //Assemble JPanels
  40.         jpnlTop.setLayout(new GridLayout(1, 1));
  41.         jpnlTop.add(scrollingResult);
  42.  
  43.         jpnlBottom.setLayout(new GridLayout(1, 3));
  44.         jpnlBottom.add(jbtnPlay);
  45.         jpnlBottom.add(jbtnClear);
  46.         jpnlBottom.add(jbtnExit);
  47.  
  48.         // Start to add the components to the JFrame:
  49.         Container pane = getContentPane();
  50.         pane.setLayout(new BorderLayout());
  51.  
  52.         pane.add(jpnlTop, BorderLayout.NORTH);
  53.         pane.add(jpnlBottom, BorderLayout.SOUTH);
  54.  
  55.         // Show the JFrame and set code to respond to the user clicking on the X:
  56.         setVisible(true);
  57.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  58.  
  59.         jpnlTop.setLayout(new GridLayout(1, 1));
  60.         jpnlTop.add(scrollingResult);
  61.  
  62.         jpnlBottom.add(jbtnPlay);
  63.         jpnlBottom.add(jbtnClear);
  64.         jpnlBottom.add(jbtnExit);
  65.  
  66.         // Show the JFrame and set code to respond to the user clicking on the X:
  67.         setVisible(true);
  68.        
  69.         setSize(320,480);
  70.        
  71.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  72.     }
  73.  
  74.     public void startNewGame()
  75.     {
  76.         this.point = 0;
  77.         this.rollCount = 0;
  78.         this.writeMessage(">>>>>>>>>>>>> New Game >>>>>>>>>>>>>");
  79.     }
  80.    
  81.     public void play()
  82.     {
  83.         int die1 = getDiceRoll();
  84.         int die2 = getDiceRoll();
  85.        
  86.         this.rollCount++;
  87.  
  88.         int sum = die1 + die2;
  89.  
  90.         this.writeMessage(String.format("You rolled %d + %d = %d", die1, die2, sum));
  91.  
  92.         if(this.rollCount > 0 && this.point == sum)
  93.         {
  94.             this.writeMessage("You win because your roll matches point ;)");
  95.             this.startNewGame();
  96.             return;
  97.         }
  98.        
  99.         if(sum == 7 || sum == 11)
  100.         {
  101.             this.writeMessage("You win because you got a natural :)");
  102.             this.startNewGame();
  103.             return;
  104.         }
  105.        
  106.         if(sum == 2 || sum == 3 || sum == 12)
  107.         {
  108.             this.writeMessage("You lost because you got a crap out :(");
  109.             this.startNewGame();
  110.             return;
  111.         }
  112.         if(this.rollCount == 1)
  113.         {
  114.             this.point = sum;
  115.             this.writeMessage("Point is: " + this.point);
  116.             return;
  117.         }
  118.     }
  119.  
  120.     public int getDiceRoll()
  121.     {
  122.         return (int)(Math.random()*6) +1;
  123.     }
  124.  
  125.     public void writeMessage(String sOutput)
  126.     {
  127.         jtaGame.append(sOutput + "\n");
  128.     }
  129.    
  130.     @Override
  131.     public void actionPerformed(ActionEvent e)
  132.     {
  133.         if(e.getSource() == jbtnPlay)
  134.         {
  135.             this.play();
  136.         }
  137.        
  138.         else if(e.getSource() == jbtnExit)
  139.         {
  140.             System.exit(0);
  141.         }
  142.        
  143.         else if(e.getSource() == jbtnClear)
  144.         {
  145.             this.startNewGame();
  146.         }
  147.     }
  148.  
  149.     public static void main(String args[])
  150.     {
  151.         new CrapsGame();
  152.     }
  153. }