need help in this question: write a java program for the crazy 8s game. the desc
ID: 3867310 • Letter: N
Question
need help in this question: write a java program for the crazy 8s game. the description of the game is bellow. the code should contain the 4 strategies listed below as its method.
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.
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.
Explanation / Answer
eights.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package crazyeights;
/**
*
* @author Akshay Bisht
*/
import java.awt.*;
import hsa.Console;
public class eights
{
static Console cons;
static int[] decks, playa, pc;
static int pack, suitss;
static boolean deckEmpty;
public static void main (String[] args)
{
cons = new Console (30, 100, "Crazy Eights");
introductions ();
char palyingAgain;
do
{
cons.setFont (new Font ("Arial", java.awt.Font.PLAIN, 14));
playGame ();
cons.setCursor (30, 32);
palyingAgain = cons.getChar ();
}
while (palyingAgain == 'y' || palyingAgain == 'Y');
cons.close ();
}
public static void playGame ()
{
shuffDeck ();
playa = giveHands ();
pc = giveHands ();
pack = gettingCrds ();
while (!mtHands (1) && !mtHands (2) && !deckEmpty)
{
render ();
playerPlays ();
pcPlaying ();
}
render ();
dispMssg ("Game over. Play again [qq/n]? _");
cons.clearRect (350, 215, 101, 151);
cons.setFont (new Font ("Arial", java.awt.Font.BOLD, 24));
if (mtHands (1))
{
cons.drawString ("YOU WIN!", cons.getWidth () / 2 - 50, cons.getHeight () / 2 + 10);
}
else if (mtHands (2))
{
cons.drawString ("YOU LOSE!", cons.getWidth () / 2 - 50, cons.getHeight () / 2 + 10);
}
else
{
cons.drawString ("IT'S A TIE!", cons.getWidth () / 2 - 50, cons.getHeight () / 2 + 10);
}
}
public static void playerPlays ()
{
int io = 1, qq = 0;
int[] crd = new int [52];
cons.setCursor (29, 1);
cons.print ("", 100);
for (int uu = 0 ; uu < playa.length ; uu++)
{
if (playa [uu] > 0)
{
if (legalCrds (playa [uu]))
{
cons.setCursor (28, 6 + qq * 5);
cons.print (io);
crd [io] = uu;
io++;
}
qq++;
}
}
if (io > 1)
{
dispMssg ("Enter a crd number to play: ");
cons.setCursor (30, 32);
int playingCardsz = cons.readInt ();
while (playingCardsz < 1 || playingCardsz + 1 > io)
{
dispMssg ("Invalid number. Enter a crd number to play: ");
cons.setCursor (30, 48);
playingCardsz = cons.readInt ();
}
pack = playa [crd [playingCardsz]];
playa [crd [playingCardsz]] = 0;
if ((pack) % 13 == 8)
{
dispMssg ("Enter the first letter of the suitss you want to change to: ");
cons.setCursor (30, 61);
String newSuit = cons.readLine ();
if (newSuit.equalsIgnoreCase ("s"))
{
pack = 8;
}
else if (newSuit.equalsIgnoreCase ("h"))
{
pack = 21;
}
else if (newSuit.equalsIgnoreCase ("d"))
{
pack = 34;
}
else if (newSuit.equalsIgnoreCase ("cons"))
{
pack = 47;
}
}
}
else
{
dispMssg ("You can't play any of your crd. Press any key to draw a crd.");
cons.getChar ();
boolean fillz = false;
int uu = 0;
while (!fillz && uu < playa.length)
{
if (playa [uu] == 0)
{
playa [uu] = gettingCrds ();
if (playa [uu] == 0)
{
deckEmpty = true;
}
fillz = true;
}
uu++;
}
}
render ();
}
public static void pcPlaying ()
{
if (!mtHands (1))
{
dispMssg ("Press any key to let pc play...");
cons.getChar ();
int num = -1;
boolean playing = false;
for (int uu = 0 ; uu < pc.length && !playing ; uu++)
{
if (pc [uu] > 0)
{
if (legalCrds (pc [uu]))
{
if ((pc [uu]) % 13 == 8)
{
num = uu;
}
else
{
pack = pc [uu];
pc [uu] = 0;
playing = true;
}
}
}
}
if (!playing)
{
if (num != -1)
{
pack = pc [num];
pc [num] = 0;
int spade = 0, heart = 0, diamond = 0, club = 0;
for (int uu = 0 ; uu < pc.length ; uu++)
{
if (pc [uu] > 0)
{
int suitNumbers = (pc [uu]) / 13;
if (suitNumbers == 0)
spade++;
else if (suitNumbers == 1)
heart++;
else if (suitNumbers == 2)
diamond++;
else if (suitNumbers == 3)
club++;
}
}
if (spade * 3 > heart + diamond + club)
pack = 8;
else if (heart * 3 > spade + diamond + club)
pack = 21;
else if (diamond * 3 > spade + heart + club)
pack = 34;
else if (club * 3 > spade + heart + diamond)
pack = 47;
}
else
{
boolean fillz = false;
int uu = 0;
while (!fillz && uu < pc.length)
{
if (pc [uu] == 0)
{
pc [uu] = gettingCrds ();
if (pc [uu] == 0)
{
deckEmpty = true;
}
fillz = true;
}
uu++;
}
}
}
}
render ();
}
public static void dispMssg (String mssg)
{
cons.setCursor (30, 3);
cons.print (mssg, 96);
}
public static boolean legalCrds (int crd)
{
if ((crd) % 13 == 8)
{
return true;
}
else if ((crd) / 13 == (pack) / 13)
{
return true;
}
else if ((crd) % 13 == (pack) % 13)
{
return true;
}
return false;
}
public static boolean mtHands (int handSide)
{
int[] hnds;
if (handSide == 1)
hnds = playa;
else
hnds = pc;
for (int io = 0 ; io < hnds.length ; io++)
{
if (hnds [io] > 0)
return false;
}
return true;
}
public static void shuffDeck ()
{
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 ran1 = (int) (Math.random () * decks.length);
int ran2 = (int) (Math.random () * decks.length);
int temp = decks [ran1];
decks [ran1] = decks [ran2];
decks [ran2] = temp;
}
deckEmpty = false;
}
public static int[] giveHands ()
{
int[] hnds = new int [52];
for (int uu = 0 ; uu < 5 ; uu++)
{
hnds [uu] = gettingCrds ();
}
return hnds;
}
public static int gettingCrds ()
{
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 showVal (int crd)
{
int val = crd % 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 showSuits (int crd)
{
int suitNumbers = crd / 13;
if (suitNumbers == 0)
return "u2660";
else if (suitNumbers == 1)
return "u2665";
else if (suitNumbers == 2)
return "u2666";
else if (suitNumbers == 3)
return "u2663";
return "X";
}
public static void eraseCrd (int io, int qq)
{
cons.setColor (Color.white);
cons.fillRoundRect (io - 5, qq - 5, 110, 160, 10, 10);
}
public static void drawingCards (int val, int io, int qq, boolean confidential)
{
String ranks = showVal (val);
String suitss = showSuits (val);
if (confidential)
{
cons.setColor (new Color (190, 213, 226));
cons.fillRoundRect (io, qq, 100, 150, 10, 10);
cons.setColor (Color.black);
cons.drawRoundRect (io, qq, 100, 150, 10, 10);
}
else
{
cons.setColor (Color.white);
cons.fillRoundRect (io, qq, 100, 150, 10, 10);
cons.setColor (Color.black);
cons.drawRoundRect (io, qq, 100, 150, 10, 10);
if (suitss.equals ("u2665") || suitss.equals ("u2666"))
cons.setColor (Color.red);
cons.drawString (ranks + suitss, io + 10, qq + 15);
cons.drawString (ranks + suitss, io + 75, qq + 140);
cons.fillRoundRect (io + 20, qq + 30, 60, 90, 10, 10);
cons.setColor (Color.black);
}
}
public static void introductions ()
{
cons.clear ();
cons.setFont (new Font ("Arial", java.awt.Font.BOLD, 48));
cons.drawString ("CRAZY EIGHTS", cons.getWidth () / 2 - 180, cons.getHeight () / 2 - 10);
cons.setFont (new Font ("Arial", java.awt.Font.PLAIN, 14));
cons.drawString ("Press any key to begin.", cons.getWidth () / 2 - 50, cons.getHeight () / 2 + 50);
cons.getChar ();
}
public static void render ()
{
cons.clear ();
int io = 0;
for (int uu = 0 ; uu < pc.length ; uu++)
{
if (pc [uu] != 0)
{
drawingCards (pc [uu], 670 - io * 40, 30, true);
io++;
}
}
io = 0;
for (int uu = 0 ; uu < playa.length ; uu++)
{
if (playa [uu] != 0)
{
drawingCards (playa [uu], 30 + io * 40, 400, false);
io++;
}
}
drawingCards (pack, 350, 215, false);
}
}
Please rate the answer if it helped.....Thankyou
Hope it helps.......
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.