Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please take time to answer the question correctly. Show correct code and the result of compiled code. All the classes are below. Crazy 8s Crazy

Please take time to answer the question correctly. Show correct code and the result of compiled code. All the classes are below.

Crazy 8s

Crazy Eights, also known as Eights and as Swedish Rummy, is a distant relative of Rummy. It's known as a "stops" game because players can be stopped from discarding if they don't have a proper card. It's believed that Crazy Eights can trace its heritage back to the mid-1600s and a French gambling game known as Hoc.

Players: 2 to 4 players.

Deck: Standard 52-card deck.

Goal: To discard all of your cards.

Setup: Choose a dealer. (The program is the dealer)

In a two-player game, each player is dealt seven cards. In a game with three or four players, each player is dealt five cards.

The remaining cards are placed face down in the center of the table, forming a draw pile. The top card of the draw pile is removed and turned face up to start the discard pile.

Gameplay: A player is chosen as the first player. Play moves clockwise. Here, clockwise means the next player in the list holding the players (wrapping around to the first when we go past the end).

On a turn, the given player adds to the discard pile by playing one card from their hand that matches the top card on the discard pile either by suit (diamonds, spades, etc) or by rank (6, 10, jack, etc.).

A player who cannot match the top card on the discard pile by suit or rank must draw cards from the draw pile until they can play one. A player is allowed to pull cards from the draw pile even if they already have a legal play (when it is their turn, before they discard to the discard pile). When the draw pile is empty, a player who cannot add to the discard pile passes their turn.

Power Cards: All eights are wild and can be played on any card during a player's turn. When a player discards an eight, they chooses which suit is now in play. They do this in our game by removing the 8 from their hand and placing a new card object on the discard pile that has rank 8 and the desired new suit.

The next player must play either a card of that suit or another eight.

All twos are special cards. When a player discards a two, the next player must take two cards from the draw pile and then end their turn without playing a card to the discard pile (or taking more cards from the draw pile).

All fours are special. When a player discards a four, the next player misses their turn and play goes to the following player.

All sevens are special. When a player discards a seven, the direction of play is reversed. The next player is the now the player who played just before the player who discarded the seven.

Winning: The first player to discard all of his cards wins.

(With four players, it is possible to play partnership. If you do this, the game ends when both members of a partnership discard all their cards.)

To play multiple games, add up the cards remaining in the losers' hands and give the points to the winner: 10 points for each face card, 1 point for each ace, 50 points for each eight, 25 points for each two or four, 20 points for each seven and the face value for the other number cards.

Games are then played until a player reaches some predetermined goal such as 150, 200, or 1000.

Provided Classes

You are given the following classes: Card, Player (abstract), BadPlayer (as an example player), DiscardPile and the sample program Crazy8s.

Your Tasks

You will implement several new player classes that implement different strategies for players and modify Crazy8s to visualize game play. You must ensure that the games are played properly. You may modify any of the provided classes.

Game Play

You must ensure that a proper game of crazy 8's is played. This means that all played cards (cards added to the discard pile) are valid. At the end of a game, points should be awarded to the winning player.

There should be choice for 2,3 or 4 player games and whether or not it is a single game play or multiple game (with a set points goal).

Player Strategies

RandomPlayer should play a random valid card.

MindTheEights should always be aware of any eights they are holding. This player will their eights until late in the game, but won't hold on to them too long (as they are worth a lot of points). Once any opponent goes down to one card, it's time to play your eight. If you have two eights, start playing them once an opponent goes down to two cards. (Expand this for 3 or 4 or more eights.)

HamperLeader will try to hamper the progress of the leader if the leader is either the next or previous player. If the next player is the leader (least amount of cards) then this player will try to hamper their progress by playing a power card. If the previous player is the leader, this player will hold on to their power cards until the direction of play is reversed and then hamper them (if this player has a seven they will change direction so that they can try to hamper the leader).

DiscardHighPoints will try to discard their highest point cards as soon as they can. This strategy aims to prevent the winner of a game (if is a different player) from obtaining too many points. This player will try to change suits whenever possible to a different suit if they have high point cards of that different suit.

ExtraCards will risk taking cards from the draw pile in an effort to get power cards. They will be clever with this though. If the next player only has 1 card left, they will keep picking a card until they get a power card (if they do not already have one) so that they can try to prevent the next player from winning. They will not take more than one extra card in the early rounds of the game. They will not take extra cards if they already have power cards in their hand.

Five player teams: MemoryPlayer will analyze the full discard pile at each turn and make decisions based on cards played by every player. A MemoryPlayer player is not allowed to use the instanceof method to check which kind of player each player is. This player should simulate a clever player who remembers each card played by each player during the game.

BADPLAYER.JAVA

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Stack;
public class BadPlayer extends Player{
public BadPlayer(Card[] cards){this.hand = new ArrayList(Arrays.asList(cards));}
/* play a card */
public boolean play(DiscardPile discardPile,
Stack drawPile,
ArrayList players)
{
discardPile.add(this.hand.remove(0));
if( this.hand.size() == 0 ){return true;}
return false;
}

CARD.JAVA

import java.util.HashMap;
public class Card implements Comparable{
/* handy arrays for ranks and suits */
/* do NOT change these */
public final static String[] RANKS = { "None", "None",
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"};
public final static String[] SUITS = { "Diamonds",
"Clubs", "Hearts", "Spades", "None"};
protected String suit;
protected String rank;
protected HashMap rankValue;
/** creates a card with specified suit and rank
*
* @param suit is the suit of the card (must be a string from Card.SUITS)
* @param rank is the rank of the card (must be a string from Card.RANKS)
*/
public Card(String suit, String rank){
// assume input is valid!
this.suit = suit;
this.rank = rank;
this.rankValue = new HashMap(15);
for(int r = 2; r < RANKS.length; r+=1){
this.rankValue.put(RANKS[r], r);
}
}
/** the numerical representation of the rank of the current card
*

* ranks have the numerical values
* 2 = 2, 3 = 3, ..., 10 = 10
* Jack = 11, Queen = 12, King = 13, Ace = 14
*
* @return the numerical rank of this card
*/
public int getRank(){
if(this.rank.equals(RANKS[0])){ return -1; } // "no card"
return rankValue.get(this.rank);
}
/** the string representation of the rank of the current card
*
* @return the string representation of the rank of this card (must be a string from Card.RANKS)
*/
public String getRankString(){ return this.rank; }
/** the suit of the current card
*
* @return the suit of this card (must be a string from Card.SUITS)
*/
public String getSuit(){ return this.suit; }
@Override
public int compareTo(Card other){
return 0;
}
@Override
public final String toString(){
// outputs a string representation of a card object
if(this.rank.equals(RANKS[0])){
return "no card";
}
int r = getRank();
if( r >= 2 && r <= 14 ){
return r + getSuit().substring(0,1);
}
return "no card";
CRAZY8GAME.JAVA

}

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.Random;
public class Crazy8Game{
public static void main(String[] args){
/* create the deck */
Card[] deck = new Card[52];
int index = 0;
for(int r=2; r<=14; r+=1){
for(int s=0; s<4; s+=1){
deck[index++] = new Card(Card.SUITS[s], Card.RANKS[r]);
}
}
/* shuffle the deck */
Random rnd = new Random();
Card swap;
for(int i = deck.length-1; i>=0; i=i-1){
int pos = rnd.nextInt(i+1);
swap = deck[pos];
deck[pos] = deck[i];
deck[i] = swap;
}
/* players in the game */
Player[] players = new Player[3];
players[0] = new BadPlayer( Arrays.copyOfRange(deck, 0, 5) );
System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 0, 5)));
players[1] = new BadPlayer( Arrays.copyOfRange(deck, 5, 10) );
System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 5, 10)));
players[2] = new BadPlayer( Arrays.copyOfRange(deck, 10, 15) );
System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 10, 15)));
/* discard and draw piles */
DiscardPile discardPile = new DiscardPile();
Stack drawPile = new Stack();
for(int i=15; i
drawPile.push(deck[i]);
}
System.out.println("draw pile is : " + Arrays.toString( Arrays.copyOfRange(deck, 15, deck.length) ));
deck = null;
boolean win = false;
int player = -1; // start game play with player 0
ArrayList people = new ArrayList(Arrays.asList(players));
discardPile.add( drawPile.pop() );
while( !win ){
player = (player + 1) % players.length;
System.out.println("player " + player);
System.out.println("draw pile : " + drawPile.peek() );
System.out.println("discard pile : " + discardPile.top() );
win = people.get(player).play(discardPile, drawPile, people);
System.out.println("draw pile : " + drawPile.peek() );
System.out.println("discard pile : " + discardPile.top() );
}
System.out.println("winner is player " + player);
}

DISCARDPILE.JAVA

import java.util.Stack;
public class DiscardPile{
/* the actual discard pile */
protected Stack cards = new Stack();
/* the top card on the discard pile */
public Card top(){
if(cards.isEmpty()){
return new Card("None","None");
}
return cards.peek();
}
/* add a card to the discard pile */
public void add(Card card){
cards.push(card);
}

PLAYER.JAVA

import java.util.ArrayList;
import java.util.Stack;
public abstract class Player{
protected ArrayList hand;
public int getSizeOfHand(){
return hand.size();
}
/* play a card */
public abstract boolean play(DiscardPile discardPile,
Stack drawPile,
ArrayList players);
// return true if player wins game by playing last card
// returns false otherwise
// side effects: plays a card to top of discard Pile, possibly taking zero
// or more cards from the top of the drawPile
// card played must be valid card
}

}

}

}

}

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

More Books

Students also viewed these Databases questions

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago