write java classes for each strategy below.for the game to play make a new insta
ID: 3867944 • Letter: W
Question
write java classes for each strategy below.for the game to play make a new instant of each class in the Crazy8Game given at the end:
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 : make each strategy a class which extends the player class. add alot of comment to explain your work.
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.
crazy 8 rules :https://www.thespruce.com/crazy-eights-rules-card-game-411132
crazy8Game code:
import java.util.Arrays;
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); } }Explanation / Answer
Code to copy:
Crazy8Game.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.Random;
public class Crazy8Game
{
public static int negate = 1;
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[5];
players[0] = new HamperLeader( 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)));
players[3] = new BadPlayer( Arrays.copyOfRange(deck, 15, 20) );
System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 15, 20)));
players[4] = new BadPlayer( Arrays.copyOfRange(deck, 20, 25) );
System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 20,35 )));
/* 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;
// start game play with player 0
int player = -1;
ArrayList<Player> people = new ArrayList<Player>(Arrays.asList(players));
discardPile.add( drawPile.pop() );
while( !win )
{
//handles reverse order, prevents out of bounds
if ((player <= 0) && (negate==(-1))) {
player = players.length-1;
} else if ((player >= players.length-1) && (negate == 1)) {
player = 0;
}else {
player = player + negate;
}
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() );
// Reverses order if 7 Card is played
if (discardPile.top().getRank() == 7){
negate *= -1;
System.out.println("----------------");
System.out.println("DIRECTION SWITCH");
System.out.println("----------------");
}
// Skips next player if 4 Card is played
else if(discardPile.top().getRank() == 4){
player = (player + negate);
System.out.println("----------------");
System.out.println("TURN SKIPPED");
System.out.println("----------------");
}
}
System.out.println("winner is player " + player);
}
public static int getDirection() {
return negate;
}
}
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);
}
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;
}
}
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;
}
}
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;
}
}
Note:
Here i am posting only required classes. though i have complete running code but i unable to post the complete code because of the space isuue. If you need any other class just write in the comment box.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.