Advertisement
Anon017706349

h1

Sep 28th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Hand
  4. {
  5.  
  6. // Copied from quiz 1 solution. I'm assuming that this is okay.
  7.  
  8. private Card [] cardArray;
  9. private int cardCount;
  10.  
  11. public Hand(int size) // Constructor to create new empty hand
  12. {
  13. cardArray = new Card[size];
  14. cardCount = 0;
  15. }
  16.  
  17. public void addCard (Card newCard)
  18. {
  19. if (cardCount > cardArray.length - 1)
  20. {
  21.  
  22. }
  23.  
  24. cardArray[cardCount++] = newCard;
  25. }
  26.  
  27. public void clear()
  28. {
  29. Arrays.fill(cardArray, null);
  30. }
  31.  
  32. public Card getCard(int position) // Returns the card element in the array cardArray
  33. { // Position is where in the array index the card is
  34. if (position >=0 && position < cardArray.length -1)
  35. {
  36. return cardArray[position];
  37. }
  38.  
  39. else
  40. {
  41. return null;
  42. }
  43.  
  44. }
  45.  
  46. public int getValue() // Adds the the value of the cards in the hand together and returns the total value
  47. {
  48. int totalValue = 0;
  49. int aces = 0;
  50. int rank;
  51.  
  52. for (Card aCard : cardArray)
  53. {
  54. rank = aCard.getRank();
  55.  
  56. if (rank == 1)
  57. {
  58. aces += 1;
  59. }
  60.  
  61. else if (rank == 2)
  62. {
  63. totalValue += 2;
  64. }
  65.  
  66. else if (rank == 3)
  67. {
  68. totalValue += 3;
  69. }
  70.  
  71. else if (rank == 4)
  72. {
  73. totalValue += 4;
  74. }
  75.  
  76. else if (rank == 5)
  77. {
  78. totalValue += 5;
  79. }
  80.  
  81. else if (rank == 6)
  82. {
  83. totalValue += 6;
  84. }
  85.  
  86. else if (rank == 7)
  87. {
  88. totalValue += 7;
  89. }
  90.  
  91. else if (rank == 8)
  92. {
  93. totalValue += 8;
  94. }
  95.  
  96. else if (rank == 9)
  97. {
  98. totalValue += 9;
  99. }
  100.  
  101. else if (rank == 10)
  102. {
  103. totalValue += 10;
  104. }
  105.  
  106. else if (rank == 11)
  107. {
  108. totalValue += 10;
  109. }
  110.  
  111. else if (rank == 12)
  112. {
  113. totalValue += 10;
  114. }
  115.  
  116. else if (rank == 13)
  117. {
  118. totalValue += 10;
  119. }
  120.  
  121. }
  122.  
  123. // Checking the hand for aces and affecting totalValue if there is an ace in the hand
  124.  
  125. for (int i = 0; i < aces; i++)
  126. {
  127. if (totalValue > 10)
  128. {
  129. totalValue += 1;
  130. }
  131.  
  132. else
  133. {
  134. totalValue += 11;
  135. }
  136.  
  137. }
  138.  
  139. return totalValue;
  140.  
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement