Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Player{
- //declare your fields here
- private double balance;
- private Hand pHand = new Hand();
- //initialize your fields in the constructor
- public Player(double balance){
- this.balance = balance;
- }
- public void deal(Card c){
- pHand.addCard(c);
- }
- //Returns an array of Cards that the Player wishes to discard.
- //The game engine will call deal() on this player for each card
- //that exists in the return value. MS2 Instructions: Print the hand to
- //the terminal using System.out.println and ask the user which cards
- //they would like to discard. The user will first the number of cards they
- //wish to discard, followed by the indices, one at a time, of
- //the card(s) they would like to discard,
- //Return an array with the appropriate Card objects
- //that have been discarded, and remove the Card objects from this
- //player's hand. Use IO.readInt() for all inputs. In cases of error
- //re-ask the user for input.
- //
- // Example call to discard():
- //
- // This is your hand:
- // 0: Ace of Hearts
- // 1: 2 of Diamonds
- // 2: 5 of Hearts
- // 3: Jack of Spades
- // 4: Ace of Clubs
- // How many cards would you like to discard?
- // 2
- // 1
- // 2
- //
- // The resultant array will contain the 2 of Diamonds and the 5 of hearts in that order
- // This player's hand will now only have 3 cards
- public Card[] discard(){
- boolean falseAmount = true;
- boolean falseIndex = true;
- int numDiscard = 0;
- int userDiscard = 0;
- while (falseAmount) {
- userDiscard =1;
- if (userDiscard <= pHand.getCardCount() && userDiscard >=0) {
- falseAmount = false;
- numDiscard = userDiscard;
- }
- }
- Card [] discardCards = new Card[numDiscard];
- int [] usedIndexes = new int[numDiscard];
- for (int i = 0; i < usedIndexes.length; i++) {
- usedIndexes[i] = -10;
- }
- int counter = 0;
- int indexDiscard = 0;
- for (int i = 0; i<numDiscard; i++) {
- falseIndex = true;
- while (falseIndex) {
- indexDiscard = 0;
- if (((indexDiscard != usedIndexes[i]) && (pHand.getCard(indexDiscard) != null) && (indexDiscard >= 0 && indexDiscard < 5) ) ) {
- discardCards[i] = pHand.getCard(indexDiscard);
- falseIndex = false;
- }
- }
- usedIndexes [counter] = indexDiscard;
- counter++;
- pHand.removeCard(indexDiscard);
- }
- return discardCards;
- }
- //Returns the amount that this player would like to wager, returns
- //-1.0 to fold hand. Any non zero wager should immediately be deducted
- //from this player's balance. This player's balance can never be below
- // 0 at any time. This player's wager must be >= to the parameter min
- // MS2 Instructions: Show the user the minimum bet via the terminal
- //(System.out.println), and ask the user for their wager. Use
- //IO.readDouble() for input. In cases of error re-ask the user for
- //input.
- //
- // Example call to wager()
- //
- // How much do you want to wager?
- // 200
- //
- // This will result in this player's balance reduced by 200
- public double wager(double min){
- boolean isBadWager = true;
- double wagerAmount = 0;
- if (min == 0 && balance > 1)
- wagerAmount = 1;
- else if (min == 0 && !(balance > 1) && balance > 0)
- wagerAmount = balance;
- else if (min > balance / 3)
- return - 1;
- else
- wagerAmount = min;
- if (wagerAmount >= min && wagerAmount <= balance ) {
- isBadWager = false;
- balance -= wagerAmount;
- }
- return wagerAmount;
- }
- //Returns this player's hand
- public Hand showHand(){
- System.out.println("This is your hand.");
- return pHand;
- }
- //Returns this player's current balance
- public double getBalance(){
- return balance;
- }
- //Increase player's balance by the amount specified in the parameter,
- //then reset player's hand in preparation for next round. Amount will
- //be 0 if player has lost hand
- public void winnings(double amount){
- balance += amount;
- pHand.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement