Advertisement
NewbProgrammer

FullCraps

Dec 19th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class CrapsGame extends JFrame
  6. {
  7.     private static final int Width = 400;
  8.     private static final int Height = 300;
  9.  
  10.     //JFrame Componenets
  11.     private JTextArea jtaGame;
  12.  
  13.     private JButton jbtnPlay, jbtnClear, jbtnExit;
  14.  
  15.     private PlayButtonHandler playHandler;
  16.     private ClearButtonHandler  clearHandler;
  17.     private ExitButtonHandler   exitHandler;
  18.  
  19.     private JScrollPane scrollingResult;
  20.  
  21.     private JPanel jpnlTop = new JPanel();
  22.     private JPanel jpnlBottom = new JPanel();
  23.  
  24.     public CrapsGame()
  25.     {
  26.         //Set the title and size
  27.         setTitle("Craps Game");
  28.         setSize(WIDTH, HEIGHT);
  29.  
  30.         //Instantiate JTextArea + Scrollabe
  31.         jtaGame = new JTextArea(10,1);
  32.         scrollingResult = new JScrollPane(jtaGame);
  33.  
  34.         // Instantiate and register the Play button for clicks events:
  35.         jbtnPlay = new JButton("Play");
  36.         playHandler = new PlayButtonHandler();
  37.         jbtnPlay.addActionListener(playHandler);
  38.  
  39.         // Instantiate and register the Clear button for clicks events:
  40.         jbtnClear = new JButton("Clear");
  41.         clearHandler = new ClearButtonHandler();
  42.         jbtnClear.addActionListener(clearHandler);
  43.  
  44.         // Instantiate and register the Exit button for clicks events:
  45.         jbtnExit = new JButton("Exit");
  46.         exitHandler = new ExitButtonHandler();
  47.         jbtnExit.addActionListener(exitHandler);
  48.  
  49.         //Assemble JPanels
  50.         jpnlTop.setLayout(new GridLayout(1, 1));
  51.         jpnlTop.add(scrollingResult);
  52.  
  53.         jpnlBottom.setLayout(new GridLayout(1, 3));
  54.         jpnlBottom.add(jbtnPlay);
  55.         jpnlBottom.add(jbtnClear);
  56.         jpnlBottom.add(jbtnExit);
  57.  
  58.         // Start to add the components to the JFrame:
  59.         Container pane = getContentPane();
  60.         pane.setLayout(new BorderLayout());
  61.  
  62.         pane.add(jpnlTop, BorderLayout.NORTH);
  63.         pane.add(jpnlBottom, BorderLayout.SOUTH);
  64.  
  65.         // Show the JFrame and set code to respond to the user clicking on the X:
  66.         setVisible(true);
  67.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  68.  
  69.         jpnlTop.setLayout(new GridLayout(1, 1));
  70.         jpnlTop.add(scrollingResult);
  71.  
  72.         jpnlBottom.add(jbtnPlay);
  73.         jpnlBottom.add(jbtnClear);
  74.         jpnlBottom.add(jbtnExit);
  75.  
  76.         // Show the JFrame and set code to respond to the user clicking on the X:
  77.         setVisible(true);
  78.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  79.     }//End Constructor
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.     public int rollDice()
  87.     {
  88.         int iDie = (int)(Math.random()*6) +1;
  89.  
  90.         return (iDie);
  91.     }
  92.  
  93.     public void writeMessage(String sOutput)
  94.     {
  95.         jtaGame.append(sOutput);
  96.     }
  97.  
  98.     private class PlayButtonHandler implements ActionListener
  99.     {
  100.  
  101.  
  102.  
  103.         public void actionPerformed(ActionEvent e)
  104.         {
  105.  
  106.             int iDie1, iDie2;
  107.  
  108.             iDie1 = rollDice();
  109.             iDie2 = rollDice();
  110.  
  111.             int iSum = iDie1 + iDie2;
  112.  
  113.             String sRoll = "You rolled " + iDie1 + "+" + iDie2 + "=" + iSum + ".\n";
  114.             String sOutput = "";
  115.             int iPoint = 0;
  116.             int iRollCount = 0;
  117.  
  118.             if(iSum == 7 || iSum == 11)
  119.             {
  120.                 sOutput = sRoll + "You win because you got a natural :)\n";
  121.             }
  122.             else if(iSum == 2 || iSum == 3 || iSum == 12)
  123.             {
  124.                 sOutput = sRoll + "You lost because you got a crap out :(\n";
  125.             }
  126.             else if(iRollCount == 0)
  127.             {
  128.                 iPoint = iSum;
  129.                 sOutput = sRoll + "Point is: " + iPoint + ".\n";
  130.                 iRollCount++;
  131.             }
  132.             else if(iRollCount != 0 && iPoint == iSum)
  133.             {
  134.                 sOutput = sRoll + "You win because your roll matches point ;)\n";
  135.             }
  136.             else if(iRollCount != 0 && iSum == 7)
  137.             {
  138.                 sOutput = sRoll + "You lost because your roll matches 7 :(\n";
  139.             }
  140.             else
  141.             {
  142.                 sOutput = sRoll;
  143.             }
  144.  
  145.             writeMessage(sOutput);
  146.  
  147.         }
  148.  
  149.     }
  150.  
  151.  
  152.  
  153.  
  154.     private class ExitButtonHandler implements ActionListener
  155.     {
  156.         public void actionPerformed(ActionEvent e)
  157.         {
  158.             System.exit(0);
  159.         }
  160.     }//end ExitButtonHandler
  161.  
  162.  
  163.     private class ClearButtonHandler implements ActionListener
  164.     {
  165.         public void actionPerformed(ActionEvent e)
  166.         {
  167.             jtaGame.setText("");
  168.          }
  169.     } // end ClearButtonHandler
  170.  
  171.  
  172.     public static void main(String args[])
  173.     {
  174.         // The only thing this main does is Instantiate the JFrame:
  175.         // Note the name of the object is the name of the class from above.
  176.         CrapsGame crGame = new CrapsGame();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement