write a java code:
You will implement several new player classes that implement the two strategies below for players and modify Crazy8s to visualize game play .You must ensure that the games are played properly.The following description of the game is a slight modification of the rules found at https://www.thespruce.com/crazy-eights-rules-card-game-411132
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.)
crazy8s code:
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); | |
} | |
} |
Expert Answer
This is the crazyeight class for the crazyEight in java.Hence, here is the program.Thankyou.
eight.java
import java.awt.*;
import hsa.Console;
public class eights
{
static Console con;
static int[] decks, playa, pc;
static int packs, suits;
static boolean empDeck;
public static void main (String[] args)
{
con = new Console (30, 100, “Crazy Eights”);
introduction ();
char palying;
do
{
con.setFont (new Font (“Arial”, java.awt.Font.PLAIN, 14));
playGame ();
con.setCursor (30, 32);
palying = con.getChar ();
}
while (palying == ‘qq’ || palying == ‘Y’);
con.close ();
}
public static void playGame ()
{
gettingDecks ();
playa = makingHands ();
pc = makingHands ();
packs = gettingCards ();
while (!mtHands (1) && !mtHands (2) && !empDeck)
{
rendered ();
playaPlay ();
computerPlaya ();
}
rendered ();
dispMssg (“Game over. Play again [qq/n]? _”);
con.clearRect (350, 215, 101, 151);
con.setFont (new Font (“Arial”, java.awt.Font.BOLD, 24));
if (mtHands (1))
{
con.drawString (“YOU WIN!”, con.getWidth () / 2 – 50, con.getHeight () / 2 + 10);
}
else if (mtHands (2))
{
con.drawString (“YOU LOSE!”, con.getWidth () / 2 – 50, con.getHeight () / 2 + 10);
}
else
{
con.drawString (“IT’S A TIE!”, con.getWidth () / 2 – 50, con.getHeight () / 2 + 10);
}
}
public static void playaPlay ()
{
int io = 1, qq = 0;
int[] cards = new int [52];
con.setCursor (29, 1);
con.print (“”, 100);
for (int uu = 0 ; uu < playa.length ; uu++)
{
if (playa [uu] > 0)
{
if (legalCards (playa [uu]))
{
con.setCursor (28, 6 + qq * 5);
con.print (io);
cards [io] = uu;
io++;
}
qq++;
}
}
if (io > 1)
{
dispMssg (“Enter a card number to play: “);
con.setCursor (30, 32);
int playingCards = con.readInt ();
while (playingCards < 1 || playingCards + 1 > io)
{
dispMssg (“Invalid number. Enter a card number to play: “);
con.setCursor (30, 48);
playingCards = con.readInt ();
}
packs = playa [cards [playingCards]];
playa [cards [playingCards]] = 0;
if ((packs) % 13 == 8)
{
dispMssg (“Enter the first letter of the suits you want to change to: “);
con.setCursor (30, 61);
String newSuits = con.readLine ();
if (newSuits.equalsIgnoreCase (“s”))
{
packs = 8;
}
else if (newSuits.equalsIgnoreCase (“h”))
{
packs = 21;
}
else if (newSuits.equalsIgnoreCase (“d”))
{
packs = 34;
}
else if (newSuits.equalsIgnoreCase (“con”))
{
packs = 47;
}
}
}
else
{
dispMssg (“You can’t play any of your cards. Press any key to draw a card.”);
con.getChar ();
boolean fillings = false;
int uu = 0;
while (!fillings && uu < playa.length)
{
if (playa [uu] == 0)
{
playa [uu] = gettingCards ();
if (playa [uu] == 0)
{ //decks ran out, quit playGame
empDeck = true;
}
fillings = true;
}
uu++;
}
}
rendered ();
}
public static void computerPlaya ()
{
if (!mtHands (1))
{
dispMssg (“Press any key to let pc play…”);
con.getChar ();
int eighty = -1;
boolean played = false;
for (int uu = 0 ; uu < pc.length && !played ; uu++)
{
if (pc [uu] > 0)
{
if (legalCards (pc [uu]))
{
if ((pc [uu]) % 13 == 8)
{
eighty = uu;
}
else
{
packs = pc [uu];
pc [uu] = 0;
played = true;
}
}
}
}
if (!played)
{
if (eighty != -1)
{
packs = pc [eighty];
pc [eighty] = 0;
int spade = 0, heart = 0, diamond = 0, club = 0;
for (int uu = 0 ; uu < pc.length ; uu++)
{
if (pc [uu] > 0)
{
int suitNob = (pc [uu]) / 13;
if (suitNob == 0) // spade
spade++;
else if (suitNob == 1)
heart++;
else if (suitNob == 2)
diamond++;
else if (suitNob == 3)
club++;
}
}
if (spade * 3 > heart + diamond + club)
packs = 8;
else if (heart * 3 > spade + diamond + club)
packs = 21;
else if (diamond * 3 > spade + heart + club)
packs = 34;
else if (club * 3 > spade + heart + diamond)
packs = 47;
}
else
{
boolean fillings = false;
int uu = 0;
while (!fillings && uu < pc.length)
{
if (pc [uu] == 0)
{
pc [uu] = gettingCards ();
if (pc [uu] == 0)
{
empDeck = true;
}
fillings = true;
}
uu++;
}
}
}
}
rendered ();
}
public static void dispMssg (String mssg)
{
con.setCursor (30, 3);
con.print (mssg, 96);
}
public static boolean legalCards (int card)
{
if ((card) % 13 == 8)
{
return true;
}
else if ((card) / 13 == (packs) / 13)
{
return true;
}
else if ((card) % 13 == (packs) % 13)
{
return true;
}
return false;
}
public static boolean mtHands (int whHand)
{
int[] hand;
if (whHand == 1)
hand = playa;
else
hand = pc;
for (int io = 0 ; io < hand.length ; io++)
{
if (hand [io] > 0)
return false;
}
return true;
}
public static void gettingDecks ()
{
decks = new int [52];
for (int uu = 0 ; uu < decks.length ; uu++)
{
decks [uu] = uu + 1;
}
for (int io = 0 ; io < decks.length * 2 ; io++)
{
int ranking = (int) (Math.random () * decks.length);
int ranking1 = (int) (Math.random () * decks.length);
int tempo = decks [ranking];
decks [ranking] = decks [ranking1];
decks [ranking1] = tempo;
}
empDeck = false;
}
public static int[] makingHands ()
{
int[] hand = new int [52];
for (int uu = 0 ; uu < 5 ; uu++)
{
hand [uu] = gettingCards ();
}
return hand;
}
public static int gettingCards ()
{
int indices = 0, val = 0;
while (indices < decks.length && decks [indices] == 0)
indices++;
if (decks.length > indices)
{
val = decks [indices];
decks [indices] = 0;
}
return val;
}
public static String shownValue (int card)
{
int val = card % 13;
if (val == 0)
return “A”;
else if (val <= 9)
return “” + val;
else if (val == 10)
return “J”;
else if (val == 11)
return “Q”;
else if (val == 12)
return “K”;
return “X”;
}
public static String showingSuits (int card)
{
int suitNob = card / 13;
if (suitNob == 0)
return “u2660”;
else if (suitNob == 1)
return “u2665”;
else if (suitNob == 2)
return “u2666”;
else if (suitNob == 3)
return “u2663”;
return “X”;
}
public static void delCard (int io, int qq)
{
con.setColor (Color.white);
con.fillRoundRect (io – 5, qq – 5, 110, 160, 10, 10);
}
public static void withdrawCard (int val, int io, int qq, boolean hide)
{
String rank = shownValue (val);
String suits = showingSuits (val);
if (hide)
{
con.setColor (new Color (190, 213, 226));
con.fillRoundRect (io, qq, 100, 150, 10, 10);
con.setColor (Color.black);
con.drawRoundRect (io, qq, 100, 150, 10, 10);
}
else
{
con.setColor (Color.white);
con.fillRoundRect (io, qq, 100, 150, 10, 10);
con.setColor (Color.black);
con.drawRoundRect (io, qq, 100, 150, 10, 10);
if (suits.equals (“u2665”) || suits.equals (“u2666”))
con.setColor (Color.red);
con.drawString (rank + suits, io + 10, qq + 15);
con.drawString (rank + suits, io + 75, qq + 140);
con.fillRoundRect (io + 20, qq + 30, 60, 90, 10, 10);
con.setColor (Color.black);
}
}
public static void introduction ()
{
con.clear ();
con.setFont (new Font (“Arial”, java.awt.Font.BOLD, 48));
con.drawString (“CRAZY EIGHTS”, con.getWidth () / 2 – 180, con.getHeight () / 2 – 10);
con.setFont (new Font (“Arial”, java.awt.Font.PLAIN, 14));
con.drawString (“Press any key to begin.”, con.getWidth () / 2 – 50, con.getHeight () / 2 + 50);
con.getChar ();
}
public static void rendered ()
{
con.clear ();
int io = 0;
for (int uu = 0 ; uu < pc.length ; uu++)
{
if (pc [uu] != 0)
{
withdrawCard (pc [uu], 670 – io * 40, 30, true);
io++;
}
}
io = 0;
for (int uu = 0 ; uu < playa.length ; uu++)
{
if (playa [uu] != 0)
{
withdrawCard (playa [uu], 30 + io * 40, 400, false);
io++;
}
}
withdrawCard (packs, 350, 215, false);
}
}