-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCards.java
187 lines (164 loc) · 4.89 KB
/
Cards.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
/** ****************************************************
* NAME: KEVAL VARIA
* CLASS: CS4B
* PROGRAM: BLACKJACK PART 1
* FILE: CARDS.JAVA
***************************************************** */
package blackjack;
import java.util.concurrent.ThreadLocalRandom;
import javafx.scene.image.Image;
/**
*
* @author keval
*/
public class Cards {
// class variable declaration
private int rank1, rank2;
private int suit1, suit2;
// default constructor
Cards() {
this.rank1 = this.rank2 = 0;
this.suit1 = this.suit2 = 0;
}
/**
* Function: getRandomRank()
* - generate a random number between 1 and 13 to express a random rank from the deck between Ace, 2, 3.., Queen, King
**/
private int getRandomRank() {
return (ThreadLocalRandom.current().nextInt(1, 13 + 1));
}
/**
* Function: getRandomSuit()
* - generate a random number between 1 and 4 to express a random suit from the deck: Diamonds, Clubs, Hearts, Spades
**/
private int getRandomSuit() {
return (ThreadLocalRandom.current().nextInt(1, 4 + 1));
}
/**
* Function: prepareHands()
* - Distribute cards to the player (avoid using the same random cards as there can only be one of each set)
**/
public void perpareHands() {
this.rank1 = getRandomRank();
this.suit1 = getRandomSuit();
this.rank2 = this.rank1;
this.suit2 = this.suit1;
while(this.rank1 == this.rank2 && this.suit1 == this.suit2) {
this.rank2 = getRandomRank();
this.suit2 = getRandomSuit();
}
}
/**
* Function: getRank1()
* - Obtain the rank of the first card in hand
**/
public String getRank1() {
if(this.rank1 == 1) {
return "ACE";
}
else if(this.rank1 >= 2 && this.rank1 <= 10) {
return (this.rank1 + "");
}
else if(this.rank1 == 11) {
return "JACK";
}
else if(this.rank1 == 12) {
return "QUEEN";
}
else {
return "KING";
}
}
/**
* Function: getRank2()
* - Obtain the rank of the second card in hand
**/
public String getRank2() {
if(this.rank2 == 1) {
return "ACE";
}
else if(this.rank2 >= 2 && this.rank2 <= 10) {
return (this.rank2 + "");
}
else if(this.rank2 == 11) {
return "JACK";
}
else if(this.rank2 == 12) {
return "QUEEN";
}
else {
return "KING";
}
}
/**
* Function: getSuit1()
* - Obtain the suit of the first card in hand
**/
public String getSuit1() {
switch(this.suit1) {
case 1:
return "HEARTS";
case 2:
return "DIAMONDS";
case 3:
return "CLUBS";
default:
return "SPADES";
}
}
/**
* Function: getSuit2()
* - Obtain the suit of the second card in hand
**/
public String getSuit2() {
switch(this.suit2) {
case 1:
return "HEARTS";
case 2:
return "DIAMONDS";
case 3:
return "CLUBS";
default:
return "SPADES";
}
}
/**
* Function: getResult()
* - Obtain the sum of the two cards
**/
public String getResult() {
if(this.rank1 == 1 && this.rank2 == 11) {
return "21 = Black Jack";
}
else if(this.rank1 == 11 && this.rank2 == 1) {
return "21 = Black Jack";
}
else {
return ("" + (this.rank1 + this.rank2));
}
}
/**
* Function: getImageCard1()
* - Obtain the image of the first card in hand to display to the UI
**/
public Image getImageCard1() {
return new Image("File:\\\\\\\\C:\\Users\\keval\\Documents\\NetBeansProjects\\Blackjack\\src\\cards\\png\\" + getRank1() + "_of_" + getSuit1().toLowerCase() + ".png");
}
/**
* Function: getImageCard2()
* - Obtain the image of the second card in hand to display to the UI
**/
public Image getImageCard2() {
return new Image("File:\\\\\\\\C:\\Users\\keval\\Documents\\NetBeansProjects\\Blackjack\\src\\cards\\png\\" + getRank2() + "_of_" + getSuit2().toLowerCase() + ".png");
}
/**
* Override toString Function
* @return String
*/
@Override
public String toString() {
return ("\n\nCARD #1: " + getRank1() + " of " + getSuit1() + "\n"
+ "CARD #2: " + getRank2() + " of " + getSuit2() + "\n"
+ "SUM: " + getResult() + "\n\n");
}
}