Explorar el Código

extracted methods

ThuyKhong hace 6 años
padre
commit
e4bc032063
Se han modificado 1 ficheros con 70 adiciones y 47 borrados
  1. 70
    47
      src/main/java/io/zipcoder/casino/cardgames/BlackJack.java

+ 70
- 47
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java Ver fichero

@@ -42,6 +42,10 @@ public class BlackJack extends CardGame implements Gamble {
42 42
 
43 43
         Console.println("The dealer is dealing cards to the players.");
44 44
 
45
+        for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
46
+            placeBet();
47
+        }
48
+
45 49
         dealerHand.add(deck.removeFirst());
46 50
         dealerHand.add(deck.removeFirst());
47 51
 
@@ -52,47 +56,39 @@ public class BlackJack extends CardGame implements Gamble {
52 56
             Console.println("Dealer has +," +  dealerHand.display());
53 57
             Console.println("You have " + currentPlayer.getHand().display());
54 58
             Console.println("Dealer has blackjack. Dealer wins.");
59
+            for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
60
+                evaluateBet(p.getP(), bet);
61
+            }
55 62
         }
63
+            evalBlackjack();
56 64
 
57
-        for (currentPlayer : blackJackPlayers) {
58
-
59
-          Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
60
-
61
-          placeBet();
62
-
63
-          if (getSum(currentPlayer.getHand()) == 21){
64
-              Console.println("Dealer has " + dealerHand.display());
65
-              Console.println("You have " + currentPlayer.getHand().display());
66
-              Console.println("You have blackjack. You win.");
67
-          }
68
-
69
-            while(true) {
65
+            playersTurn();
70 66
 
71
-                Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
72 67
 
73
-                String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
68
+        if (dealersTurn()) return false;
74 69
 
75
-                while (getSum(currentPlayer.getHand()) < 21) {
76
-                    if (hitOrStand.equalsIgnoreCase("H")) {
77
-                        dealCard(currentPlayer,1);
78
-                    } else if (hitOrStand.equalsIgnoreCase("S")) {
79
-                        break;
80
-                    } else {
81
-                        hitOrStand = Console.getStringInput("Invalid input. Please enter H for Hit or S for Stand");
82
-                    }
83
-                }
84 70
 
85
-                Console.println("You have: " + currentPlayer.getHand().display());
86
-                Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
71
+        return evalWinner(currentPlayer, bet);
87 72
 
88
-                if (getSum(currentPlayer.getHand()) > 21){
89
-                    Console.println("You busted. House wins.");
90
-                    return false;
91
-                }
73
+    }
92 74
 
93
-            }
75
+    public boolean evalWinner(BlackJackPlayer currentPlayer, long bet) {
76
+        if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
77
+            Console.println("Dealer wins on a tie.");
78
+            evaluateBet(currentPlayer.getP(), -bet);
79
+            return false;
80
+        } else if (getSum(dealerHand) <=21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
81
+            Console.println("Dealer wins.");
82
+            evaluateBet(currentPlayer.getP(), -bet);
83
+            return false;
84
+        } else {
85
+            Console.println("You win!");
86
+            evaluateBet(currentPlayer.getP(), bet);
87
+            return false;
94 88
         }
89
+    }
95 90
 
91
+    public boolean dealersTurn() {
96 92
         Console.println("Dealer's card are: " + dealerHand.display());
97 93
         Console.println("The sum of dealer's cards is " + getSum(dealerHand));
98 94
 
@@ -103,26 +99,54 @@ public class BlackJack extends CardGame implements Gamble {
103 99
 
104 100
         if (getSum(dealerHand) > 21) {
105 101
             Console.println("Dealer busted. You win.");
106
-            return false;
102
+            return true;
107 103
         }
104
+        return false;
105
+    }
108 106
 
109
-        if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
110
-            Console.println("Dealer wins on a tie.");
111
-            evaluateBet(currentPlayer.getP(), -bet);
112
-            return false;
113
-        } else if (getSum(dealerHand) <=21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
114
-            Console.println("Dealer wins.");
115
-            evaluateBet(currentPlayer.getP(), -bet);
116
-            return false;
117
-        } else {
118
-            Console.println("You win!");
119
-            evaluateBet(currentPlayer.getP(), bet);
120
-            return false;
107
+    public void playersTurn() {
108
+
109
+        for (BlackJackPlayer currentPlayer : this.blackJackPlayers) {
110
+
111
+            Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
112
+
113
+            Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
114
+
115
+            hitOrStand(currentPlayer);
116
+
117
+            Console.println("You have: " + currentPlayer.getHand().display());
118
+            Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
119
+
120
+            if (getSum(currentPlayer.getHand()) > 21){
121
+                Console.println("You busted. House wins.");
122
+            }
121 123
         }
124
+    }
125
+
126
+    public void hitOrStand(BlackJackPlayer currentPlayer) {
127
+        String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
122 128
 
129
+        while (getSum(currentPlayer.getHand()) < 21) {
130
+            if (hitOrStand.equalsIgnoreCase("H")) {
131
+                dealCard(currentPlayer, 1);
132
+            } else if (hitOrStand.equalsIgnoreCase("S")) {
133
+                break;
134
+            } else {
135
+                hitOrStand = Console.getStringInput("Invalid input. Please enter H for Hit or S for Stand");
136
+            }
137
+        }
138
+    }
139
+
140
+    public void evalBlackjack() {
141
+        for (BlackJackPlayer p: this.blackJackPlayers)
142
+        if (getSum(p.getHand()) == 21){
143
+            Console.println("Dealer has " + dealerHand.display());
144
+            Console.println("You have " + p.getHand().display());
145
+            Console.println("You have blackjack. You win.");
146
+        }
123 147
     }
124 148
 
125
-}
149
+
126 150
 
127 151
     public void dealCards(int numberOfCards) {
128 152
 
@@ -184,6 +208,5 @@ public class BlackJack extends CardGame implements Gamble {
184 208
         Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit());
185 209
         dealerHand.add(newCard);
186 210
     }
187
-
188
-
211
+    
189 212
 }