Question: Here is what I have coded so far: CARD.JAVA public class Card implements Comparable { private Suit suit; private int rank; public Card(Suit suit, int

Here is what I have coded so far: CARD.JAVAHere is what I have coded so far: CARD.JAVA

Here is what I have coded so far:

CARD.JAVA

public class Card implements Comparable{ private Suit suit; private int rank; public Card(Suit suit, int rank) { this.rank = rank; this.suit = suit; } public int getRank() { return rank; }

public Suit getSuit() { return suit; }

public String toString(){ String rankName = ""; switch(rank) { case 1: rankName = "ACE"; break; case 2: rankName = "TWO"; break; case 3: rankName = "THREE"; break; case 4: rankName = "FOUR"; break; case 5: rankName = "FIVE"; break; case 6: rankName = "SIX"; break; case 7: rankName = "SEVEN"; break; case 8: rankName = "EIGHT"; break; case 9: rankName = "NINE"; break; case 10: rankName = "TEN"; break; case 11: rankName = "JACK"; break; case 12: rankName = "QUEEN"; break; case 13: rankName = "KING"; break; default: rankName = "INVALID"; } String output = rankName + " of " + suit; return output ; }

public int compareTo(Card c) { //get the numeric values of the enumerated types int thisSuit = suit.ordinal(); int otherSuit = c.getSuit().ordinal(); if (thisSuit otherSuit) return 1; else if (rank c.getRank()) return 1; else return 0; } }

CARDEXCEPTION

public class CardException extends RuntimeException { public CardException(String s) { super(s); }

}

CARDGAME

public abstract class CardGame { protected CardPlayer [] players; public CardGame(CardPlayer [] players) { this.players = players; } public abstract void play(); public abstract boolean isGameOver(); public abstract String getWinner(); }

CARDPLAYER

public abstract class CardPlayer { protected String name; public CardPlayer(String name) { this.name = name; } public abstract Card takeTurn();

public abstract void dealCard(Card card); public String toString() { return name; } }

DECK

import java.util.Random; import java.util.ArrayList; import java.util.Collections;

public class Deck { private final int DECK_SIZE = 52; private ArrayList cards; //Creates a full deck public Deck() { cards = new ArrayList(DECK_SIZE); Suit [] suits = {Suit.CLUBS, Suit.DIAMONDS, Suit.HEARTS, Suit.SPADES}; int j = 0; for (Suit s: suits){ for (int i = 1; i cards) { this.cards = cards; } public String toString() { String output = "[ "; for (int i = cards.size()-1; i >= 0; i--) output += "\t" +i+": "+ cards.get(i) + ", "; return output + "]"; }

public boolean isDeckEmpty(){ if (cards.size() == 0) return true; return false; } public Card deal() { if (cards.size() == 0) throw new DeckOrHandEmptyException(); Card c = cards.get(cards.size()-1); cards.remove(cards.size()-1); return c; } public void shuffle(){ //Standard approach to shuffle cards //Walk through each card slot, and pick another random slot to exchange it with. Random random = new Random(); for (int i = 0; i

DECKOREMPTYHAND

public class DeckOrHandEmptyException extends CardException{ public DeckOrHandEmptyException() { super("Deck or Hand empty"); }

}

HAND

import java.util.ArrayList; public class Hand { private ArrayList cards; public Hand(int m){ //Does not use the parameter cards = new ArrayList(); } public Card drawNext(){ if (cards.size() == 0) throw new DeckOrHandEmptyException(); Card c = cards.get(cards.size()-1); cards.remove(cards.size()-1); return c; }

public Card draw(int n){ if (n = cards.size()) throw new InvalidCardException(); Card c = cards.get(n); cards.remove(n); return c; } public void addCard(Card c){ //adds it anywhere in hand, currently at the end cards.add(c); } }

HIGHERCARDCOMPUTERPLAYER

public class HigherCardComputerPlayer extends HigherCardPlayer { private Hand hand; private int rounds; //do not need this, for my implementation; private int roundsWon; public HigherCardComputerPlayer(String name, int rounds) { super(name, rounds); this.rounds = rounds; hand = new Hand(rounds); roundsWon = 0; }

public Card takeTurn(){ return hand.drawNext(); }

public void dealCard(Card card){ hand.addCard(card); } public void updateRoundsWon(){ roundsWon++; } public int getRoundsWon(){ return roundsWon; } }

HIGHERCARDGAME

public class HigherCardGame extends CardGame{ private Deck deck; private int rounds, turns; public HigherCardGame(HigherCardPlayer [] players, int rounds) { super(players); this.rounds = rounds; this.turns = 0; deck = new Deck(); deck.shuffle(); //deal one card per player per round for (int i = 0; i

private int getRoundWinner(Card[] playedThisTurn){ //Find greatest card int greatest = 0; for (int i=1; i 0) { greatest = i; } } return greatest; } public boolean isGameOver(){ if (turns >= rounds) return true;

return false; } public String getWinner(){ //Find greatest card int greatest = 0; for (int i=1; i a) { greatest = i; } } return "" + players[greatest]; } }

HIGHERCARDPLAYER

public abstract class HigherCardPlayer extends CardPlayer { protected Hand hand; protected int rounds; protected int roundsWon; public HigherCardPlayer(String name, int rounds) { super(name); this.rounds = rounds; hand = new Hand(rounds); roundsWon = 0; } public abstract void updateRoundsWon(); public abstract int getRoundsWon(); }

INVALIDCARDEXCEPTION

public class InvalidCardException extends CardException { public InvalidCardException() { super("Invalid Card exception"); }

}

SUIT

public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

DRIVER

public class Driver { public static void main(String[] args) {

int rounds = 10; 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(); } }

Please provide the code with the classes in the instructions.

Updates 1. You may add the following method to the Deck API public Deck (ArrayList) that creates a new Deck with the given array list of cards, instead of the default 52 cards in a full deck. This assignment picks up where we left off with the previous assignment. Your objective is to use our card API to implement the Crazy Eights card game. Here are the rules of our version of the game: Each player is initially dealt n cards from a deck of 52 cards, where n is specified by the user . The winner of the game is the first person to discard (play) all the cards in their hand. To begin play after the cards are dealt, one card is drawn from the deck and placed on top of a discard pile (which is empty when the game starts). At their turn, each player must play a card that matches the top card on the pile. The matching is determined as follows . o o o cards of the same suit match cards of the same rank match A card with rank 8 can be used to change the suit of the current card on top of the pile. This current top card is now deemed to have a rank of 8 and the new If a player is unable to play at their turn, they must draw the top card from the deck . If no more cards are available in the deck, then the top card on the discard pile is suit that was just declared. and lose their turn. retained, and all other cards in the discard pile are added to the deck, which is then shuffled. And the game then resumes, from where it stopped. The game ends when there is a winner, or when no player can play a card, and there . are no more cards in the deck. Your task is to code: . a game that enforces these rules . a human player that can select cards and play the game . a basic computer player that just plays the first card it can Do this by coding the following classes . CrazyEightPlayer that extends CardPlayer . CrazyEightComputerPlayer that extends CrazyEightPlayer .CrazyEightHumanPlayer that extends CrazyEightPlayer Updates 1. You may add the following method to the Deck API public Deck (ArrayList) that creates a new Deck with the given array list of cards, instead of the default 52 cards in a full deck. This assignment picks up where we left off with the previous assignment. Your objective is to use our card API to implement the Crazy Eights card game. Here are the rules of our version of the game: Each player is initially dealt n cards from a deck of 52 cards, where n is specified by the user . The winner of the game is the first person to discard (play) all the cards in their hand. To begin play after the cards are dealt, one card is drawn from the deck and placed on top of a discard pile (which is empty when the game starts). At their turn, each player must play a card that matches the top card on the pile. The matching is determined as follows . o o o cards of the same suit match cards of the same rank match A card with rank 8 can be used to change the suit of the current card on top of the pile. This current top card is now deemed to have a rank of 8 and the new If a player is unable to play at their turn, they must draw the top card from the deck . If no more cards are available in the deck, then the top card on the discard pile is suit that was just declared. and lose their turn. retained, and all other cards in the discard pile are added to the deck, which is then shuffled. And the game then resumes, from where it stopped. The game ends when there is a winner, or when no player can play a card, and there . are no more cards in the deck. Your task is to code: . a game that enforces these rules . a human player that can select cards and play the game . a basic computer player that just plays the first card it can Do this by coding the following classes . CrazyEightPlayer that extends CardPlayer . CrazyEightComputerPlayer that extends CrazyEightPlayer .CrazyEightHumanPlayer that extends CrazyEightPlayer

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!