Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b3422f0

Browse filesBrowse files
committed
add minicasino
2 parents 163077a + dae09ba commit b3422f0
Copy full SHA for b3422f0

2 files changed

+1,880Lines changed: 1880 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+392Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
1+
package MiniCasinoPackage;
2+
import java.util.ArrayList;
3+
import java.util.Random;
4+
5+
6+
/** This class holds all of the basic blackjack logic
7+
*
8+
*/
9+
10+
public class BlackJack {
11+
12+
private ArrayList<Card> deck;
13+
14+
/**Player*/
15+
16+
protected ArrayList<Card> playerHand;
17+
//Field holds player's score; sum of all card values
18+
private int playerScore;
19+
//Field holds player's ace count; important for game logic
20+
private int playerAceCount;
21+
22+
/**Dealer*/
23+
24+
protected ArrayList<Card> dealerHand;
25+
//Field holds dealer's score; sum of all card values
26+
private int dealerScore;
27+
//Field holds dealer's ace count; important for game logic
28+
private int dealerAceCount;
29+
30+
31+
public BlackJack () {
32+
33+
newHand();
34+
35+
}
36+
37+
//function begins game by creating deck and handling first deals
38+
public void newHand () {
39+
40+
if (MiniCasino.getBlackjackGameCount() > 1) {
41+
42+
playerHand.clear();
43+
dealerHand.clear();
44+
45+
}
46+
47+
playerScore = 0;
48+
dealerScore = 0;
49+
playerAceCount = 0;
50+
dealerAceCount = 0;
51+
52+
//Calling method that builds (or rebuilds) the deck
53+
deck = createDeck();
54+
55+
//Calling method that shuffles deck randomly
56+
deck = shuffleDeck(deck);
57+
58+
//Deals out player's hand
59+
playerHand = dealPlayerHand();
60+
61+
//Deals out dealer's hand
62+
dealerHand = dealDealerHand();
63+
64+
}
65+
66+
67+
//function handles creating the deck
68+
public ArrayList<Card> createDeck () {
69+
70+
ArrayList<Card> deck = new ArrayList<>();
71+
72+
String [] values = {"A", "2", "3", "4","5","6","7","8","9","10","J","Q","K"};
73+
String [] types = {"H","S","C","D"};
74+
75+
for (int i = 0; i < values.length; i++) {
76+
77+
for ( int j = 0; j< types.length; j++ ) {
78+
79+
deck.add( new Card (values[i] ,types[j]) );
80+
81+
}
82+
83+
}
84+
85+
return deck;
86+
87+
}
88+
89+
90+
//Method randomly shuffles deck by swapping cards
91+
92+
public ArrayList<Card> shuffleDeck(ArrayList<Card> deck) {
93+
94+
Random random = new Random();
95+
96+
int n = deck.size();
97+
98+
for (int i = n - 1; i > 0; i--) {
99+
100+
int j = random.nextInt(i + 1);
101+
102+
// Swap deck[i] with deck[j]
103+
Card temp = deck.get(i);
104+
deck.set(i, deck.get(j));
105+
deck.set(j, temp);
106+
107+
}
108+
109+
return deck;
110+
111+
}
112+
113+
114+
//Method returns player's first hand
115+
public ArrayList<Card> dealPlayerHand() {
116+
117+
ArrayList<Card> hand = new ArrayList<>();
118+
119+
Card card = deck.remove( deck.size() - 1) ;
120+
playerScore+= card.getVal();
121+
122+
if ( card.isAce() ) {
123+
124+
playerAceCount++;
125+
}
126+
127+
hand.add( card );
128+
129+
card = deck.remove( deck.size() - 1) ;
130+
playerScore+= card.getVal();
131+
132+
if ( card.isAce() ) {
133+
134+
playerAceCount++;
135+
136+
}
137+
138+
hand.add( card );
139+
140+
return hand;
141+
142+
}
143+
144+
//Method returns dealer's first hand
145+
public ArrayList<Card> dealDealerHand() {
146+
147+
ArrayList<Card> hand = new ArrayList<>();
148+
149+
Card card = deck.remove( deck.size() - 1);
150+
dealerScore+= card.getVal();
151+
152+
if ( card.isAce() ) {
153+
154+
dealerAceCount++;
155+
}
156+
157+
158+
hand.add( card );
159+
160+
card = deck.remove( deck.size() - 1) ;
161+
dealerScore+= card.getVal();
162+
163+
if ( card.isAce() ) {
164+
165+
dealerAceCount++;
166+
}
167+
168+
hand.add( card );
169+
170+
return hand;
171+
172+
}
173+
174+
//Void method adds card from deck to players hand
175+
public void playerHit () {
176+
177+
Card card = deck.remove(deck.size() - 1);
178+
playerScore+= card.getVal();
179+
180+
if ( card.isAce() ) {
181+
182+
playerAceCount++;
183+
184+
}
185+
186+
playerHand.add(card);
187+
188+
}
189+
190+
//Void method adds card from deck to dealers hand
191+
public void dealerHit () {
192+
193+
Card card = deck.remove(deck.size() - 1);
194+
dealerScore+= card.getVal();
195+
196+
if ( card.isAce() ) {
197+
198+
dealerAceCount++;
199+
200+
}
201+
202+
dealerHand.add(card);
203+
204+
}
205+
206+
//method reduces score in accordance to when aces put player's score over 21
207+
public int playerAceAdjustment () {
208+
209+
210+
while (playerScore > 21 && playerAceCount > 0 ) {
211+
212+
playerScore-=10;
213+
playerAceCount--;
214+
215+
}
216+
217+
return playerScore;
218+
219+
}
220+
221+
222+
//method reduces score in accordance to when aces put dealer's score over 21
223+
public int dealerAceAdjustment () {
224+
225+
while (dealerScore > 21 && dealerAceCount > 0 ) {
226+
227+
dealerScore-=10;
228+
dealerAceCount--;
229+
230+
}
231+
232+
return dealerScore;
233+
234+
}
235+
236+
public boolean playerHasBlackJack() {
237+
238+
239+
if ( playerAceAdjustment() == 21 && playerHand.size() == 2 ) {
240+
241+
return true;
242+
243+
}
244+
245+
return false;
246+
247+
}
248+
249+
250+
251+
public boolean dealerHasBlackJack() {
252+
253+
if ( dealerAceAdjustment() == 21 && dealerHand.size() == 2 ) {
254+
255+
return true;
256+
}
257+
258+
return false;
259+
260+
}
261+
262+
263+
264+
//determines winner; 1 is player win, 2 is dealer win, 3 is tie
265+
public int determineWinner () {
266+
267+
/**The three outcomes of either the player or dealer having blackjack */
268+
269+
if (!playerHasBlackJack() && dealerHasBlackJack() ) {
270+
271+
return 2;
272+
273+
}
274+
275+
276+
else if ( playerHasBlackJack() && !dealerHasBlackJack() ) {
277+
278+
return 1;
279+
280+
}
281+
282+
else if ( playerHasBlackJack() && dealerHasBlackJack() ) {
283+
284+
return 3;
285+
286+
}
287+
/***********************************************************************/
288+
289+
290+
//Other conditions
291+
292+
if (playerAceAdjustment() <=21 && playerAceAdjustment() > dealerAceAdjustment() ) {
293+
294+
return 1;
295+
}
296+
297+
else if (playerAceAdjustment() <=21 && dealerAceAdjustment() > 21) {
298+
299+
return 1;
300+
}
301+
302+
else if (dealerAceAdjustment() <= 21 && dealerAceAdjustment() > playerAceAdjustment() ) {
303+
304+
return 2;
305+
}
306+
307+
else if (dealerAceAdjustment() <= 21 && playerAceAdjustment() > 21 ) {
308+
309+
return 2;
310+
}
311+
312+
else if (dealerAceAdjustment() == playerAceAdjustment() && playerAceAdjustment()<=21) {
313+
314+
return 3;
315+
}
316+
317+
else if (dealerAceAdjustment() > 21 && playerAceAdjustment() > 21 ) {
318+
319+
return 3;
320+
}
321+
322+
return 3;
323+
324+
}
325+
326+
327+
//Card class, which holds card type and value
328+
class Card {
329+
330+
331+
private String value;
332+
private String type;
333+
334+
335+
//Card constructor to initialize card with value and type
336+
public Card (String value, String type ) {
337+
338+
this.value = value;
339+
this.type = type;
340+
341+
}
342+
343+
//method returns image path for drawing in GUI
344+
public String imagePath () {
345+
346+
String str = "file:lib/cards/" + this.value + "-" + this.type + ".png";
347+
return str;
348+
349+
}
350+
351+
//Method determines whether the calling card is an ace or not
352+
public boolean isAce (){
353+
354+
if (this.value.compareTo("A") == 0 ) {
355+
356+
return true;
357+
358+
}
359+
360+
return false;
361+
362+
}
363+
364+
365+
366+
public int getVal () {
367+
368+
if (this.value.equals("A") ) {
369+
370+
return 11;
371+
372+
}
373+
374+
if (this.value.equals("J") || this.value.equals("Q") || this.value.equals("K") ) {
375+
376+
return 10;
377+
378+
}
379+
380+
381+
else {
382+
383+
return Integer.parseInt( this.value );
384+
385+
386+
}
387+
388+
}
389+
390+
}
391+
392+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.