-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackJackFrame.java
427 lines (392 loc) · 12.2 KB
/
BlackJackFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
*@author: Lenny Ardiles
*@description: A class for a single playing card
*@date: 05-05-2001
*Last Modified: 05-05-2001
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import cards.*;
public class BlackJackFrame extends JFrame{
JMenuBar jMenuBarBJ;
TripToCasino trip;
Dealer dealer;
public JPanel jPanelData;
public BlackJackTable jPanelDealer;// = new BlackJackTable();
public BlackJackTable jPanelPlayer;// = new BlackJackTable();
public JPanel jPanelControls = new JPanel();
JTextField jTextFieldPlayer;
JTextField jTextFieldCash;
JTextField jTextFieldBet;
JTextField jTextFieldGameWinnings;
// JTextField jTextFieldTotalWinnings; //add Later
JButton jButtonSplit;
JButton jButtonDouble;
JButton jButtonStay;
JButton jButtonHit;
JButton jButtonDeal;
JMenuItem jMenuItemDeal;
JCheckBoxMenuItem jMenuItemShowDeck;
ResultImage image;
JProgressBar jProgressBarDeck;
private JLabel jLabelPlayer;
private JLabel jLabelCash;
private JLabel jLabelBet;
private JLabel jLabelGameWinnings;
// private JLabel jLabelTotalWinnings; //add Later
private String dealerName;
private String playerName;
///TestingCardImage
/// CardImage c1 = new CardImage( new String("S"), new String("A") );
/// CardImage c2 = new CardImage( new String("D"), new String("10") );
BlackJackFrame(){
setMenu();
loadPlayer();
dealer = new Dealer( this );
trip.setBet(10); //Start Game at $10 bets
setTable();
}
public void setTable(){
setTitle("OX BlackJack Table");
setSize(500,350);
setLocation(200,200);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
getContentPane().setLayout( new GridLayout(4,0) );
setJPanelData();
getContentPane().add(jPanelData);
setJPanelDealer();
getContentPane().add(jPanelDealer);
setJPanelPlayer();
getContentPane().add(jPanelPlayer);
setJPanelControls();
getContentPane().add(jPanelControls);
setVisible(true);
addKeyListener( new KeyAdapter(){
public void keyTyped( KeyEvent e ){
if( e.getKeyCode() == KeyEvent.VK_D ){
deal();
}
}
});
/* addWindowListener( new WindowAdapter(){
public void WindowClosed( WindowEvent e ){
exitGame();
}
public void WindowClosing( WindowEvent e ){
exitGame();
}
});
*/ }
public void setJPanelData(){
jPanelData = new JPanel();
jPanelData.setLayout( new GridLayout(2,0) );
/////
Box boxCash = new Box( BoxLayout.Y_AXIS );
jLabelGameWinnings = new JLabel("Game Winnings");
boxCash.add(jLabelGameWinnings);
jTextFieldGameWinnings = new JTextField(7);
jTextFieldGameWinnings.setHorizontalAlignment(JTextField.RIGHT);
jTextFieldGameWinnings.setEditable(false);
setWinnings();
boxCash.add(jTextFieldGameWinnings);
JPanel jPanelTopData = new JPanel();
jPanelTopData.add( boxCash );
jPanelData.add( jPanelTopData );
/////
jProgressBarDeck = new JProgressBar(
new DefaultBoundedRangeModel( 4*52, 0, 0, 4*52 ) ); //Default 4 decks
jProgressBarDeck.setString(""+jProgressBarDeck.getValue());
jProgressBarDeck.addChangeListener( new ChangeListener(){
public void stateChanged( ChangeEvent e ){
jProgressBarDeck.setString(""+jProgressBarDeck.getValue());
}
});
jProgressBarDeck.setStringPainted( true );
JPanel jPanelBottomData = new JPanel();
jPanelBottomData.add(jProgressBarDeck);
jPanelData.add(jPanelBottomData);
}
public void setJPanelDealer(){
jPanelDealer = new BlackJackTable( dealer.dealerCards );
//jPanelDealer.setLayout( new CardLayout() );
jPanelDealer.setLayout( new FlowLayout() );
/// jPanelDealer.add( c1 );
}
public void setJPanelPlayer(){
jPanelPlayer = new BlackJackTable( dealer.playerCards );
//jPanelPlayer.setLayout( new CardLayout() );
jPanelPlayer.setLayout( new FlowLayout() );
/// jPanelPlayer.add( c2 );
/// jPanelPlayer.add( new Card( 14, 0 ));
}
public void setJPanelControls(){
jPanelControls.setLayout( new GridLayout(2,0) );
/////
JPanel jPanelDisplay = new JPanel();
jLabelPlayer = new JLabel("Player");
jPanelDisplay.add(jLabelPlayer);
jTextFieldPlayer = new JTextField(playerName.length());
jTextFieldPlayer.setText(playerName);
jTextFieldPlayer.setEditable(false);
jPanelDisplay.add(jTextFieldPlayer);
jLabelCash = new JLabel("Cash");
jPanelDisplay.add(jLabelCash);
jTextFieldCash = new JTextField(7);
jTextFieldCash.setHorizontalAlignment(JTextField.RIGHT);
jTextFieldCash.setText("$"+trip.getCashOnHand());
jTextFieldCash.setEditable(false);
jPanelDisplay.add(jTextFieldCash);
jLabelBet = new JLabel("Bet");
jPanelDisplay.add(jLabelBet);
jTextFieldBet = new JTextField(5);
jPanelDisplay.add(jTextFieldBet);
setBet();
image = new ResultImage();
image.setSize(18,26);
jPanelDisplay.add(image);
jPanelControls.add(jPanelDisplay);
/////
JPanel jPanelButtons = new JPanel();
jPanelButtons.setLayout( new FlowLayout() );
jButtonSplit = new JButton("Split");
jPanelButtons.add(jButtonSplit);
jButtonDouble = new JButton("Double");
jPanelButtons.add(jButtonDouble);
jButtonStay = new JButton("Stay");
jPanelButtons.add(jButtonStay);
jButtonHit = new JButton("Hit");
jPanelButtons.add(jButtonHit);
jButtonDeal = new JButton("Deal");
jPanelButtons.add(jButtonDeal);
setActiveButtons();
jPanelControls.add(jPanelButtons);
jButtonDeal.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
deal();
}
});
jButtonHit.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.hit();
setActiveButtons();
}
});
jButtonStay.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.stay();
setActiveButtons();
}
});
jButtonDouble.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.doublePlay();
setActiveButtons();
}
});
jButtonSplit.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.split();
setActiveButtons();
}
});
}
private void setActiveButtons(){
if( dealer.state == dealer.BID ){
jButtonDeal.setEnabled(true);
jMenuItemDeal.setEnabled(true);
jButtonHit.setEnabled(false);
jButtonStay.setEnabled(false);
jButtonDouble.setEnabled(false);
jButtonSplit.setEnabled(false);
jTextFieldBet.setEditable(true);
}else if ( dealer.state == dealer.PLAY ){
jButtonDeal.setEnabled(false);
jMenuItemDeal.setEnabled(false);
jButtonHit.setEnabled(true);
jButtonStay.setEnabled(true);
if(dealer.playerCards.size() < 3){
jButtonDouble.setEnabled(true);
jButtonSplit.setEnabled(true);
}else{
jButtonDouble.setEnabled(false);//if first play else setEnabled(false)
jButtonSplit.setEnabled(false);//if first play else setEnabled(false)
}
jTextFieldBet.setEditable(false);
}
if( trip.getCashOnHand() < 10 ){
JOptionPane.showMessageDialog( this, "You have NO MORE MONEY left.\nPlay again Later!",
"OX SAYS GET OTTA MY CASINO", JOptionPane.INFORMATION_MESSAGE );
exitGame();
}
}
public void setMenu(){
jMenuBarBJ = new JMenuBar();
JMenu jMenuGame = new JMenu("Game");
jMenuItemDeal = new JMenuItem("Deal");
jMenuGame.add(jMenuItemDeal);
JMenuItem jMenuItemShuffle = new JMenuItem("Shuffle");
jMenuGame.add(jMenuItemShuffle);
JMenuItem jMenuItemChangePlayer = new JMenuItem("Change Player");
jMenuGame.add(jMenuItemChangePlayer);
jMenuGame.addSeparator();
JMenuItem jMenuItemExit = new JMenuItem("Exit");
jMenuGame.add(jMenuItemExit);
JMenu jMenuOptions = new JMenu("Options");
JMenuItem jMenuDecks = new JMenu("NumberOfDecks");
JMenuItem jMenuItemDisplayStatistics = new JMenuItem("Display Statistics");
jMenuOptions.add(jMenuItemDisplayStatistics);
JCheckBoxMenuItem jMenuItemOne = new JCheckBoxMenuItem("1");
jMenuDecks.add(jMenuItemOne);
JCheckBoxMenuItem jMenuItemTwo = new JCheckBoxMenuItem("2");
jMenuDecks.add(jMenuItemTwo);
JCheckBoxMenuItem jMenuItemFour = new JCheckBoxMenuItem("4", true);
jMenuDecks.add(jMenuItemFour);
jMenuOptions.add(jMenuDecks);
jMenuItemShowDeck = new JCheckBoxMenuItem("Show Cards in Deck", true);
jMenuOptions.add(jMenuItemShowDeck);
jMenuGame.setMnemonic('G');
jMenuItemDeal.setMnemonic('D');
jMenuItemShuffle.setMnemonic('S');
jMenuItemChangePlayer.setMnemonic('C');
jMenuItemExit.setMnemonic('x');
jMenuOptions.setMnemonic('O');
jMenuItemDisplayStatistics.setMnemonic('S');
jMenuDecks.setMnemonic('N');
jMenuItemShowDeck.setMnemonic('C');
ButtonGroup bg = new ButtonGroup();
bg.add(jMenuItemOne);
bg.add(jMenuItemTwo);
bg.add(jMenuItemFour);
jMenuBarBJ.add(jMenuGame);
jMenuBarBJ.add(jMenuOptions);
setJMenuBar(jMenuBarBJ);
jMenuItemDeal.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
deal();
}
});
jMenuItemShuffle.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.shuffle();
}
});
jMenuItemChangePlayer.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
changePlayer();
}
});
jMenuItemExit.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
exitGame();
}
});
jMenuItemDisplayStatistics.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
StatisticsWindow stats = new StatisticsWindow();
stats.setData( trip );
stats.setSize( 170, 300 );
stats.setLocation( 300, 150 );
stats.show();
//stats.setVisible(true);
}
});
jMenuItemOne.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.setNumberOfDecks( 1 );
((BoundedRangeModel)jProgressBarDeck.getModel()).setRangeProperties(
1*52, 0, 0, 1*52, false);
}
});
jMenuItemTwo.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.setNumberOfDecks( 2 );
((BoundedRangeModel)jProgressBarDeck.getModel()).setRangeProperties(
2*52, 0, 0, 2*52, false );
}
});
jMenuItemFour.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
dealer.setNumberOfDecks( 4 );
((BoundedRangeModel)jProgressBarDeck.getModel()).setRangeProperties(
4*52, 0, 0, 4*52, false );
}
});
jMenuItemShowDeck.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
if(jMenuItemShowDeck.getState()){
jProgressBarDeck.setVisible(true);
}else{
jProgressBarDeck.setVisible(false);
}
}
});
}
private String getPlayerName(){
playerName = JOptionPane.showInputDialog(this, "Player Name",
"Enter your name",JOptionPane.QUESTION_MESSAGE);
if((playerName == null)||(playerName.length() < 1)){
playerName = "Default";
}
return playerName;
}
private void loadPlayer(){
trip = new TripToCasino( getPlayerName() );
}
private void changePlayer(){
loadPlayer();
jTextFieldPlayer.setText(playerName);
}
private void setWinnings(){
jTextFieldGameWinnings.setText(""+trip.tripWinnings());
}
public void setMoneyField( JTextField moneyField, int money ){
if( money >= 0 ){
moneyField.setForeground( Color.black );
moneyField.setText( "$" + money );
}else{
moneyField.setForeground( Color.black );
moneyField.setText( "-$" + Math.abs(money) );
}
}
private void setBet(){
jTextFieldBet.setText(""+trip.getBet());
}
private void deal(){
if( isValidBet() ){
trip.setBet( new Integer(
jTextFieldBet.getText().trim()).intValue() );
dealer.deal();
setActiveButtons();
}else{
invalidBet();
}
jPanelDealer.repaint();
jPanelPlayer.repaint();
}
private boolean isValidBet(){
try{
if( new Integer(jTextFieldBet.getText().trim()).intValue()%10 == 0 ){
return true;
}
}catch( NumberFormatException nfe ){
System.out.println("Bet is not a valid number");
}
return false;
}
private void invalidBet(){
JOptionPane.showMessageDialog(null,
"Bet must be a multiple of 10", "INVALID BET",
JOptionPane.ERROR_MESSAGE);
jTextFieldBet.requestFocus();
}
public void setSums(){
jPanelDealer.sum.setText( ""+dealer.calculateSum(dealer.dealerCards) );
jPanelPlayer.sum.setText( ""+dealer.calculateSum(dealer.playerCards) );
}
private void exitGame(){
dispose();
System.exit(0);
}
}