-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
259 lines (238 loc) · 9.12 KB
/
Game.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
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class Game {
private class Cards{
String value;
String type;
Cards(String value, String type){
this.value = value;
this.type = type;
}
public String toString(){
return value+"-"+type;
}
public int getValue(){
if ("AJQK".contains(value)){
if(value == "A") {
return 11;
}
return 10;
}
return Integer.parseInt(value);
}
public boolean isAce(){
return value =="A";
}
public String getImagePath() {
return "/cards/" + toString() + ".png";
}
}
ArrayList<Cards> deck;
Random r = new Random();
Cards hiddenCard;
ArrayList<Cards> dealerHand;
int dealerSum;
int dealerAceCount;
ArrayList<Cards> playerHand;
int playerSum;
int playerAceCount;
int boardwidth = 500;
int boardHeight = 550;
int cardWidth = 100;
int cardHeight = 150;
String username;
JFrame frame = new JFrame("Black Jack");
JPanel gamepanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
Image hiddenCardImg = new ImageIcon(getClass().getResource("./cards/BACK.png")).getImage();
if (!staybutton.isEnabled()) {
hiddenCardImg = new ImageIcon(getClass().getResource(hiddenCard.getImagePath())).getImage();
}
g.drawImage(hiddenCardImg, 20, 20, cardWidth, cardHeight, null);
for(int i=0; i< dealerHand.size(); i++){
Cards card = dealerHand.get(i);
Image cardImg = new ImageIcon(getClass().getResource(card.getImagePath())).getImage();
g.drawImage(cardImg, cardWidth+25+(cardWidth+5)*i, 20, cardWidth, cardHeight, null);
}
for(int i=0; i< playerHand.size(); i++){
Cards card = playerHand.get(i);
Image cardImg = new ImageIcon(getClass().getResource(card.getImagePath())).getImage();
g.drawImage(cardImg, 20+ (cardWidth+5)*i, 320, cardWidth, cardHeight, null);
}
if(!staybutton.isEnabled()){
dealerSum = reduceDealerAce();
playerSum = reducePlayerAce();
System.out.println("STAY: ");
System.out.println(dealerSum);
System.out.println(playerSum);
String message = "";
if(playerSum > 21){
message = "You Lose!";
}else if(dealerSum >21){
message = "You Win!";
}else if(playerSum == dealerSum){
message = "Tie!";
}else if(playerSum>dealerSum){
message = "You Win!";
}else if(playerSum < dealerSum){
message = "You Lose!";
}
g.setFont(new Font("Times New Roman", Font.PLAIN, 30));
g.setColor(Color.white);
g.drawString(message, 220, 250);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
JPanel buttonPanel = new JPanel();
JButton staybutton = new JButton("Stay");
JButton hitButton = new JButton("Hit");
JLabel usernameLabel = new JLabel();
Game(){
getUsername();
startGame();
setupUI();
frame.setVisible(true);
frame.setSize(boardwidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamepanel.setLayout(new BorderLayout());
gamepanel.setBackground(new Color(53, 101, 77));
frame.add(gamepanel);
hitButton.setFocusable(true);
buttonPanel.add(hitButton);
staybutton.setFocusable(true);
buttonPanel.add(staybutton);
frame.add(buttonPanel, BorderLayout.SOUTH);
hitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Cards card = deck.remove(deck.size()-1);
playerSum +=card.getValue();
playerAceCount += card.isAce() ? 1 : 0;
playerHand.add(card);
if(reducePlayerAce()>21){
hitButton.setEnabled(false);
}
gamepanel.repaint();
}
});
staybutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hitButton.setEnabled(false);
staybutton.setEnabled(false);
while(dealerSum<17){
Cards card = deck.remove(deck.size()-1);
dealerSum += card.getValue();
dealerAceCount += card.isAce() ? 1 : 0;
dealerHand.add(card);
}
gamepanel.repaint();
}
});
gamepanel.repaint();
}
private void setupUI(){
frame.setLayout(new BorderLayout());
frame.add(gamepanel, BorderLayout.CENTER);
buttonPanel.add(hitButton);
buttonPanel.add(staybutton);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.add(usernameLabel, BorderLayout.SOUTH);
usernameLabel.setText("Player: "+username);
usernameLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
frame.setVisible(true);
frame.setSize(boardwidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void getUsername(){
username = JOptionPane.showInputDialog(frame, "Enter a username below: ");
if(username == null || username.trim().isEmpty()){
username = "User";
}
}
public void startGame() {
JOptionPane.showMessageDialog(frame, "Welcome To The Game "+username+"!\n Please type ENTER to star the game!\n Good Luck!");
buildDeck();
shuffleDeck();
dealerHand = new ArrayList<Cards>();
dealerSum = 0;
dealerAceCount = 0;
hiddenCard = deck.remove(deck.size() - 1);
dealerSum += hiddenCard.getValue();
dealerAceCount += hiddenCard.isAce() ? 1 : 0;
Cards card = deck.remove(deck.size() - 1);
dealerSum += card.getValue();
dealerAceCount += card.isAce() ? 1 : 0;
dealerHand.add(card);
System.out.println("DEALER: ");
System.out.println(hiddenCard);
System.out.println(dealerHand);
System.out.println(dealerSum);
System.out.println(dealerAceCount);
playerHand = new ArrayList<Cards>();
playerSum = 0;
playerAceCount = 0;
for (int i = 0; i < 2; i++) {
card = deck.remove(deck.size() - 1);
playerSum += card.getValue();
playerAceCount += card.isAce() ? 1 : 0;
playerHand.add(card);
}
System.out.println("PLAYER: ");
System.out.println(playerHand);
System.out.println(playerSum);
System.out.println(playerAceCount);
}
public void buildDeck() {
deck = new ArrayList<Cards>();
String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] types = {"C", "D", "H", "S"};
for (int i = 0; i < types.length; i++) {
for (int j = 0; j < values.length; j++) {
Cards card = new Cards(values[j], types[i]);
deck.add(card);
}
}
System.out.println("BUILD DECK: ");
System.out.println(deck);
}
public void shuffleDeck(){
for(int i=0; i< deck.size(); i++){
int j=r.nextInt(deck.size());
Cards currentCard = deck.get(i);
Cards randomCard = deck.get(j);
deck.set(i, randomCard);
deck.set(j, currentCard);
}
System.out.println("AFTER SHUFFLE: ");
System.out.println(deck);
}
public int reducePlayerAce(){
while(playerSum>21 && playerAceCount>0){
playerSum -= 10;
playerAceCount -= 1;
}
return playerSum;
}
public int reduceDealerAce(){
while(dealerSum >21 && dealerAceCount>0){
dealerSum -= 10;
dealerAceCount -= 1;
}
return dealerSum;
}
}