Package cs171a2

Class CardPlayer

Type Parameters:
Card - the type of card used in the game

public class CardPlayer extends GeneralPlayer<Card>
A class that represents a card player. For each card player instance, we should keep track of how many points they earned in the game so far, as well as whether it is their turn or not. Additionally, their hand and bank of cards should be stored in two separate ArrayLists of Card objects.

A player's points, turn, and hand of cards should all be declared private fields, whereas the bank of cards should be public, as follows:

private int points; private boolean turn; private ArrayList<Card> hand = new ArrayList<Card>(); public ArrayList<Card> bank = new ArrayList<Card>();

Note that the Field Summary section below will only show you public fields, but you must declare all the fields described above in your implementation of this class, including the private fields. You are free to create additional fields if deemed necessary.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    the player's bank of cards.

    Fields inherited from class cs171a2.GeneralPlayer

    name
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new card player with a default name.
    Creates a new card player with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a card to the player's hand.
    Returns a message showing the player's bank of cards.
    Gets the player's hand of cards.
    int
    Gets the number of points the player has earned.
    Returns a message showing the player's hand of cards.
    boolean
    Returns whether it's the player's turn.
    Gets the top card from the player's hand and removes it from the hand.
    void
    setPoints(int points)
    Sets the number of points the player has earned.
    void
    setTurn(boolean turn)
    Sets whether it's the player's turn.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • bank

      public ArrayList<Card> bank
      the player's bank of cards. Cards that had a match
  • Constructor Details

    • CardPlayer

      public CardPlayer(String name)
      Creates a new card player with the given name.
      Parameters:
      name - the name of the player
    • CardPlayer

      public CardPlayer()
      Creates a new card player with a default name.
  • Method Details

    • getPoints

      public int getPoints()
      Gets the number of points the player has earned.
      Returns:
      the number of points
    • setPoints

      public void setPoints(int points)
      Sets the number of points the player has earned.
      Parameters:
      points - the number of points
    • isTurn

      public boolean isTurn()
      Returns whether it's the player's turn.
      Returns:
      true if it's the player's turn, false otherwise
    • setTurn

      public void setTurn(boolean turn)
      Sets whether it's the player's turn.
      Parameters:
      turn - true if it's the player's turn, false otherwise
    • addToHand

      public void addToHand(Card card)
      Adds a card to the player's hand.
      Parameters:
      card - the card to add
    • getHand

      public ArrayList<Card> getHand()
      Gets the player's hand of cards.
      Returns:
      the player's hand of cards
    • handToString

      public String handToString()
      Returns a message showing the player's hand of cards.
      Returns:
      a string representation of the player's hand
    • bankToString

      public String bankToString()
      Returns a message showing the player's bank of cards.
      Returns:
      a string representation of the player's bank
    • play

      public Card play()
      Gets the top card from the player's hand and removes it from the hand.
      Specified by:
      play in class GeneralPlayer<Card>
      Returns:
      the top card from the hand, or null if the hand is empty