Package cs171a2

Class CardTable

java.lang.Object
cs171a2.CardTable
All Implemented Interfaces:
Table<Card,CardPlayer>

public class CardTable extends Object implements Table<Card,CardPlayer>
This class represents a table where a game is being played. It implements the Table interface and is designed to work with Card and CardPlayer objects.

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
    Constructor
    Description
    Constructs a CardTable object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a card to the current place on the table.
    void
    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).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public void addCardToPlace(Card card)
      Adds a card to the current place on the table.
      Specified by:
      addCardToPlace in interface Table<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 interface Table<Card,CardPlayer>
      Returns:
      an array of integers representing the identifiers of all cards placed on the table
    • checkPlaces

      public void checkPlaces(CardPlayer player)
      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 interface Table<Card,CardPlayer>
      Parameters:
      player - the player whose card is being checked against the table.