Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need some help completing my lab. We have to code a Crazy 8's card game using existing classes that we created for our last

I need some help completing my lab. We have to code a Crazy 8's card game using existing classes that we created for our last lab, I will post the classes that I have coded below. Here are the rules of the game:

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 cards; 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 <= 13; i++){ cards.add(new Card(s,i)); } } } 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< 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 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 < 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{ 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 (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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions

Question

2. What type of team would you recommend?

Answered: 1 week ago