Kaynağa Gözat

Merge branch 'Working' of tkhong102/ZCW-OOP-Casino into Working

nedredmond 6 yıl önce
ebeveyn
işleme
d904fa726f

+ 34
- 19
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java Dosyayı Görüntüle

15
 import java.util.Vector;
15
 import java.util.Vector;
16
 
16
 
17
 public class BlackJack extends CardGame implements Gamble {
17
 public class BlackJack extends CardGame implements Gamble {
18
-    long upcardsValue = 0;
19
 
18
 
20
     private Deck deck = new Deck();
19
     private Deck deck = new Deck();
21
     private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
20
     private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
25
 
24
 
26
     public BlackJack(){
25
     public BlackJack(){
27
         readyPlayers();
26
         readyPlayers();
28
-        placeBet();
29
-        run();
27
+        dealCards(2);
28
+        for (BlackJackPlayer p : blackJackPlayers) {
29
+          run();
30
+        }
30
     }
31
     }
31
 
32
 
32
     public void readyPlayers() {
33
     public void readyPlayers() {
42
     }
43
     }
43
 
44
 
44
     public boolean play(BlackJackPlayer currentPlayer, long bet) {
45
     public boolean play(BlackJackPlayer currentPlayer, long bet) {
46
+        placeBet();
47
+        // this.bet = bet;
45
 
48
 
46
-        this.bet = bet;
49
+        Console.println("The dealer is dealing cards to the players.");
47
 
50
 
48
-        dealCards(2);
49
         dealerHand.add(deck.removeFirst());
51
         dealerHand.add(deck.removeFirst());
50
         dealerHand.add(deck.removeFirst());
52
         dealerHand.add(deck.removeFirst());
51
 
53
 
54
+        Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
55
+
52
         if (getSum(dealerHand) == 21){
56
         if (getSum(dealerHand) == 21){
53
             Console.println("Dealer has +," +  dealerHand.display());
57
             Console.println("Dealer has +," +  dealerHand.display());
54
             Console.println("You have " + currentPlayer.getHand().display());
58
             Console.println("You have " + currentPlayer.getHand().display());
55
-            Console.println("Dealer win.");
59
+            Console.println("Dealer has blackjack. Dealer wins.");
56
         }
60
         }
57
 
61
 
62
+
63
+        Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
64
+
58
         if (getSum(currentPlayer.getHand()) == 21){
65
         if (getSum(currentPlayer.getHand()) == 21){
59
             Console.println("Dealer has " + dealerHand.display());
66
             Console.println("Dealer has " + dealerHand.display());
60
             Console.println("You have " + currentPlayer.getHand().display());
67
             Console.println("You have " + currentPlayer.getHand().display());
61
-            Console.println("You win.");
68
+            Console.println("You have blackjack. You win.");
62
         }
69
         }
63
-
70
+        
64
         while(true) {
71
         while(true) {
65
 
72
 
66
-            Console.println("You have: " + currentPlayer.getHand().display());
67
-            Console.println("Dealer is showing a " + dealerHand.get(0).getCard());
73
+            Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
74
+
68
             String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
75
             String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
69
 
76
 
70
             while (getSum(currentPlayer.getHand()) < 21) {
77
             while (getSum(currentPlayer.getHand()) < 21) {
71
-                if (hitOrStand.equals("H")) {
78
+                if (hitOrStand.equalsIgnoreCase("H")) {
72
                     dealCard(currentPlayer,1);
79
                     dealCard(currentPlayer,1);
73
-                } else if (hitOrStand.equals("S")) {
80
+                } else if (hitOrStand.equalsIgnoreCase("S")) {
74
                     break;
81
                     break;
75
                 } else {
82
                 } else {
76
                     hitOrStand = Console.getStringInput("Invalid input. Please enter H for Hit or S for Stand");
83
                     hitOrStand = Console.getStringInput("Invalid input. Please enter H for Hit or S for Stand");
78
             }
85
             }
79
 
86
 
80
             Console.println("You have: " + currentPlayer.getHand().display());
87
             Console.println("You have: " + currentPlayer.getHand().display());
88
+            Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
89
+
90
+            if (getSum(currentPlayer.getHand()) > 21){
91
+                Console.println("You busted. House wins.");
92
+                return false;
93
+            }
94
+
81
             Console.println("Dealer's card are: " + dealerHand.display());
95
             Console.println("Dealer's card are: " + dealerHand.display());
96
+            Console.println("The sum of dealer's cards is " + getSum(dealerHand));
82
 
97
 
83
             while (getSum(dealerHand) < 17) {
98
             while (getSum(dealerHand) < 17) {
84
-                dealerHand.add(deck.removeFirst());
85
                 revealCard();
99
                 revealCard();
86
                 Console.println("Dealer's sum is " + getSum(dealerHand));
100
                 Console.println("Dealer's sum is " + getSum(dealerHand));
87
             }
101
             }
102
+
88
             if (getSum(dealerHand) > 21) {
103
             if (getSum(dealerHand) > 21) {
89
                 Console.println("Dealer busted. You win.");
104
                 Console.println("Dealer busted. You win.");
105
+                return false;
90
             }
106
             }
91
 
107
 
92
             if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
108
             if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
122
     }
138
     }
123
 
139
 
124
     public void placeBet() {
140
     public void placeBet() {
125
-        bet = Console.getLongInput("Please enter your bet.");
141
+        this.bet = Console.getLongInput("Please enter your bet.");
126
     }
142
     }
127
 
143
 
128
 
144
 
138
     }
154
     }
139
 
155
 
140
 
156
 
141
-    public int  getSum(CardHand cardHands) {
157
+    public int  getSum(CardHand cardHand) {
142
         int cardSum = 0;
158
         int cardSum = 0;
143
         boolean ace = false;
159
         boolean ace = false;
144
 
160
 
145
-        for (int i = 0; i < cardHands.size(); i++) {
146
-            int cardValue = cardHands.get(i).getCardValue().getCardValue();
161
+        for (int i = 0; i < cardHand.size(); i++) {
162
+            int cardValue = cardHand.get(i).getCardValue().getCardValue();
147
 
163
 
148
             if (cardValue > 10) {
164
             if (cardValue > 10) {
149
                 cardValue = 10;   // For a Jack, Queen, or King.
165
                 cardValue = 10;   // For a Jack, Queen, or King.
153
             }
169
             }
154
             cardSum = cardSum + cardValue;
170
             cardSum = cardSum + cardValue;
155
         }
171
         }
156
-        if (ace == true && cardSum + 10 <= 21) {
172
+        if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
157
             cardSum = cardSum + 10;
173
             cardSum = cardSum + 10;
158
         }
174
         }
159
         return cardSum;
175
         return cardSum;
160
     }
176
     }
161
 
177
 
162
-
163
     public void revealCard(){
178
     public void revealCard(){
164
         Card newCard = drawCard();
179
         Card newCard = drawCard();
165
         Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit());
180
         Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit());