Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

complete the code below. this class is for crazy8 game. mport java.util.ArrayLis

ID: 3868180 • Letter: C

Question

complete the code below. this class is for crazy8 game.

mport java.util.ArrayList;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

class Card game extends Player {

//write methods that describe:
//1-players should always be aware of any eights the player are holding.

// 2-This player will their eights until late in the game, but won't hold on to them too long.

//3-Once any opponent goes down to one card left in hand , it's time to play your eight.

//4-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.)

Explanation / Answer

All the required classes which are extending the playe class for the Crazy8Game are given below:

DiscardHighPoints.java:

import java.util.Arrays;

import java.util.ArrayList;

import java.util.Stack;

public class DiscardHighPoints extends Player
{
public DiscardHighPoints(Card[] cards)
{
  this.hand = new ArrayList<Card>(Arrays.asList(cards));
}

/* play a card */
public boolean play(DiscardPile discardPile,

   Stack<Card> drawPile,

   ArrayList<Player> players)
{
  // Returns highest card and sorts deck
  Card highestPointCard = this.highest_card();
  
  // Returns top card in discard Pile
  Card previousCard = discardPile.top();             

  // Checks if previous card was a 2
  if (previousCard.getRank() == 2 && previousCard.rounds == 1){

   int drawCount = 0;
   while (drawCount < 2){
    if(!drawPile.isEmpty()){
     hand.add(drawPile.pop());
    }
    drawCount++;
   }

   System.out.println(" Picks up 2 cards from draw pile if draw pile isn't empty.");
  }
  else if (previousCard.getRank() == 4 && previousCard.rounds == 1){
   System.out.println(" Skipped my turn.");
  }

  // Gets rid of highest card if it is an 8 and sets suit to next highest card
  else if (highestPointCard.getRank() == 8){
   discardPile.add(this.hand.remove(0));
   highestPointCard = this.highest_card();
   Card suitChange = new Card(highestPointCard.getSuit(), "8");
   discardPile.add(suitChange);
   System.out.println(" Gets rid of highest card if it is an 8, and sets suit to next highest card.");
  }

  // Gets Rid of highest card if suits are the same
  else if (highestPointCard.getSuit().equals(previousCard.getSuit()) &&
    !(highestPointCard.getRank() == 8)){
   discardPile.add(this.hand.remove(0));
   System.out.println(" Plays highest point card.");
  }

  // Checks if we have any playable card
  else if (canPlay(previousCard)){
   
   // Plays highest card with same suit
   if (highest_card_counter() > 0)
   {              
    int cardAdded = 0;
    for (int i = 0; i < hand.size() - 1; i++){
     if ((hand.get(i+1).getSuit().equals(previousCard.getSuit()) ||
       hand.get(i+1).getRank() == previousCard.getRank()) &&
       (cardPoints(hand.get(i+1)) == cardPoints(highest_card()))){

      discardPile.add(hand.get(i+1));

      hand.remove(hand.get(i+1));

      cardAdded = cardAdded + 1;

      break;

     }

    }

    if (cardAdded != 0)
    {
     System.out.println(" Plays highest point card with the same suit as prevoius.");
    }

    // Plays card with same suit as highest card or cards
    if (cardAdded == 0){
     for (int i = 0; i < highest_card_counter() + 1; i++){
      for (Card c: hand){
       if (c.getSuit().equals(hand.get(i).getSuit()) && c.getRank() == previousCard.getRank()){
        discardPile.add(c);
        hand.remove(c);
        cardAdded++;
        break;
       }
      }

      if (cardAdded != 0){
       System.out.println(" Plays card with same suit as highest card.");
       break;
      }
     }
    }

    // Plays any valid card
    if (cardAdded == 0){
     for (Card c: hand){
      if (c.getSuit().equals(previousCard.getSuit()) || c.getRank() == previousCard.getRank()){
       discardPile.add(c);
       hand.remove(c);
       System.out.println(" Plays any valid card.");
       break;
      }
     }
    }
   }

   else{
    int cardAdded = 0;
    
    // plays card with same suit as highest card
    for (Card c: hand)
    {                        
     if (c.getSuit().equals(highestPointCard.getSuit()) && c.getRank() == previousCard.getRank()){
      discardPile.add(c);
      hand.remove(c);
      cardAdded++;
      System.out.println(" Plays card with same suit as highest card.");
      break;
     }
    }
    
     // plays any valid card
    if (cardAdded == 0){                      
     for (Card c: hand){
      if (c.getSuit().equals(previousCard.getSuit()) ||
        c.getRank() == previousCard.getRank()){

       discardPile.add(c);
       hand.remove(c);
       System.out.println(" Plays any valid card.");
       break;
      }
     }
    }
   }
  }


  // No playable card so we now have to pick up from discard pile
  else{

   // pick from pile
   if (!drawPile.isEmpty()){
    boolean checker = false;
    Card fromDraw;
    while (!checker && !drawPile.isEmpty()){
     fromDraw = drawPile.pop();
     if ((fromDraw.getSuit().equals(previousCard.getSuit())) ||
       (fromDraw.getRank() == previousCard.getRank())){

      discardPile.add(fromDraw);
      System.out.println(" Plays a valid card from draw pile.");
      checker = true;
     }
     else{
      hand.add(fromDraw);
      System.out.println(" Card from draw pile is not valid, draw again.");
     }
    }
   }
  }
  if( this.hand.size() == 0 ){return true;}
  return false;
}

// returns highest value card and sorts deck
private Card highest_card(){            

  boolean checker = true;
  Card tempCard;
  while (checker){
   int which_card;
   checker = false;
   for (int i = 0; i < hand.size()-1; i++){
    which_card = hand.get(i).compareTo(hand.get(i+1));

    // Sorts in descending order
    if (which_card == -1){                 
     tempCard = hand.get(i);
     hand.set(i, hand.get(i+1));
     hand.set(i+1, tempCard);
     checker = true;
    }
   }
  }
  return hand.get(0);
}
private int highest_card_counter(){
  int counter = 0;
  
   // sort deck
  highest_card();   
  for (int i = 0; i < hand.size() - 1; i++)
  {
   if (cardPoints(highest_card()) == cardPoints(hand.get(i + 1))) {
    counter = counter +1;
   }
   else{
    break;
   }
  }
  return counter;
}
private boolean canPlay(Card discardTop){
  for (Card c: hand){
   if (c.getRank() == discardTop.getRank() || c.getSuit().equals(discardTop.getSuit())){
    return true;
   }
  }
  return false;
}
private static int cardPoints(Card c){

  if (c.getRank() == 8){
   return 50;
  }
  else if (c.getRank() == 2 || c.getRank() == 4){
   return 25;
  }
  else if (c.getRank() == 7){
   return 20;
  }

  else if (c.getRank() >= 11 && c.getRank() <=13){
   return 10;
  }
  else if (c.getRank() == 14){
   return 1;
  }

  else
  {
   return c.getRank();
  }
}
}

ExtraCards.java:


import java.util.Arrays;
import java.util.ArrayList;
import java.util.Stack;
public class ExtraCards extends Player{

//constructor
public ExtraCards(Card[] cards) {
  this.hand = new ArrayList<Card>(Arrays.asList(cards));
}

//play method
public boolean play(
   DiscardPile discardPile,
   Stack<Card> drawPile,
   ArrayList<Player> players
   )
{
  System.out.println("--- EXTRACARDS ---");
  System.out.println("Hand before play: " + hand);
  boolean played = false;
  
  // Checks if previous card was a 2
  if ((discardPile.top().getRank() == 2) && (discardPile.top().rounds == 1)){
   pickupCard(drawPile);
   pickupCard(drawPile);
  }

  int direction = Crazy8Game.getDirection();
  int next = nextPlayer(players, direction);

  //if someone is about to win the game
  if (players.get(next).getSizeOfHand() == 1) {
   System.out.println("Player is going to win: " + next);
   int power = havePower();

   //if power card is available, play the card
   if (power > 0) {
    Card card = hand.get(power);
    if (canPlay(discardPile, card)) {
     System.out.println("has power: " + hand.get(power));
     System.out.println(card);
     discardPile.add(card);
     hand.remove(card);
     played = true;
     System.out.println("Stopped player "+ next +" from winning with a " + card);
    }
   } else {

    System.out.println("no power to play.");
    
    //if no power card, keep picking up until one is available
    boolean check = false;
    while ((!check) && (!drawPile.isEmpty())) {

     System.out.println("picking up until has power");
     pickupCard(drawPile);
     if (isPower(topCard())) {
      System.out.println("found power: " + topCard());
      playCard(discardPile, hand.size());
      played = true;
      break;
     }
    }
   }

  //if nobody is going to win, pick up one card and if its a power card, play it
  } else {

   System.out.println("nobody is going to win.");
   pickupCard(drawPile);
   if (isPower(topCard())) {
    playCard(discardPile, hand.size());
    played = true;
   }
  }

  //if no power cards come up, play a valid card
  for (Card c : hand) {
   if ((canPlay(discardPile, c)) && (!played)) {
    discardPile.add(c);
    hand.remove(c);
    played = true;
    break;
   }
  }
  //if cant play

  if ((!played) && (!drawPile.isEmpty())) {
   System.out.println("Can't play.");
   pickupCard(drawPile);
  }

  System.out.println("Hand after play: " + hand);
  
  //win check
  if (hand.size() == 0) {
   return true;
  }
  return false;
}

//check to see if the card is valid to play
public boolean canPlay(DiscardPile discardPile, Card card) {

  Card lastCard = discardPile.top();
  int lastRank = lastCard.getRank();
  String lastSuit = lastCard.getSuit();
  if (((card.getRank() == lastRank) || (card.getSuit().equals(lastSuit) || (card.getRank() == 8)))&&(hand.size() > 0)) {
   return true;
  }
  return false;
}

//checks to see if hand has power code
public int havePower() {

  for (int i = 0; i < hand.size(); i++) {
   if (isPower(hand.get(i))) {
    return i;
   }
  }
  return -1;
}

//checks if card is power
public boolean isPower(Card card)
{

  int rank = card.getRank();
  if ((rank == 2) || (rank == 4) || (rank == 7) || (rank == 8)) {
   return true;
  }
  return false;
}
public void playCard(DiscardPile discardPile, int index)
{
  System.out.println("playcard method" + hand);
  Card card = hand.get(index-1);
  discardPile.add(card);
  hand.remove(card);
}

public boolean pickupCard(Stack<Card> drawPile)
{
  if (!drawPile.isEmpty())
  {
   hand.add(drawPile.pop());
   return true;
  }
  return false;
}
public Card topCard()
{
  return this.hand.get(hand.size()-1);
}

public int nextPlayer(ArrayList<Player> players, int direction)
{

  int next;
  int me = 0;

  for (int i = 0; i < players.size(); i++)
  {
   if (players.get(i).equals(this))
   {
    me = i;
    break;
   }
  }

  if ((me >= players.size()-1) && (direction == 1)) {

   next = 0;
  }
  else if ((me <= 0) && (direction == -1)) {
   next = players.size()-1;
  }
  else
  {
   next = me + direction;
  }
  return next;
}
}

HamperLeader.java:

import java.util.ArrayList;
import java.util.Stack;
import java.util.Arrays;

public class HamperLeader extends Player{
boolean hasNotPlayed =true ;
public HamperLeader(Card[] cards){
  this.hand = new ArrayList<Card>(Arrays.asList(cards));
}
public boolean play(
   DiscardPile discardPile,
   Stack<Card> drawPile,
   ArrayList<Player> players){
  System.out.println("----------------");
  System.out.println(hand);
  System.out.println("----------------");
  
  //Gets position in list of players
  int position = getPosition(players);

  //Checks if a 2 was played,
  if (discardPile.top().getRank() == 2){
   if(!drawPile.isEmpty()){
    hand.add(drawPile.pop());
   }
   if(!drawPile.isEmpty()){
    hand.add(drawPile.pop());
   }
   System.out.println("----------------");
   System.out.println("HAMPER PICKED UP 2");
   System.out.println("----------------");
   return false;
  }
  do{

   //IF the next person is leader, checks for 2, checks for 4
   if(nextIsLeader(players,position)){
    System.out.println("----------------");
    System.out.println("LEADER IN FRONT");
    System.out.println("----------------");
    for(int i =0; i<hand.size(); ++i){
     if(hand.get(i).getRank() == 2 && playable(hand.get(i),discardPile)){
      discardPile.add(this.hand.remove(i));
      hasNotPlayed = false;
      System.out.println("----------------");
      System.out.println("HAMPER PLAYED A 2");
      System.out.println("----------------");
      break;
     }
    }
    if(hasNotPlayed){
     for(int i =0; i<hand.size(); ++i){
      if(hand.get(i).getRank() == 4 && playable(hand.get(i),discardPile)){
       discardPile.add(this.hand.remove(i));
       hasNotPlayed = false;
       System.out.println("----------------");
       System.out.println("HAMPER PLAYED A 4");
       System.out.println("----------------");
       break;
      }
     }
    }
   }

   //If the previous person is leader plays 7
   else if(previousIsLeader(players,position)){

    System.out.println("----------------");
    System.out.println("LEADER BEHIND");
    System.out.println("----------------");
    for(int i =0; i<hand.size(); ++i){

     if(hand.get(i).getRank() == 7 && playable(hand.get(i),discardPile)){
      discardPile.add(this.hand.remove(i));
      hasNotPlayed = false;
      System.out.println("----------------");

      System.out.println("HAMPER PLAYED A 7");

      System.out.println("----------------");

      break;

     }

    }

   }

   //If the leader is not adjacent, plays a random card

   if(hasNotPlayed){

    for(int i =0; i<hand.size(); ++i){

     if(hand.get(i).getRank() == 8){

      discardPile.add(this.hand.remove(i));

      System.out.println("----------------");

      System.out.println("HAMPER PLAYED AN 8");

      System.out.println("----------------");

      hasNotPlayed = false;

      break;

     }

     else if(playable(hand.get(i),discardPile)){

      discardPile.add(this.hand.remove(i));

      System.out.println("----------------");

      System.out.println("HAMPER PLAYED A RANDOM CARD");

      System.out.println("----------------");

      hasNotPlayed = false;

      break;

     }

    }

   }

   //Picks up a card if can't play

   if (hasNotPlayed && !drawPile.isEmpty()){

    System.out.println("----------------");

    System.out.println("HAMPER PICKED UP A CARD");

    System.out.println("----------------");

    Card pickup;

    pickup = drawPile.pop();

    if(playable(pickup,discardPile)){

     discardPile.add(pickup);

     System.out.println("----------------");

     System.out.println("HAMPER PLAYED A RANDOM CARD");

     System.out.println("----------------");

     hasNotPlayed = false;

     break;

    }

    else{

     hand.add(pickup);

    }

   }

   //Still hasn't played a card

   if(hasNotPlayed){

    System.out.println("----------------");

    System.out.println("HAMPER PASSES");

    System.out.println("----------------");

    hasNotPlayed = false;

   }

  }while(hasNotPlayed);

  if(this.hand.size()==0){

   return true;

  }

  return false;

}

// returns true if the next player is in the lead

private boolean nextIsLeader(ArrayList<Player> players, int myPosition){

  int nextPosition = 0;

  if(myPosition==players.size()-1){

   nextPosition = 0;

  }

  else{

   nextPosition = myPosition + 1;

  }

  for(int i=0;i <players.size(); ++i){

   if (players.get(i).getSizeOfHand()>players.get(nextPosition).getSizeOfHand()){

    return false;

   }

  }

  return true;

}

// returns true if the previous player is in the lead

public boolean previousIsLeader(ArrayList<Player> players, int myPosition){

  int previousPosition = 0;

  if(myPosition==0){

   previousPosition = players.size()-1;

  }

  else{

   previousPosition = myPosition - 1;

  }

  for(int i=0;i <players.size(); ++i){

   if (players.get(i).getSizeOfHand()>players.get(previousPosition).getSizeOfHand()){

    return false;

   }

  }

  return true;

}

public int getPosition(ArrayList<Player> players){

  int myPosition = 0;

  while(!(players.get(myPosition).equals(this))){

   ++myPosition;

  }

  return myPosition;

}

public boolean playable(Card cardToPlay,DiscardPile discardPile){

  if (cardToPlay.getRank() == discardPile.top().getRank() || cardToPlay.getSuit().equals(discardPile.top().getSuit())){

   return true;

  }

  return false;

}

}

MindTheEights.java:


import java.util.Arrays;

import java.util.ArrayList;

import java.util.Stack;

public class MindTheEights extends Player
{
public MindTheEights (Card[] cards)
{
  this.hand = new ArrayList<Card>(Arrays.asList(cards));
}

/* play a card */
public boolean play(DiscardPile discardPile,  
   Stack<Card> drawPile,
   ArrayList<Player> players)
{
  // Returns top card in discard Pile
  Card previousCard = discardPile.top();          
  int myEights = 0;
  int smallestHand = 52;
  
  // Finds My Amount of Eights
  for (Card c: hand)
  {
   if (c.getRank() == 8)
   {
    myEights++;
   }
  }

  // Finds Smallest Hand
  for (Player p: players)
  {
   if (p.getSizeOfHand() < smallestHand)
   {
    smallestHand = p.getSizeOfHand();
   }
  }

  // Decide what to do
  if (myEights == smallestHand && myEights != 0)
  {
   for (Card c: hand)
   {
    if (c.getRank() == 8)
    {
     discardPile.add(c);
     hand.remove(c);
    }
   }
  }
  else if (canPlay(previousCard))
  {
   for (Card c: hand)
   {
    if (c.getRank() == previousCard.getRank() ||
      c.getSuit().equals(previousCard.getSuit())){
     discardPile.add(c);
     hand.remove(c);
    }
   }
  }
  else{
   if (!drawPile.isEmpty())
   {
    boolean checker = false;
    Card fromDraw;
    while (!checker)
    {
     fromDraw = drawPile.pop();
     if ((fromDraw.getSuit().equals(previousCard.getSuit())) ||
       (fromDraw.getRank() == previousCard.getRank())){
      discardPile.add(fromDraw);
      checker = true;
     }
     else{
      hand.add(fromDraw);
     }
    }
   }
  }
  if( this.hand.size() == 0 ){return true;}
  return false;
}
private boolean canPlay(Card discardTop)
{
  for (Card c: hand)
  {
   if (c.getRank() == discardTop.getRank() || c.getSuit().equals(discardTop.getSuit())){
    return true;
   }
  }
  return false;
}
}

RandomPlayer.java:

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Stack;

public class RandomPlayer extends Player
{
public RandomPlayer(Card[] cards)
{
  this.hand = new ArrayList<Card>(Arrays.asList(cards));
}

/* play a card */

public boolean play(DiscardPile       discardPile,
   Stack<Card>       drawPile,
   ArrayList<Player> players)
{
  //picks up cards if last played was 2
  if (discardPile.top().getRank() == 2)
  {
   getCard(drawPile);
   getCard(drawPile);
  }
  Card previous = discardPile.top();

  int previousRank = previous.getRank();

  String previousSuit = previous.getSuit();

  boolean played = false;

  //loops through hand, plays the first card that is valid
  for (Card c : hand)
  {
   if (hand.size() > 0)
   {
    if ((c.getRank() == previousRank) || (c.getSuit().equals(previousSuit) || (c.getRank() == 8))) {

     discardPile.add(c);

     hand.remove(c);

     System.out.println("Played: " + c);

     played = true;

     break;

    }

   }

  }

  //if no cards were played then pick one card up
  if (!played)
  {
   getCard(drawPile);
   played = true;
  }

  //if hand is empty, return true
  if (hand.size() == 0)
  {
   return true;
  }
  return false;
}
public boolean getCard(Stack<Card> drawPile)
{
  if (!drawPile.isEmpty())
  {
   hand.add(drawPile.pop());
   return true;
  }
  return false;
}
}

Player.java:

import java.util.ArrayList;
import java.util.Stack;

// Create the class called player
public abstract class Player
{
// declare the local variable.
public int points;

// declare a local array of card type.
protected ArrayList<Card> hand;

// method to get the size of the arraylist.
public int getSizeOfHand()
{
  return hand.size();
}

// Abstact method.
public abstract boolean play(DiscardPile discardPile,
   Stack<Card> drawPile, ArrayList<Player> players);

}

Note: i have all the classes of the Crazy8Game, but unable to post all the classes therefore, posting only the required classes. if you need any class just write in the comment box.