Question & Answer: orrectly. Show correct code and the result of compiled code. All the classes are below……

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<Card>(Arrays.asList(cards));}
/* play a card */
public boolean play(DiscardPile discardPile,
Stack<Card> drawPile,
ArrayList<Player> 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<Card>{
/* 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<String, Integer> 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<String,Integer>(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
* <p>
* 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<Card> drawPile = new Stack<Card>();
for(int i=15; i<deck.length; 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<Player> people = new ArrayList<Player>(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<Card> cards = new Stack<Card>();
/* 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<Card> hand;
public int getSizeOfHand(){
return hand.size();
}
/* play a card */
public abstract boolean play(DiscardPile discardPile,
Stack<Card> drawPile,
ArrayList<Player> 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
}

Expert Answer

 

Crazy8Game.java
——————————————-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

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 */
ArrayList<Player> players = new ArrayList<>();
players.add(new BadPlayer(Arrays.copyOfRange(deck, 0, 5)));
System.out.println(“Player 0’s hand: ” + players.get(0));
players.add(new BadPlayer(Arrays.copyOfRange(deck, 5, 10)));
System.out.println(“Player 1’s hand: ” + players.get(1));
players.add(new BadPlayer(Arrays.copyOfRange(deck, 10, 15)));
System.out.println(“Player 2’s hand: ” + players.get(2));

/* discard and draw piles */
DiscardPile discardPile = new DiscardPile();
Stack<Card> drawPile = new Stack<>();
for (int i = 15; i < deck.length; i++) {
drawPile.push(deck[i]);
}

boolean win = false;
int player = -1;    // start game with player 0

discardPile.add(drawPile.pop());

System.out.println(“Draw pile is: ” + drawPile);
System.out.println(“Discard pile is: ” + discardPile);

while (!win) {
player = (player + 1) % players.size();
System.out.println(“nIt is player ” + player + “‘s turn!”);
System.out.println(“Their hand: ” + players.get(player));
Card topDraw = drawPile.isEmpty() ? new Card(“None”, “None”) : drawPile.peek();
System.out.println(“Top of draw pile: ” + topDraw);
System.out.println(“Top of discard pile: ” + discardPile.top());

win = players.get(player).play(discardPile, drawPile, players);

// topDraw = drawPile.isEmpty() ? new Card(“None”, “None”) : drawPile.peek();
// System.out.println(“Top of draw pile: ” + topDraw);
// System.out.println(“Top of discard pile: ” + discardPile.top());
}
System.out.println(“n——————–nWinner is player ” + player + “!”);
}
}
————————————————————————————————
DiscardPile.java
—————————————————–
import java.util.Objects;
import java.util.Stack;

public class DiscardPile {
/* the actual discard pile */
protected Stack<Card> cards = new Stack<Card>();

/* 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) {
if (Objects.equals(card.rank, top().rank) || Objects.equals(card.suit, top().suit) ||
Objects.equals(card.rank, Card.RANKS[8]) || cards.isEmpty()) {
System.out.println(“Added card to discard pile: ” + card);
cards.push(card);
} else {
System.out.println(“ERROR: INVALID CARD ADDED TO DISCARD PILE”);
System.out.println(“ERROR: Discarded card: ” + card);
System.out.println(“ERROR: Card on top of deck: ” + top());
}
}

@Override
public String toString() {
return String.valueOf(cards);
}
}
———————————————————————-
Card.java
————————–
import java.util.HashMap;

public class Card implements Comparable<Card> {

/* 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<String, Integer> rankValue;

public Card(String suit, String rank) {
// assume input is valid!
this.suit = suit;
this.rank = rank;
this.rankValue = new HashMap<String, Integer>(15);
for (int r = 2; r < RANKS.length; r += 1) {
this.rankValue.put(RANKS[r], r);
}
}

public int getRank() {
if (this.rank.equals(RANKS[0])) { // “no card”
return -1;
}
return rankValue.get(this.rank);
}

public String getRankString() {
return this.rank;
}

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”;
}

}
————————————————————–
Player.java
————————————————–
import java.util.ArrayList;
import java.util.Stack;

public abstract class Player {
protected ArrayList<Card> hand;

public int getSizeOfHand() {
return hand.size();
}

/* play a card */
// 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
public abstract boolean play(DiscardPile discardPile, Stack<Card> drawPile, ArrayList<Player> players);

public Card pickupCard(Stack<Card> drawPile) {
if (drawPile.isEmpty()) {
System.out.println(“ERROR: TRIED TO PICK UP FROM EMPTY DRAW PILE”);
return new Card(“None”, “None”);
}
Card card = drawPile.pop();
hand.add(card);
System.out.println(“Picked up card: ” + card);
return card;
}

@Override
public String toString() {
return String.valueOf(hand);
}
}
—————————————————————————–
BadPlayer.java
——————————-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
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<Card> drawPile, ArrayList<Player> players) {

// go through all cards in the hand, and play the first valid one
for (int i = 0; i < hand.size(); i++) {
if (Objects.equals(hand.get(i).rank, discardPile.top().rank) ||
Objects.equals(hand.get(i).suit, discardPile.top().suit) ||
Objects.equals(hand.get(i).rank, Card.RANKS[8])) {
discardPile.add(this.hand.remove(i));
return this.hand.size() == 0;
}
}

// pick up a card from the draw pile, if it isn’t empty
if (!drawPile.isEmpty()) {
pickupCard(drawPile);
return false;
}

// nothing left to do, so pass
System.out.println(“Passing!”);
return this.hand.size() == 0;
}
}

Still stressed from student homework?
Get quality assistance from academic writers!