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