Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. /*
  2. Contains methods and variables that keeps track of the player's cards and name,
  3. such that each Player object has their own cards and name that can be keept track of during the game.
  4. */
  5.  
  6. public class Player{
  7.    
  8.  
  9. //Initializations
  10. //private variable name initialization
  11.     private final String playerName;
  12.  
  13. //private class object initialization
  14.     private Card[] drawnCards = new Card[3]; //max three cards in the hand
  15.     private Card[] cardsCollected = new Card[40]; //max 40 cards in the card collection
  16.    
  17. //constructor sets the player's name
  18.     public Player(String setName){
  19.         this.playerName = setName;
  20.     }
  21.    
  22. //Methods
  23. //get method for name
  24.     public String name() {
  25.         return this.playerName;
  26.     }
  27.    
  28. //returns a copy of the player's hand
  29.     public Card[] hand(){
  30.  
  31.         int numNotNullCards = 0;
  32.         int numCardsInHand = cardsInHand();
  33.         Card[] drawnCardsNotNull = new Card[numCardsInHand];
  34.  
  35.  
  36.         for (int i = 0; i < 3; i++) {
  37.             if (this.drawnCards[i] != null) {
  38.                 drawnCardsNotNull[numNotNullCards] = this.drawnCards[i];
  39.                 numNotNullCards += 1;
  40.             }
  41.         }
  42.         return drawnCardsNotNull;
  43.     }
  44.    
  45.    
  46. //returns the number of cards held in the player's hand as an integer
  47.     public int cardsInHand(){
  48.         int cardSum = 0;
  49.         for (Card drawnCard : drawnCards) {
  50.             if (drawnCard != null) {
  51.                 cardSum ++;
  52.             }
  53.         }
  54.         return cardSum;
  55.     }
  56.    
  57.    
  58. //adds the passed card to the player's hand
  59.     public void addToHand(Card card){
  60.         boolean emptyIndexCheck = false;
  61.         int i = 0;
  62.         while (emptyIndexCheck != true) {
  63.                 if (i == drawnCards.length ) {
  64.                     emptyIndexCheck = true;
  65.                 }
  66.                 if (this.drawnCards[i] == null) {
  67.                     this.drawnCards[i] = card;
  68.                     emptyIndexCheck = true;
  69.                 }
  70.             i++;
  71.         }
  72.     }
  73.    
  74.    
  75. //adds the passed card to the player's collected cards
  76.     public void addToCollectedCards(Card card){
  77.     boolean emptyIndexCheck = false;
  78.     int i = 0;
  79.     while (emptyIndexCheck != true) {
  80.             if ( i == cardsCollected.length) {
  81.                 emptyIndexCheck = true;
  82.             }
  83.             if (this.cardsCollected[i] == null) {
  84.                 this.cardsCollected[i] = card;
  85.                 emptyIndexCheck = true;
  86.             }
  87.             i++;
  88.        }
  89.     }
  90.    
  91. //removes any instance of the passed card from the player's hand
  92.     public void removeFromHand(Card card){
  93.     for (int i = 0; i < drawnCards.length; i++){
  94.             if (this.drawnCards[i] == card){
  95.                 this.drawnCards[i] = null;
  96.             }
  97.         }
  98.     }
  99.    
  100. //returns a copy of the player's collected cards
  101.     public Card[] collectedCards(){
  102.         return this.cardsCollected;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement