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.JAVA
public class Card implements Comparable
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
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
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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
