-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckOfCards.java
executable file
·214 lines (191 loc) · 6.25 KB
/
DeckOfCards.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
/**
* This is a game about decking of cards.
* According to the rules,try to get a higher score by putting the cards in different place.
* From Java How to Program Deitel and Deitel, and modified by Zhijiao LIU.
* @author Deitel and Deitel
* @author Vindya Wijeratne
* @author Zhijiao LIU
* @version Last modified 17/05/2011
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Open the game interface, and set all buttons and labels.
* @author Deitel and Deitel
* @author Vindya Wijeratne
* @author Zhijiao LIU
* @version Last modified 17/05/2011
*/
public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard,number,noOfSelected=0;
private JButton shuffleButton, skipButton, nButton[];
private JLabel status;
private Container c;
private int hand1,hand2,hand3;
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six"};
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
void setNumber(int number) {this.number = number;}
int getNumber() {return number;}
// Define Parameters
/**
* Open the game interface, and set all buttons and labels.
*/
public DeckOfCards() {
super("Card Grid game");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit tk = this.getToolkit();
int w = 560;
int h = 480;
Dimension dm = tk.getScreenSize();
this.setLocation((int)(dm.getWidth()-w)/2,(int)(dm.getHeight()-h)/2);
deck = new Card[24];
currentCard = -1;
setNumber(1);
for (int i = 0; i < deck.length; i++)
deck[i] = new Card(faces[i % 6], suits[i / 6]);
c = getContentPane();
JPanel p = new JPanel(new FlowLayout());
c.add(p,BorderLayout.NORTH);
JPanel q = new JPanel(new GridLayout(3,3));
c.add(q,BorderLayout.CENTER);
shuffleButton = new JButton("Shuffle cards");
shuffleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent shuffle) {
status.setText("SHUFFLING ...");
shuffle();
status.setText("DECK IS SHUFFLED");
for(int n=0;n<9;n++){
String j = String.valueOf(n);
nButton[n].setEnabled(true);
nButton[n].setText(j);
}
dealCard();
shuffleButton.setEnabled(false);
skipButton.setEnabled(true);
}
});
p.add(shuffleButton);// set the shuffle button
skipButton = new JButton("Skip (once)");
skipButton.setEnabled(false);
skipButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent skip) {
dealCard();
skipButton.setEnabled(false);
}
});
p.add(skipButton);// set the skip button
status = new JLabel("SHUFFLE CARDS TO BEGIN");
p.add(status);
nButton = new JButton[9];// set the 9 game buttons
int m=0;
do{
nButton[m] = new JButton();
nButton[m].setEnabled(false);
nButton[m].addActionListener(new GridListener());
q.add(nButton[m]);
m++;
}while(m<9);
setSize(w,h); // set the window size
setVisible(true); // show the window
}
/**
* Define what will be done after click the current button.
*/
public class GridListener implements ActionListener{
public void actionPerformed(ActionEvent grid) {
JButton m = (JButton)grid.getSource();
m.setText(deck[currentCard].toString());
m.setEnabled(false);
dealCard();
noOfSelected++;
if(skipButton.isSelected()){
noOfSelected-=1;
}
if(noOfSelected==9){
WindowOfScore(); // call the window of final score
}
}
}
/**
* Swap the cards.
*/
public void shuffle() {
currentCard = -1;
setNumber(1);
for (int i = 0; i < deck.length; i++) {
int j = (int) (Math.random() * 24);
Card temp = deck[i]; // swap
deck[i] = deck[j]; // the
deck[j] = temp; // cards
}
}
public Card dealCard() {
/**
* Return the information of current card.
* @return deck[currentCard] Face and suit of current card.
*/
if (currentCard++ < deck.length){
status.setText(deck[currentCard].toString());
return deck[currentCard];}
else {
return null;
}
}
/**
* Calculate the final score.
*/
public void calculateScore(){
hand1=0;
hand2=0;
hand3=0;
int position[][] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8}};
for(int compareP[]:position){
for(String compareS:suits){// find same suit in column or row
if(nButton[compareP[0]].getText().indexOf(compareS)>=0
&& nButton[compareP[1]].getText().indexOf(compareS)>=0
&& nButton[compareP[2]].getText().indexOf(compareS)>=0){
hand2+=1;
for(int compareAce:compareP){// find Ace to classify the hands
if(nButton[compareAce].getText().indexOf(faces[0])>=0){
hand1+=1;
hand2-=1;
break;
}
}
break;
}
}
for(String compareF:faces){// find two same faces at least
if((nButton[compareP[0]].getText().indexOf(compareF)>=0 && nButton[compareP[1]].getText().indexOf(compareF)>=0 && nButton[compareP[2]].getText().indexOf(compareF)<0)||
(nButton[compareP[0]].getText().indexOf(compareF)>=0 && nButton[compareP[2]].getText().indexOf(compareF)>=0 && nButton[compareP[1]].getText().indexOf(compareF)<0)||
(nButton[compareP[1]].getText().indexOf(compareF)>=0 && nButton[compareP[2]].getText().indexOf(compareF)>=0 && nButton[compareP[0]].getText().indexOf(compareF)<0)){
hand3+=1;
}
}
}
}
/**
* Open the score interface, and set all buttons and labels.
*/
public void WindowOfScore() {
calculateScore();
JOptionPane.showMessageDialog(null,"Total score: "+(hand1*1000+hand2*500+hand3*100)+" points \n " +
"Category 1: Ace Flush (1000 points): "+hand1+" hands \n " +
"Cateogry 2: Flush (500 points): "+hand2+" hands \n " +
"Category 3: Pair (100 points): "+hand3+" hands ");
noOfSelected=0;
skipButton.setEnabled(false);
status.setText("SHUFFLE CARDS TO BEGIN");
shuffleButton.setEnabled(true);
}
/**
* Main now only creates a new instance of my
* program and calls the program's method.
* @param args This program does not use this parameter.
*/
public static void main(String args[]) {
DeckOfCards app = new DeckOfCards();
}
}