-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameWork.java
More file actions
271 lines (215 loc) · 7.51 KB
/
FrameWork.java
File metadata and controls
271 lines (215 loc) · 7.51 KB
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
package main;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.ImageIcon;
/**
* Frame work of the game.
* This class stores the graphical user interface components of the game that make it user friendly.
* It also displays the initial setting of the game when the user first runs the program.
*
* @author Janvi Sharma
*
*/
public class FrameWork {
static JFrame frame;
static JPanel remainingAmtPanel, dealerCardsPanel, userCardsPanel,jOptionPanel;
static JTextField betAmount;
static int betAmtInInt, shuffleCtr=0, replaceCtr=0, gameCtr=2;
static int betAmtUser = 100;
static JLabel displayUC1,displayUC2,displayUC3,remainingAmt,displayDC1,displayDC2,displayDC3;
static Cards[] userC, totalCards,dealerC;
static JButton repCard1, repCard2, repCard3,startButton,resultButton;
public static void main(String args[]) {
frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Control");
JMenuItem menuItem = new JMenuItem("Exit");
JMenu menu2 = new JMenu("Help");
JMenuItem instruction = new JMenuItem("Instruction");
menu1.add(menuItem);
menu2.add(instruction);
menuBar.add(menu1);
menuBar.add(menu2);
menuItem.addActionListener(new ControlActionListener());
instruction.addActionListener(new HelpActionListener());
frame.setJMenuBar(menuBar);
JPanel mainPanel = new JPanel();
JPanel cardsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 500, 500));
cardsPanel.setBackground(Color.green);
mainPanel.add(cardsPanel);
dealerCardsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,20)); // first panel for cardsPanel
dealerCardsPanel.setBackground(Color.green);
userCardsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,20));
userCardsPanel.setBackground(Color.green); // second panel for cardsPanel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,50));
buttonPanel.setBackground(Color.green); // third panel for cardsPanel
cardsPanel.add(dealerCardsPanel);
cardsPanel.add(userCardsPanel);
cardsPanel.add(buttonPanel);
repCard1 = new JButton("Replace Card 1");
repCard2 = new JButton("Replace Card 2");
repCard3 = new JButton("Replace Card 3");
repCard1.setBackground(Color.green);
repCard2.setBackground(Color.green);
repCard3.setBackground(Color.green);
repCard1.addActionListener(new repCard1ActionListener());
repCard2.addActionListener(new repCard2ActionListener());
repCard3.addActionListener(new repCard3ActionListener());
buttonPanel.add(repCard1);
buttonPanel.add(repCard2);
buttonPanel.add(repCard3);
JPanel bottomPanel = new JPanel();
JPanel betPanel = new JPanel();
JLabel betLabel = new JLabel("Bet $:");
betPanel.add(betLabel,BorderLayout.NORTH);
betAmount = new JTextField(10);
betPanel.add(betAmount);
startButton = new JButton("Start");
resultButton = new JButton("Result");
betPanel.add(startButton);
betPanel.add(resultButton);
bottomPanel.add(betPanel);
remainingAmtPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,0,80));
remainingAmt = new JLabel("");
remainingAmtPanel.add(remainingAmt);
bottomPanel.add(remainingAmtPanel);
mainPanel.add(bottomPanel);
repCard1.setEnabled(false);
repCard2.setEnabled(false);
repCard3.setEnabled(false);
resultButton.setEnabled(false);
remainingAmt.setText("Please place your bet! Amount of money you have: $"+betAmtUser);
totalCards = new Cards[52];
int i,j;
int start = 0;
int ctr;
for(i=1;i<5;i++) {
ctr = 1;
for(j=start;j<start+13;j++) {
totalCards[j] = new Cards();
totalCards[j].assignCardValue(ctr);
String cardName = "";
cardName = cardName + i;
cardName = cardName + ctr;
totalCards[j].assignImageName(cardName);
ctr++;
}
start = start + 13;
}
ShuffleDeck(totalCards);
shuffleCtr = 1;
dealerC = new Cards[3];
userC = new Cards[3];
for(i=0;i<3;i++) {
dealerC[i] = totalCards[i];
}
int k = 0;
for(i=3;i<6;i++) {
userC[k] = totalCards[i];
k++;
}
ImageIcon displayDCard1 = new ImageIcon("card_back-0000.jpg"); // Display 3 back images of dealer's cards
displayDC1 = new JLabel(displayDCard1);
dealerCardsPanel.add(displayDC1);
ImageIcon displayDCard2 = new ImageIcon("card_back-0000.jpg");
displayDC2 = new JLabel(displayDCard2);
dealerCardsPanel.add(displayDC2);
ImageIcon displayDCard3 = new ImageIcon("card_back-0000.jpg");
displayDC3 = new JLabel(displayDCard3);
dealerCardsPanel.add(displayDC3);
ImageIcon displayUCard1 = new ImageIcon("card_back-0000.jpg");
displayUC1 = new JLabel(displayUCard1);
userCardsPanel.add(displayUC1);
ImageIcon displayUCard2 = new ImageIcon("card_back-0000.jpg");
displayUC2 = new JLabel(displayUCard2);
userCardsPanel.add(displayUC2);
ImageIcon displayUCard3 = new ImageIcon("card_back-0000.jpg");
displayUC3 = new JLabel(displayUCard3);
userCardsPanel.add(displayUC3);
startButton.addActionListener(new startButtonListener());
resultButton.addActionListener(new resultButtonActionListener());
cardsPanel.setLayout(new BoxLayout(cardsPanel, BoxLayout.Y_AXIS));
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
frame.setTitle("A Simple Card Game");
frame.add(mainPanel,BorderLayout.NORTH);
frame.setSize(500,700);
frame.setVisible(true);
}
/**
* Method to start the game's next round.
* This method stores the initial set up of the game
* that will allow the user to keep playing until the bet amount becomes zero.
*
*/
public static void startGame() {
replaceCtr = 0;
shuffleCtr++;
startButton.setEnabled(true);
repCard1.setEnabled(false);
repCard2.setEnabled(false);
repCard3.setEnabled(false);
resultButton.setEnabled(false);
int i;
shuffleCtr =0 ;
Cards temp = new Cards();
ShuffleDeck(totalCards);
shuffleCtr=1;
for(i=0;i<3;i++) {
dealerC[i] = totalCards[i];
}
int k = 0;
for(i=3;i<6;i++) {
userC[k] = totalCards[i];
k++;
}
ImageIcon displayDCard1 = new ImageIcon("card_back-0000.jpg"); // Display 3 back images of dealer's cards
displayDC1.setIcon(displayDCard1);
ImageIcon displayDCard2 = new ImageIcon("card_back-0000.jpg");
displayDC2.setIcon(displayDCard2);
ImageIcon displayDCard3 = new ImageIcon("card_back-0000.jpg");
displayDC3.setIcon(displayDCard3);
ImageIcon displayUCard1 = new ImageIcon("card_back-0000.jpg");
displayUC1.setIcon(displayUCard1);
ImageIcon displayUCard2 = new ImageIcon("card_back-0000.jpg");
displayUC2.setIcon(displayUCard2);
ImageIcon displayUCard3 = new ImageIcon("card_back-0000.jpg");
displayUC3.setIcon(displayUCard3);
frame.repaint();
frame.setSize(500,700);
frame.setVisible(true);
}
/**
* Method to Shuffle Deck of Cards.
* This method takes in a parameter of type Cards array and then shuffles the cards in it.
* The shuffle counter makes sure that the cards that have been given to the user and the dealer are not
* shuffled with the remaining cards in the deck.
*
* @param totalCards Array that stores all 52 cards.
*/
public static void ShuffleDeck(Cards totalCards[]) {
int i;
Cards temp;
Random rand = new Random();
if(shuffleCtr==0) {
for(i=0;i<52;i++) {
int r = i + rand.nextInt(52 - i);
temp = totalCards[i];
totalCards[i] = totalCards[r];
totalCards[r] = temp;
}
}
else
for(i=6;i<52;i++) {
int r = i + rand.nextInt(52 - i);
temp = totalCards[i];
totalCards[i] = totalCards[r];
totalCards[r] = temp;
}
}
}