Question
Using Java implement the Crazy Eights card game. Here are the rules of our version of the game: Each player is initially dealt n cards
Using Java 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 cards of the same suit match
O cards of the same rank match
O 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 suit that was just declared.
If a player is unable to play at their turn, they must draw the top card from the deck and lose their turn.
If no more cards are available in the deck, then the top card on the discard pile is 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
CrazyEightGame that extends CardGame
Finally, test this on a driver defined in a Driver.java file that plays a crazy eight game between a human player and a computer player, each with 5 cards per hand initially.
HERE IS WHAT I HAVE CODED:
Deck.Java 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< cards.size(); i++){ int swapSlot = random.nextInt(DECK_SIZE); //0-51 Collections.swap(cards, i, swapSlot); } } }
Hand.java
/* * Class that models a hand of cards held by a player */ import java.util.ArrayList; public class Hand { private ArrayList
public Card draw(int n){ if (n < 0 || 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); } }
Card.java * Class that models a playing card */ 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 (thisSuit > otherSuit) return 1; else if (rank < c.getRank()) return -1; else if (rank > c.getRank()) return 1; else return 0; } }
These last 2 classes are abstract:
CardPlayer.java
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; } }
CardGame.java
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(); }
Anything helps. Thank you in advance, I will make sure to rate any help.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started