Package cs171a2
Class CardPlayer
- Type Parameters:
Card
- the type of card used in the game
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
FieldsFields inherited from class cs171a2.GeneralPlayer
name
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new card player with a default name.CardPlayer
(String name) Creates a new card player with the given name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a card to the player's hand.Returns a message showing the player's bank of cards.getHand()
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
isTurn()
Returns whether it's the player's turn.play()
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.
-
Field Details
-
bank
the player's bank of cards. Cards that had a match
-
-
Constructor Details
-
CardPlayer
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
Adds a card to the player's hand.- Parameters:
card
- the card to add
-
getHand
Gets the player's hand of cards.- Returns:
- the player's hand of cards
-
handToString
Returns a message showing the player's hand of cards.- Returns:
- a string representation of the player's hand
-
bankToString
Returns a message showing the player's bank of cards.- Returns:
- a string representation of the player's bank
-
play
Gets the top card from the player's hand and removes it from the hand.- Specified by:
play
in classGeneralPlayer<Card>
- Returns:
- the top card from the hand, or null if the hand is empty
-