Class CardTable
- All Implemented Interfaces:
Table<Card,
CardPlayer>
Each table instance must keep track of the cards that players place on the table
during the game. The number of places available has a fixed size (NUMBER_OF_PLACES
),
so we use a regular Java array to represent a CardTable's places field.
Each entry in this places array contains
the cards that were added to that place, which is a more dynamic structure (we don't know
in advance how many cards will be added to this place!).
Therefore, each place entry in this array will reference an ArrayList of Card objects.
Here is how to declare the array of ArrayLists field places
:
private ArrayList<Card>[] places = new ArrayList[NUMBER_OF_PLACES];
Note that the Field Summary section below will only show you public fields, but you must declare the required field places described above, which is private. You are also free to create additional fields in your class implementation, if deemed necessary.
-
Field Summary
Fields inherited from interface cs171a2.Table
NUMBER_OF_PLACES
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCardToPlace
(Card card) Adds a card to the current place on the table.void
checkPlaces
(CardPlayer player) Checks if the previous card played by a player matches the rank of any card on the table.int[]
Returns the identifiers of the cards on places 1, 2, 3, and 4 on the table (in that same order).
-
Constructor Details
-
CardTable
public CardTable()Constructs a CardTable object. Initializes the ArrayLists in the places array and sets the currentPlace to 0.
-
-
Method Details
-
addCardToPlace
Adds a card to the current place on the table.- Specified by:
addCardToPlace
in interfaceTable<Card,
CardPlayer> - Parameters:
card
- the card to add to the table.
-
getPlaces
public int[] getPlaces()Returns the identifiers of the cards on places 1, 2, 3, and 4 on the table (in that same order).- Specified by:
getPlaces
in interfaceTable<Card,
CardPlayer> - Returns:
- an array of integers representing the identifiers of all cards placed on the table
-
checkPlaces
Checks if the previous card played by a player matches the rank of any card on the table. If a match is found, the player earns a point and both the player's card and the matching card on the table are removed.- Specified by:
checkPlaces
in interfaceTable<Card,
CardPlayer> - Parameters:
player
- the player whose card is being checked against the table.
-