Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class CrapsGame extends JFrame
- {
- private static final int Width = 400;
- private static final int Height = 300;
- //JFrame Componenets
- private JTextArea jtaGame;
- private JButton jbtnPlay, jbtnClear, jbtnExit;
- private PlayButtonHandler playHandler;
- private ClearButtonHandler clearHandler;
- private ExitButtonHandler exitHandler;
- private JScrollPane scrollingResult;
- private JPanel jpnlTop = new JPanel();
- private JPanel jpnlBottom = new JPanel();
- public CrapsGame()
- {
- //Set the title and size
- setTitle("Craps Game");
- setSize(WIDTH, HEIGHT);
- //Instantiate JTextArea + Scrollabe
- jtaGame = new JTextArea(10,1);
- scrollingResult = new JScrollPane(jtaGame);
- // Instantiate and register the Play button for clicks events:
- jbtnPlay = new JButton("Play");
- playHandler = new PlayButtonHandler();
- jbtnPlay.addActionListener(playHandler);
- // Instantiate and register the Clear button for clicks events:
- jbtnClear = new JButton("Clear");
- clearHandler = new ClearButtonHandler();
- jbtnClear.addActionListener(clearHandler);
- // Instantiate and register the Exit button for clicks events:
- jbtnExit = new JButton("Exit");
- exitHandler = new ExitButtonHandler();
- jbtnExit.addActionListener(exitHandler);
- //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);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }//End Constructor
- public int rollDice()
- {
- int iDie = (int)(Math.random()*6) +1;
- return (iDie);
- }
- public void writeMessage(String sOutput)
- {
- jtaGame.append(sOutput);
- }
- private class PlayButtonHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- int iDie1, iDie2;
- iDie1 = rollDice();
- iDie2 = rollDice();
- int iSum = iDie1 + iDie2;
- String sRoll = "You rolled " + iDie1 + "+" + iDie2 + "=" + iSum + ".\n";
- String sOutput = "";
- int iPoint = 0;
- int iRollCount = 0;
- if(iSum == 7 || iSum == 11)
- {
- sOutput = sRoll + "You win because you got a natural :)\n";
- }
- else if(iSum == 2 || iSum == 3 || iSum == 12)
- {
- sOutput = sRoll + "You lost because you got a crap out :(\n";
- }
- else if(iRollCount == 0)
- {
- iPoint = iSum;
- sOutput = sRoll + "Point is: " + iPoint + ".\n";
- iRollCount++;
- }
- else if(iRollCount != 0 && iPoint == iSum)
- {
- sOutput = sRoll + "You win because your roll matches point ;)\n";
- }
- else if(iRollCount != 0 && iSum == 7)
- {
- sOutput = sRoll + "You lost because your roll matches 7 :(\n";
- }
- else
- {
- sOutput = sRoll;
- }
- writeMessage(sOutput);
- }
- }
- private class ExitButtonHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- }//end ExitButtonHandler
- private class ClearButtonHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- jtaGame.setText("");
- }
- } // end ClearButtonHandler
- public static void main(String args[])
- {
- // The only thing this main does is Instantiate the JFrame:
- // Note the name of the object is the name of the class from above.
- CrapsGame crGame = new CrapsGame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement