Question: The objective of this assignment is to implement a basic API for Card games, and then code a simple Card game. Card.java Define a Card

The objective of this assignment is to implement a basic API for Card games, and then code a simple Card game.

Card.java

Define a Card class with the following API:

public Card(Suit suit, int rank)

public int getRank()

public Suit getSuit()

public String toString() // "Ace of Spades"

Note that Suit is an enumerated type. Your Card class should also implement the Comparable interface. Order cards with the different Suits ranked, from lowest to highest, as CLUBS, DIAMONDS, HEARTS, SPADES, and then in ascending order of their rank within the Suit.

Deck.java

Implement a Deck class that keeps an ArrayList of cards. Provide the following API:

public Deck()

public String toString() // print all cards in deck

public boolean isDeckEmpty()

public Card deal() //deals the topmost card in deck

public void shuffle() //shuffles cards in deck

Hand.java

Then define a Hand class, that is a set of cards held by a player. This class also keeps an ArrayList of cards and should support the following API:

public Hand(int m) //specifies initial size of hand

public Card drawNext() //draws "next" card from hand

public Card draw(String s)

//draws specific card from hand, in format "8s", "1c", "11h", "13d"

public void addCard(Card c) //adds card to hand

public ArrayList getCards() // gets the list of Cards in hand

Custom Exception classes

Define the following custom exception classes, and throw them at appropriate places in the preceding classes:

CardException extends RuntimeException

DeckOrHandEmptyException extends CardException

InvalidCardException extends CardException

CardPlayer.java

Now define the abstract class CardPlayer, with the following API:

public CardPlayer(String name) //player name

public abstract Card takeTurn()

//returns the card to be played, from the hand held by this player

public abstract void dealCard(Card card)

//adds a card to the hand held by this player

public String toString()

CardGame.java

And now define the abstract class CardGame, with the following API:

public CardGame(CardPlayer [] players)

public abstract void play()

/* loops through all players while game is not over, having each

player take a turn until game is over, then uses getWinner to display winner */

public abstract boolean isGameOver()

/* check if game is over */

public abstract String getWinner()

/* returns the winner(s) */

Using this class API, we will now implement a simple card game called Higher Card that is played as follows: each player is dealt a number of cards, one per round. Then the players each play a card per round. The player with the highest card wins that round. When all rounds are done, the player(s) who has won the most rounds is the winner. Implement the game as follows:

HigherCardPlayer.java

Define an abstract class HigherCardPlayer that extends CardPlayer with the following API:

public HigherCardPlayer(String name, int rounds)

public abstract void updateRoundsWon();

//updates how many rounds this player has won

public abstract int getRoundsWon();

//retrieves how many rounds this player has won

The HigherCardPlayer also has an instance variable as follows:

protected Hand hand;

HigherCardComputerPlayer.java

Now create a HigherCardComputerPlayer that extends HigherCardPlayer. This class should provide definitions for all the abstract methods in its ancestors. The class simulates the computer playing, and simply randomly chooses the next card to play.

HigherCardGame.java

Finally, define the class HigherCardGame that extends CardGame. It should provide implementations for all the abstract methods in CardGame, that play the Higher card game as described. In the constructor, it should also create the deck and deal out the cards to the players.

The HigherCardGame has an instance variable as follows:

protected Deck deck;

Driver.java

Once you have implemented these classes, put the following code in a class called Driver and simply run it.

int rounds = 3;

HigherCardPlayer[] players = {

new HigherCardComputerPlayer("A",rounds),

new HigherCardComputerPlayer("B",rounds),

new HigherCardComputerPlayer("C",rounds),

new HigherCardComputerPlayer("D",rounds)

};

HigherCardGame game = new HigherCardGame(players,rounds);

game.play();

In all classes, you may provide additional private methods and instance variables but please do not change the public API without discussing this with me.

Your code should be well-organized, with sufficient comments to explain what each significant block of coding accomplishes. You should also follow the standard Java conventions on naming, indentation and BlueJ style documentation.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!