commit 3e7b6deeddaee23f322a9a1d564bb70ba72de871
parent aace6103747924cd50d2fcfac14b8e6adeb6f23f
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date: Fri, 7 Oct 2022 19:56:06 +0200
Update Hand.java
Diffstat:
1 file changed, 50 insertions(+), 2 deletions(-)
diff --git a/src/controller/Hand.java b/src/controller/Hand.java
@@ -6,7 +6,7 @@ import model.Card;
import model.Deck;
/**
- * Contains methods to calculate the value of a hand,
+ * Contains methods to calculate the value of a hand,
* take cards from a deck and
* discard the hand to a deck.
*
@@ -17,13 +17,61 @@ public class Hand {
private ArrayList<Card> hand;
/**
- * Hand constructor
+ * Hand Constructor:
+ * Creates empty hand
*/
public Hand() {
hand = new ArrayList<Card>();
}
+ /**
+ * Mehtod to add a card from the deck to the hand
+ *
+ * @param deck deck to take card from
+ */
public void takeCardFromDeck(Deck deck) {
hand.add(deck.takeCard());
}
+
+ /**
+ * Method to calulate the value of a hand
+ *
+ * @return value value of the hand
+ */
+ public int calculateValue() {
+
+ int value = 0; // current total value
+ int aceCount = 0; // number of aces
+
+ for (Card card : hand) {
+ value += card.getValue(); // add card value to hand
+
+ // count number of aces
+ if (card.getValue() == 11) {
+ ++aceCount;
+ }
+ }
+
+ // for the scenario where we have multiple aces
+ // go back and set each ace to 1 until get back under 21, if possible
+ if(value > 21 && aceCount > 0) {
+ while(aceCount > 0 && value > 21) {
+ -- aceCount;
+ value -= 10;
+ }
+ }
+
+ return value;
+ }
+
+ @Override
+ public String toString() {
+
+ String ret = ""; // holds string to return
+ for (Card card : hand) {
+ ret += card + " - ";
+ }
+
+ return ret;
+ }
}