Ahmad Rusdi 6 lat temu
rodzic
commit
d95ced65ea

+ 117
- 50
src/main/java/io/zipcoder/casino/BlackJack.java Wyświetl plik

@@ -1,71 +1,138 @@
1 1
 package io.zipcoder.casino;
2
-
3
-public class BlackJack extends CardGame {
4
-    int playerHand;
5
-    int dealerHand;
6
-    int currentBet;
7
-    boolean playingBJ = false;
8
-    CardDeck deck = new CardDeck();
9
-    Player player;
2
+import java.util.ArrayList;
3
+
4
+public class BlackJack {
5
+    private ArrayList<Card> playerHand = new ArrayList<Card>();
6
+    private ArrayList<Card> dealerHand = new ArrayList<Card>();
7
+    private int currentBet;
8
+    private boolean playingBJ = false;
9
+    private CardDeck deck = new CardDeck();
10
+    private Player player;
11
+
12
+    public BlackJack() {
13
+        Player player = new Player("Eugene");
14
+    }
10 15
 
11 16
     public void start() {
12 17
         playingBJ = true;
13 18
 
14 19
     }
15 20
 
16
-    public BlackJack(Player player) {
17
-
21
+    private void dealToPlayerHand(int amountOfCards) {
22
+        for (int i = 0; i < amountOfCards; i++) {
23
+            this.playerHand.add(deck.draw());
24
+        }
18 25
     }
19 26
 
27
+    private void dealToDealerHand(int amountOfCards) {
28
+        for (int i = 0; i < amountOfCards; i++) {
29
+            this.dealerHand.add(deck.draw());
30
+        }
31
+    }
20 32
 
33
+    private void setCurrentBet(int bet) {
34
+        this.currentBet = bet;
35
+    }
21 36
 
22
-while(playingBJ == true){
23
-        Console.output("Welcome to BlackJack " + player.name + ". I am your dealer Vince and I am here to take your money!");
24
-        currentBet = Console.numberFromString(Console.askForInput("How much would you like to bet?"));
25
-        deck.deal();
26
-        if (dealerHand == 21) {
27
-            Console.output("Dealer has BlackJack! You lose!");
28
-            loseBet(currentBet);
29
-            currentBet = 0;
30
-            deck.deal();
31
-        } else if (playerHand == 21) {
32
-            Console.output("You have BlackJack! You win!");
33
-            winBet((currentBet * 3) / 2);
34
-            currentBet = 0;
35
-            deck.deal();
37
+    private int getCardValue(Card card) {
38
+        Rank rank = card.getRank();
39
+        int calcValue = 0;
40
+        switch(rank) {
41
+            case ACE:
42
+                calcValue = 1;
43
+                break;
44
+            case TWO:
45
+                calcValue = 2;
46
+                break;
47
+            case THREE:
48
+                calcValue = 3;
49
+                break;
50
+            case FOUR:
51
+                calcValue = 4;
52
+                break;
53
+            case FIVE:
54
+                calcValue = 5;
55
+                break;
56
+            case SIX:
57
+                calcValue = 6;
58
+                break;
59
+            case SEVEN:
60
+                calcValue = 7;
61
+                break;
62
+            case EIGHT:
63
+                calcValue = 8;
64
+                break;
65
+            case NINE:
66
+                calcValue = 9;
67
+                break;
68
+            case TEN:
69
+                calcValue = 10;
70
+                break;
71
+            case JACK:
72
+            case QUEEN:
73
+            case KING:
74
+                calcValue = 10;
75
+                break;
76
+        }
77
+        return calcValue;
78
+    }
36 79
 
80
+    private int calcHand(ArrayList<Card> hand) {
81
+        int totalScore = 0;
82
+        for (int i = 0; i < hand.size(); i++) {
83
+            totalScore = getCardValue(hand.get(i));
37 84
         }
38
-        while (playerHand < 21) {
39
-            String action = Console.askForInput("Dealer is showing: " + "You have " + playerHand + ". Please enter 'hit' or 'stay'.");
40
-            if (action.equals("hit")) {
41
-                drawCard();
42
-                cardValue += playerHand;
43
-                if (playerHand > 21) {
44
-                    Console.output("You have " + playerHand + " and bust, you suck!");
45
-                    loseBet(currentBet);
46
-                    currentBet = 0;
85
+        return totalScore;
86
+    }
87
+
88
+    public void startGame() {
89
+        while(playingBJ)
90
+        {
91
+            Console.output("Welcome to BlackJack " + player.getName() + ". I am your dealer Vince and I am here to take your money!");
92
+            int bet = Console.numberFromString(Console.askForInput("How much would you like to bet?"));
93
+            setCurrentBet(bet);
94
+            dealToPlayerHand(2);
95
+            dealToDealerHand(2);
96
+            if (calcHand(dealerHand) == 21) {
97
+                Console.output("Dealer has BlackJack! You lose!");
98
+                player.subtractFromBankroll(currentBet);
99
+                currentBet = 0;
100
+                deck.draw();
101
+            } else if (calcHand(playerHand) == 21) {
102
+                Console.output("You have BlackJack! You win!");
103
+                winBet((currentBet * 3) / 2);
104
+                currentBet = 0;
105
+                deck.deal();
106
+
107
+            }
108
+            while (calcHand(playerHand) < 21) {
109
+                String action = Console.askForInput("Dealer is showing: " + "You have " + playerHand + ". Please enter 'hit' or 'stay'.");
110
+                if (action.equals("hit")) {
111
+                    drawCard();
112
+                    cardValue += playerHand;
113
+                    if (calcHand(playerHand) > 21) {
114
+                        Console.output("You have " + playerHand + " and bust, you suck!");
115
+                        loseBet(currentBet);
116
+                        currentBet = 0;
117
+                    }
118
+                } else if (action.equals("stay")) {
119
+                    break;
47 120
                 }
48
-            } else if (action.equals("stay")) {
49
-                break;
50 121
             }
51
-        }
52
-        while (dealerHand < 17 && dealerHand < 21) {
53
-            Console.output("Dealer has " + dealerHand + " and hits.");
54
-            drawcard();
55
-            cardValue += dealerHand;
56
-            if (dealerHand > 21) {
57
-                Console.output("Dealer has " + dealerHand + " and busts, you win!");
58
-                winBet(currentBet);
59
-                currentBet = 0;
60
-            } else if (dealerHand >= 17 && dealerHand < 21) {
61
-                Console.output("Dealer has " + dealerHand + " and stands.");
122
+            while (calcHand(dealerHand) < 17 && calcHand(dealerHand) < 21) {
123
+                Console.output("Dealer has " + dealerHand + " and hits.");
124
+                drawcard();
125
+                cardValue += dealerHand;
126
+                if (calcHand(dealerHand > 21) {
127
+                    Console.output("Dealer has " + dealerHand + " and busts, you win!");
128
+                    winBet(currentBet);
129
+                    currentBet = 0;
130
+                } else if (dealerHand >= 17 && dealerHand < 21) {
131
+                    Console.output("Dealer has " + dealerHand + " and stands.");
132
+                }
62 133
             }
63 134
         }
64
-
65
-
66
-
67 135
     }
68
-
69 136
 }
70 137
 
71 138
 

+ 11
- 0
src/main/java/io/zipcoder/casino/GameFactory.java Wyświetl plik

@@ -0,0 +1,11 @@
1
+package io.zipcoder.casino;
2
+
3
+//public class GameFactory {
4
+//    public static Game getGame( gameName, Player player) {
5
+//        gameName = Consoler.numberFromString(gameName);
6
+//        switch(gameName) {
7
+//            case 1:
8
+//
9
+//        }
10
+//    }
11
+//}