Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class Hand
- {
- // Copied from quiz 1 solution. I'm assuming that this is okay.
- private Card [] cardArray;
- private int cardCount;
- public Hand(int size) // Constructor to create new empty hand
- {
- cardArray = new Card[size];
- cardCount = 0;
- }
- public void addCard (Card newCard)
- {
- if (cardCount > cardArray.length - 1)
- {
- }
- cardArray[cardCount++] = newCard;
- }
- public void clear()
- {
- Arrays.fill(cardArray, null);
- }
- public Card getCard(int position) // Returns the card element in the array cardArray
- { // Position is where in the array index the card is
- if (position >=0 && position < cardArray.length -1)
- {
- return cardArray[position];
- }
- else
- {
- return null;
- }
- }
- public int getValue() // Adds the the value of the cards in the hand together and returns the total value
- {
- int totalValue = 0;
- int aces = 0;
- int rank;
- for (Card aCard : cardArray)
- {
- rank = aCard.getRank();
- if (rank == 1)
- {
- aces += 1;
- }
- else if (rank == 2)
- {
- totalValue += 2;
- }
- else if (rank == 3)
- {
- totalValue += 3;
- }
- else if (rank == 4)
- {
- totalValue += 4;
- }
- else if (rank == 5)
- {
- totalValue += 5;
- }
- else if (rank == 6)
- {
- totalValue += 6;
- }
- else if (rank == 7)
- {
- totalValue += 7;
- }
- else if (rank == 8)
- {
- totalValue += 8;
- }
- else if (rank == 9)
- {
- totalValue += 9;
- }
- else if (rank == 10)
- {
- totalValue += 10;
- }
- else if (rank == 11)
- {
- totalValue += 10;
- }
- else if (rank == 12)
- {
- totalValue += 10;
- }
- else if (rank == 13)
- {
- totalValue += 10;
- }
- }
- // Checking the hand for aces and affecting totalValue if there is an ace in the hand
- for (int i = 0; i < aces; i++)
- {
- if (totalValue > 10)
- {
- totalValue += 1;
- }
- else
- {
- totalValue += 11;
- }
- }
- return totalValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement