Advertisement
Guest User

here you go

a guest
Dec 14th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. public class Player{
  2.  
  3. //declare your fields here
  4. private double balance;
  5. private Hand pHand = new Hand();
  6.  
  7. //initialize your fields in the constructor
  8. public Player(double balance){
  9.  
  10. this.balance = balance;
  11. }
  12.  
  13. public void deal(Card c){
  14.  
  15. pHand.addCard(c);
  16. }
  17.  
  18. //Returns an array of Cards that the Player wishes to discard.
  19. //The game engine will call deal() on this player for each card
  20. //that exists in the return value. MS2 Instructions: Print the hand to
  21. //the terminal using System.out.println and ask the user which cards
  22. //they would like to discard. The user will first the number of cards they
  23. //wish to discard, followed by the indices, one at a time, of
  24. //the card(s) they would like to discard,
  25. //Return an array with the appropriate Card objects
  26. //that have been discarded, and remove the Card objects from this
  27. //player's hand. Use IO.readInt() for all inputs. In cases of error
  28. //re-ask the user for input.
  29. //
  30. // Example call to discard():
  31. //
  32. // This is your hand:
  33. // 0: Ace of Hearts
  34. // 1: 2 of Diamonds
  35. // 2: 5 of Hearts
  36. // 3: Jack of Spades
  37. // 4: Ace of Clubs
  38. // How many cards would you like to discard?
  39. // 2
  40. // 1
  41. // 2
  42. //
  43. // The resultant array will contain the 2 of Diamonds and the 5 of hearts in that order
  44. // This player's hand will now only have 3 cards
  45.  
  46. public Card[] discard(){
  47.  
  48. boolean falseAmount = true;
  49. boolean falseIndex = true;
  50.  
  51. int numDiscard = 0;
  52.  
  53. int userDiscard = 0;
  54.  
  55. while (falseAmount) {
  56. userDiscard =1;
  57.  
  58. if (userDiscard <= pHand.getCardCount() && userDiscard >=0) {
  59. falseAmount = false;
  60. numDiscard = userDiscard;
  61. }
  62. }
  63.  
  64. Card [] discardCards = new Card[numDiscard];
  65.  
  66. int [] usedIndexes = new int[numDiscard];
  67.  
  68. for (int i = 0; i < usedIndexes.length; i++) {
  69. usedIndexes[i] = -10;
  70. }
  71.  
  72. int counter = 0;
  73. int indexDiscard = 0;
  74.  
  75. for (int i = 0; i<numDiscard; i++) {
  76.  
  77. falseIndex = true;
  78.  
  79.  
  80. while (falseIndex) {
  81. indexDiscard = 0;
  82. if (((indexDiscard != usedIndexes[i]) && (pHand.getCard(indexDiscard) != null) && (indexDiscard >= 0 && indexDiscard < 5) ) ) {
  83. discardCards[i] = pHand.getCard(indexDiscard);
  84. falseIndex = false;
  85. }
  86. }
  87. usedIndexes [counter] = indexDiscard;
  88. counter++;
  89. pHand.removeCard(indexDiscard);
  90. }
  91. return discardCards;
  92. }
  93.  
  94. //Returns the amount that this player would like to wager, returns
  95. //-1.0 to fold hand. Any non zero wager should immediately be deducted
  96. //from this player's balance. This player's balance can never be below
  97. // 0 at any time. This player's wager must be >= to the parameter min
  98. // MS2 Instructions: Show the user the minimum bet via the terminal
  99. //(System.out.println), and ask the user for their wager. Use
  100. //IO.readDouble() for input. In cases of error re-ask the user for
  101. //input.
  102. //
  103. // Example call to wager()
  104. //
  105. // How much do you want to wager?
  106. // 200
  107. //
  108. // This will result in this player's balance reduced by 200
  109.  
  110. public double wager(double min){
  111.  
  112. boolean isBadWager = true;
  113. double wagerAmount = 0;
  114.  
  115. if (min == 0 && balance > 1)
  116. wagerAmount = 1;
  117.  
  118. else if (min == 0 && !(balance > 1) && balance > 0)
  119. wagerAmount = balance;
  120.  
  121. else if (min > balance / 3)
  122. return - 1;
  123.  
  124. else
  125. wagerAmount = min;
  126.  
  127. if (wagerAmount >= min && wagerAmount <= balance ) {
  128. isBadWager = false;
  129. balance -= wagerAmount;
  130. }
  131. return wagerAmount;
  132. }
  133.  
  134. //Returns this player's hand
  135. public Hand showHand(){
  136.  
  137. System.out.println("This is your hand.");
  138. return pHand;
  139. }
  140.  
  141. //Returns this player's current balance
  142. public double getBalance(){
  143.  
  144. return balance;
  145. }
  146.  
  147. //Increase player's balance by the amount specified in the parameter,
  148. //then reset player's hand in preparation for next round. Amount will
  149. //be 0 if player has lost hand
  150. public void winnings(double amount){
  151.  
  152. balance += amount;
  153. pHand.clear();
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement