Ahmad Rusdi пре 6 година
родитељ
комит
526de34c1d

+ 127
- 43
src/main/java/io/zipcoder/casino/BlackJack.java Прегледај датотеку

@@ -1,21 +1,24 @@
1 1
 package io.zipcoder.casino;
2 2
 import java.util.ArrayList;
3
+import java.util.Arrays;
3 4
 
4 5
 public class BlackJack {
5 6
     private ArrayList<Card> playerHand = new ArrayList<Card>();
6 7
     private ArrayList<Card> dealerHand = new ArrayList<Card>();
7 8
     private int currentBet;
8 9
     private boolean playingBJ = false;
10
+    private boolean playingHands = true;
11
+    private boolean askToPlayAgain = false;
9 12
     private CardDeck deck = new CardDeck();
10 13
     private Player player;
11 14
 
12 15
     public BlackJack() {
13
-        Player player = new Player("Eugene");
16
+        this.player = new Player("Eugene");
14 17
     }
15 18
 
16 19
     public void start() {
17 20
         playingBJ = true;
18
-
21
+        this.deck.shuffle();
19 22
     }
20 23
 
21 24
     private void dealToPlayerHand(int amountOfCards) {
@@ -30,10 +33,55 @@ public class BlackJack {
30 33
         }
31 34
     }
32 35
 
36
+    private void setPlayingHands(boolean bool) {
37
+        this.playingHands = bool;
38
+    }
39
+
40
+    private String getPlayerHand() {
41
+        return Arrays.toString(this.playerHand.toArray());
42
+    }
43
+
44
+    private String getDealerHand() {
45
+        return Arrays.toString(this.dealerHand.toArray());
46
+    }
47
+
48
+    private String showDealerHand() {
49
+        Card[] filteredArray = this.dealerHand.toArray(new Card[this.dealerHand.size()]);
50
+        filteredArray[0] = new Card();
51
+        return Arrays.toString(filteredArray);
52
+    }
53
+
54
+
55
+    private int getCurrentBet() {
56
+        return this.currentBet;
57
+    }
58
+
33 59
     private void setCurrentBet(int bet) {
34 60
         this.currentBet = bet;
35 61
     }
36 62
 
63
+    private void setAskToPlayAgain(boolean bool) {
64
+        this.askToPlayAgain = bool;
65
+    }
66
+
67
+    private void resetHands() {
68
+        this.playerHand = new ArrayList<Card>();
69
+        this.dealerHand = new ArrayList<Card>();
70
+    }
71
+    private void startHands() {
72
+        this.deck = new CardDeck();
73
+        this.deck.shuffle();
74
+        resetHands();
75
+        dealToPlayerHand(2);
76
+        dealToDealerHand(2);
77
+    }
78
+
79
+    private void startNextHand() {
80
+        setCurrentBet(0);
81
+        setAskToPlayAgain(true);
82
+        setPlayingHands(false);
83
+    }
84
+
37 85
     private int getCardValue(Card card) {
38 86
         Rank rank = card.getRank();
39 87
         int calcValue = 0;
@@ -80,59 +128,95 @@ public class BlackJack {
80 128
     private int calcHand(ArrayList<Card> hand) {
81 129
         int totalScore = 0;
82 130
         for (int i = 0; i < hand.size(); i++) {
83
-            totalScore = getCardValue(hand.get(i));
131
+            totalScore += getCardValue(hand.get(i));
84 132
         }
85 133
         return totalScore;
86 134
     }
87 135
 
88 136
     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
-
137
+        start();
138
+        Console.output("Starting BlackJack...");
139
+        while (playingBJ) {
140
+            while (askToPlayAgain) {
141
+                String answer = Console.askForInput("Would you like to play again? (Y/N)");
142
+                if (answer.equals("y")) {
143
+                    setPlayingHands(true);
144
+                    setAskToPlayAgain(false);
145
+                } else {
146
+                    setPlayingHands(false);
147
+                    setAskToPlayAgain(false);
148
+                }
107 149
             }
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;
150
+            while (playingHands) {
151
+                int bet = Console.numberFromString(Console.askForInput("Starting bet amount: "));
152
+                setCurrentBet(bet);
153
+                startHands();
154
+
155
+                Console.output(getPlayerHand());
156
+
157
+                if (calcHand(dealerHand) == 21) {
158
+                    Console.output("Dealer has BlackJack! You lose!");
159
+                    player.subtractFromBankroll(currentBet);
160
+                    startNextHand();
161
+                } else if (calcHand(playerHand) == 21) {
162
+                    Console.output("You have BlackJack! You win!");
163
+                    player.addToBankroll((currentBet * 3) / 2);
164
+                    startNextHand();
165
+                }
166
+
167
+                int stay = 0;
168
+                while ((calcHand(playerHand) <= 21 && calcHand(dealerHand) <= 21) && stay < 2) {
169
+                    System.out.println(stay);
170
+                    stay = 0;
171
+                    String action = Console.askForInput("Hit or Stay? (H/S)");
172
+
173
+                    if (action.equals("h")) {
174
+                        dealToPlayerHand(1);
175
+                    }
176
+
177
+                    if (action.equals("s")) {
178
+                        stay++;
179
+                    }
180
+
181
+                    if (calcHand(dealerHand) < 17) {
182
+                        dealToDealerHand(1);
183
+                    } else {
184
+                        stay++;
117 185
                     }
118
-                } else if (action.equals("stay")) {
119
-                    break;
186
+
187
+                    Console.output("Player Hand: " + getPlayerHand());
188
+                    Console.output("Dealer Hand: " + showDealerHand());
120 189
                 }
121
-            }
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.");
190
+
191
+                if (calcHand(playerHand) == calcHand(dealerHand)) {
192
+                    Console.output("It's a tie!");
193
+                    Console.output("Player Hand: " + getPlayerHand() + ", " + calcHand(playerHand));
194
+                    startNextHand();
195
+                } else if (calcHand(playerHand) > 21) {
196
+                    Console.output("Dealer wins! You bust.");
197
+                    Console.output("Player Hand: " + getPlayerHand() + ", " + calcHand(playerHand));
198
+                    startNextHand();
199
+                } else if (calcHand(dealerHand) > 21) {
200
+                    Console.output("You win! Dealer bust.");
201
+                    Console.output("Dealer Hand: " + getDealerHand() + ", " + calcHand(dealerHand));
202
+                    startNextHand();
203
+                } else if (calcHand(playerHand) > calcHand(dealerHand)) {
204
+                    Console.output("Player has higher hand.");
205
+                    Console.output("Player Hand: " + getPlayerHand() + ", " + calcHand(playerHand));
206
+                    startNextHand();
207
+                } else if (calcHand(dealerHand) > calcHand(playerHand)) {
208
+                    Console.output("Dealer has higher hand.");
209
+                    Console.output("Player Hand: " + getDealerHand() + ", " + calcHand(playerHand));
210
+                    startNextHand();
132 211
                 }
133 212
             }
134 213
         }
135 214
     }
215
+
216
+    public static void main(String[] args) {
217
+        BlackJack bj = new BlackJack();
218
+        bj.startGame();
219
+    }
136 220
 }
137 221
 
138 222
 

+ 19
- 13
src/main/java/io/zipcoder/casino/Card.java Прегледај датотеку

@@ -4,15 +4,18 @@ public class Card {
4 4
 
5 5
     private Suit suit;
6 6
     private Rank rank;
7
-    private int x;
7
+    private boolean isPrivate = false;
8 8
 
9
+    public Card() {
10
+        this.isPrivate = true;
11
+    }
9 12
 
10 13
     public Card(Suit suit, Rank rank) {
11 14
         this.suit = suit;
12 15
         this.rank = rank;
13
-        x = 0;
14 16
     }
15 17
 
18
+
16 19
     public Rank getRank() {
17 20
         return rank;
18 21
     }
@@ -31,17 +34,20 @@ public class Card {
31 34
 
32 35
     @Override
33 36
     public String toString() {
34
-        String describe = "";
35
-        int x = 5;
36
-        if (suit.equals(Suit.SPADES)) {
37
-            describe = rank + "\u2660 of " + suit + "\n";
38
-        } else if (suit.equals(Suit.CLUBS)) {
39
-            describe = rank + "\u2663 of " + suit + "\n";
40
-        } else if (suit.equals(Suit.DIAMONDS)) {
41
-            describe = rank + "\u2666 of " + suit + "\n";
42
-        } else if (suit.equals(Suit.HEARTS)) {
43
-            describe = rank + "\u2665 of " + suit + "\n";
37
+        if (!isPrivate) {
38
+            String describe = "";
39
+            if (suit.equals(Suit.SPADES)) {
40
+                describe = rank + "\u2660 of " + suit;
41
+            } else if (suit.equals(Suit.CLUBS)) {
42
+                describe = rank + "\u2663 of " + suit;
43
+            } else if (suit.equals(Suit.DIAMONDS)) {
44
+                describe = rank + "\u2666 of " + suit;
45
+            } else if (suit.equals(Suit.HEARTS)) {
46
+                describe = rank + "\u2665 of " + suit;
47
+            }
48
+            return describe;
49
+        } else {
50
+            return "PRIVATE";
44 51
         }
45
-        return describe + x;
46 52
     }
47 53
 }

+ 43
- 43
src/main/java/io/zipcoder/casino/CardGame.java Прегледај датотеку

@@ -1,45 +1,45 @@
1 1
 package io.zipcoder.casino;
2
-
3
-public abstract class CardGame implements Game, Gamble {
4
-
5
-    int playerHand;
6
-    int dealerHand;
7
-    int currentBet;
8
-    Player player;
9
-    CardDeck deck;
10
-
11
-    public CardGame(Player player) {
12
-
13
-        this.player = player;
14
-        this.deck = new deck();
15
-
16
-    }
17
-
18
-
19
-    public int drawCard() {
20
-
21
-
22
-    }
23
-
24
-    public void stand() {
25
-
26
-    }
27
-
28
-    void startGame();
29
-
30
-    void endGame();
31
-
32
-    void declareWinner();
33
-
34
-    void bet(int currentBet) {
35
-
36
-    }
37
-    void winBet() {
38
-
39
-    }
40
-    void loseBet(Player player) {
41
-
42
-    }
43
-
44
-
2
+//
3
+//public abstract class CardGame implements Game, Gamble {
4
+//
5
+//    int playerHand;
6
+//    int dealerHand;
7
+//    int currentBet;
8
+//    Player player;
9
+//    CardDeck deck;
10
+//
11
+//    public CardGame(Player player) {
12
+//
13
+//        this.player = player;
14
+//        this.deck = new deck();
15
+//
16
+//    }
17
+//
18
+//
19
+//    public int drawCard() {
20
+//
21
+//
22
+//    }
23
+//
24
+//    public void stand() {
25
+//
26
+//    }
27
+//
28
+//    void startGame();
29
+//
30
+//    void endGame();
31
+//
32
+//    void declareWinner();
33
+//
34
+//    void bet(int currentBet) {
35
+//
36
+//    }
37
+//    void winBet() {
38
+//
39
+//    }
40
+//    void loseBet(Player player) {
41
+//
42
+//    }
43
+//
44
+//
45 45
 

+ 11
- 11
src/main/java/io/zipcoder/casino/Casino.java Прегледај датотеку

@@ -27,15 +27,15 @@ public class Casino {
27 27
         }
28 28
 
29 29
 
30
-    public static void main(String[] args) {
31
-
32
-        CardDeck cardDeck = new CardDeck();
33
-        cardDeck.getDeck();
34
-        cardDeck.shuffle();
35
-        cardDeck.getDeck();
36
-        System.out.println(cardDeck.draw());
37
-        cardDeck.deal(2);
38
-        Casino cas = new Casino();
39
-        cas.startCasino();
40
-    }
30
+//    public static void main(String[] args) {
31
+//
32
+//        CardDeck cardDeck = new CardDeck();
33
+//        cardDeck.getDeck();
34
+//        cardDeck.shuffle();
35
+//        cardDeck.getDeck();
36
+//        System.out.println(cardDeck.draw());
37
+//        cardDeck.deal(2);
38
+//        Casino cas = new Casino();
39
+//        cas.startCasino();
40
+//    }
41 41
 }