# (Homework Solution): I am writing a program. Including the things on the paper such as the Deck constructor

I am writing a program. Including the things on the paper such as the Deck constructor and Card Class private variables, can someone help me complete this program? When I press new deck, it would create unshuffled deck. When I display the cards, it needs to display a 3×13 column unshuffled. Then, when i press shuffled cards, it would shuffle the cards. When I press display the cards, it would display the cards 3×13 column shuffled. Finally, how can i play the game while always having the game shuffled before i repeatedly press 4. I am having a hard time with formatting the cards in a grid.

Solitaire Prime tiance is a card game that is played by one person. Solitaire is also known by the name because we ed that there are more than 1700 different versions of Solitaire… make that 1701 Patience we are inventing a new Solitaire game today standard deek of cards. Here are Prime! This game uses one standard deck of cards. Here are the rules: Take the top card from the deck and place it face up on the table. 2) 1) The Surn is now the value of that card (Ace = 1, 2-2, 10-10, Jack-10, Queen- 10, King 3) 10) If the Sunn is a prime number, discard that pile, and start over at instruction #1 4) If the Sum is not prime, take the next card from the top of the deck and place it on top of the pile on the table. 5) The Sum is now the sum of all cards in the pile on the table. Continue to play the game, keeping track of how many piles you have created t If the last card from the deck gives you a prime pile, then you win! Write the word hat are prime. “Winner” on the screen and show how many prime piles there were. If the last card from the deck does not give you a prime pile, then you lose. Write the word “Loser” in the screen. In the main function, you will have a menu that looks like this: Welcome to Solitaire Prime! 1) New Deck 2) Display Deck 3) Shuffle Deck 4) Play Solitaire Prime 5) Exit New Deck will create an unshufled deck. (Spades, Hearts, Diamonds, Club… Ace, 2, 3,. .0 Jack, Queen, King) Display Deck will display all cards in a grid: 13 columns by 4 rows. Shuffle Deck will randomly shuffle all cards in the deck. Play Solitaire will play the game as described above. xit will exit the program.

/*

Simple Solitaire Card Game in Java

*/

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

////////////////////////

//

////////////////////////

{

private Object valueField;

{

valueField = newValue;

}

// get Object part of link

public Object value()

{

return valueField;

}

{

}

}

//////////////////////////////

//

// used by CardPile

//////////////////////////////

{

{

}

// true if empty, false otherwise

public boolean empty()

{

}

// add an Object to our list

{

}

// inspect front of list

public Object front()

{

return null;

}

// pop front of list

public Object pop()

{

return null;

return result;

}

// return an iterator for the list

public ListIterator iterator()

{

}

}

////////////////////////////////

// Defines a ListIterator class

//

////////////////////////////////

class ListIterator

{

{

}

// true if we reached end of list, false otherwise

public boolean atEnd()

{

}

public void next()

{

}

// return value of current link

public Object current()

{

return null;

}

}

////////////////////////

// Defines a Card class

//

// used by CardPile

////////////////////////

class Card

{

// data fields for colors and suits

final static int width = 50;

final static int height = 70;

final static int red = 0;

final static int black = 1;

final static int heart = 0;

final static int spade = 1;

final static int diamond = 2;

final static int club = 3;

final static int ace = 0;

final static int king = 12;

private static String names[] = {“A”, “2”, “3”, “4”, “5”, “6”,

“7”, “8”, “9”, “10”, “J”, “Q”, “K”};

// data fields

private boolean faceup;

private int rank;

private int suit;

// constructor

Card (int s, int r)

{

suit = s;

rank = r;

faceup = false;

}

// get rank of card as an int in the interval [0, 12]

public int rank()

{

return rank;

}

// get suit of card as an int in the interval [0, 3]

public int suit()

{

return suit;

}

// true if card is face up, false otherwise

public boolean faceUp()

{

return faceup;

}

// change value of faceup

public void flip()

{

faceup = ! faceup;

}

// true if card is ace, false otherwise

public boolean isAce()

{

return rank == ace;

}

// true if card is king, false otherwise

public boolean isKing()

{

return rank == king;

}

// return color of card as an int in the range [0,1]

public int color()

{

if (suit() == heart || suit() == diamond)

return red;

return black;

}

// draw the card

public void draw (Graphics g, int x, int y) {

// clear rectangle, draw border

g.clearRect(x, y, width, height);

g.setColor(Color.black);

g.drawRect(x, y, width, height);

// draw body of card

if (faceUp())

{

if (color() == red)

g.setColor(Color.red);

else

g.setColor(Color.black);

g.drawString(names[rank()], x+3, y+15);

if (suit() == heart)

{

g.drawLine(x+25, y+30, x+35, y+20);

g.drawLine(x+35, y+20, x+45, y+30);

g.drawLine(x+45, y+30, x+25, y+60);

g.drawLine(x+25, y+60, x+5, y+30);

g.drawLine(x+5, y+30, x+15, y+20);

g.drawLine(x+15, y+20, x+25, y+30);

}

{

g.drawLine(x+25, y+20, x+40, y+50);

g.drawLine(x+40, y+50, x+10, y+50);

g.drawLine(x+10, y+50, x+25, y+20);

g.drawLine(x+23, y+45, x+20, y+60);

g.drawLine(x+20, y+60, x+30, y+60);

g.drawLine(x+30, y+60, x+27, y+45);

}

else if (suit() == diamond)

{

g.drawLine(x+25, y+20, x+40, y+40);

g.drawLine(x+40, y+40, x+25, y+60);

g.drawLine(x+25, y+60, x+10, y+40);

g.drawLine(x+10, y+40, x+25, y+20);

}

else if (suit() == club)

{

g.drawOval(x+20, y+25, 10, 10);

g.drawOval(x+25, y+35, 10, 10);

g.drawOval(x+15, y+35, 10, 10);

g.drawLine(x+23, y+45, x+20, y+55);

g.drawLine(x+20, y+55, x+30, y+55);

g.drawLine(x+30, y+55, x+27, y+45);

}

}

else // face down

{

g.setColor(Color.yellow);

g.drawLine(x+15, y+5, x+15, y+65);

g.drawLine(x+35, y+5, x+35, y+65);

g.drawLine(x+5, y+20, x+45, y+20);

g.drawLine(x+5, y+35, x+45, y+35);

g.drawLine(x+5, y+50, x+45, y+50);

}

}

}

//////////////////////////////////////

// Defines a CardPile class

//

// used as a base to build card piles

//////////////////////////////////////

class CardPile

{

// coordinates of the card pile

protected int x;

protected int y;

CardPile (int xl, int yl)

{

x = xl;

y = yl;

}

/////////////////////////////////////

// true if pile is empty, false otherwise

public final boolean empty()

{

return cardList.empty();

}

// inspect card at the top of pile

public final Card top()

{

return (Card)cardList.front();

}

// pop card at the top of pile

public final Card pop()

{

return (Card)cardList.pop();

}

/////////////////////////////////////////

// the following are sometimes overridden

// true if point falls inside pile, false otherwise

public boolean includes (int tx, int ty)

{

return x <= tx && tx <= x + Card.width &&

y <= ty && ty <= y + Card.height;

}

// to be overridden by descendants

public void select (int tx, int ty)

{

// do nothing

}

// add a card to pile

{

}

// draw pile

public void display (Graphics g)

{

g.setColor(Color.black);

if (cardList.empty())

g.drawRect(x, y, Card.width, Card.height);

else

top().draw(g, x, y);

}

// to be overridden by descendants

public boolean canTake (Card aCard)

{

return false;

}

// get number of cards in pile

public int getNoCards()

{

int count = 0;

ListIterator iterator = cardList.iterator();

while (!iterator.atEnd())

{

count++;

iterator.next();

}

return count;

}

}

////////////////////////////

// Defines a DeckPile class

////////////////////////////

class DeckPile extends CardPile

{

DeckPile (int x, int y)

{

// first initialize parent

super(x, y);

// then create the new deck

// first put them into a local pile

CardPile pileOne = new CardPile(0, 0);

CardPile pileTwo = new CardPile(0, 0);

int count = 0;

for (int i = 0; i < 4; i++)

for (int j = 0; j <= 12; j++)

{

count++;

}

// then pull them out randomly

for (; count > 0; count–)

{

int limit = ((int)(Math.random() * 1000)) % count;

// move down to a random location in pileOne

// while poping the cards into pileTwo

for (int i = 0; i < limit; i++)

// then add the card found there

// to our LinkedList object: cardList

// now put back into pileOne the cards

// that we poped into pileTwo

while (!pileTwo.empty())

}

}

public void select(int tx, int ty)

{

// if deck becomes empty, refill from discard pile

if (empty())

{

{

top().flip();

}

}

else

}

}

///////////////////////////////

///////////////////////////////

{

{

super (x, y);

}

{

if (!aCard.faceUp())

aCard.flip();

}

public void select (int tx, int ty)

{

if (empty())

return;

Card topCard = top();

// check the SuitPile’s first

for (int i = 0; i < Solitaire.no_suit_piles; i++)

if (Solitaire.suitPile[i].canTake(topCard))

{

return;

}

// then check the TablePile’s

for (int i = 0; i < Solitaire.no_table_piles; i++)

if (Solitaire.tableau[i].canTake(topCard))

{

return;

}

}

}

////////////////////////////

// Defines a SuitPile class

////////////////////////////

class SuitPile extends CardPile

{

SuitPile (int x, int y)

{

super(x, y);

}

public boolean canTake (Card aCard)

{

if (empty())

return aCard.isAce();

Card topCard = top();

return (aCard.suit() == topCard.suit()) &&

(aCard.rank() == 1 + topCard.rank());

}

public void select (int tx, int ty)

{

if (empty())

return;

Card topCard = top();

// check the TablePile’s

for (int i = 0; i < Solitaire.no_table_piles; i++)

if (Solitaire.tableau[i].canTake(topCard))

{

return;

}

}

}

/////////////////////////////

// Defines a TablePile class

/////////////////////////////

class TablePile extends CardPile

{

final static int ydist = 25;

TablePile (int x, int y, int c)

{

// initialize the parent class

super(x, y);

// then initialize our pile of cards

for (int i = 0; i < c; i++)

// flip topmost card face up

top().flip();

}

public boolean canTake (Card aCard)

{

if (empty())

return aCard.isKing();

Card topCard = top();

// if our topmost card is face down

// we can’t accept another card

if (!topCard.faceUp())

return false;

return (aCard.color() != topCard.color()) &&

(aCard.rank() == topCard.rank() – 1);

}

public boolean includes (int tx, int ty)

{

if (empty())

return false;

// don’t test bottom of card

return x <= tx && tx <= x + Card.width &&

y <= ty;

}

public void select (int tx, int ty)

{

if (empty())

return;

// if face down, then flip

Card topCard = top();

if (!topCard.faceUp())

{

topCard.flip();

return;

}

// see if any suit pile can take card

for (int i = 0; i < Solitaire.no_suit_piles; i++)

if (Solitaire.suitPile[i].canTake(topCard))

{

return;

}

// try to create a build

CardPile build = new CardPile(0, 0);

// get the cards for the build from the suit pile

while (!empty())

{

// stop if we reached a card that is face down

if (!top().faceUp())

break;

}

// We don’t allow the user to play a King card

// that is at the bottom of a table pile

// to another table pile

if (build.top().isKing() && empty())

{

while (!build.empty())

return;

}

// if we have to play only one card

if (build.top() == topCard)

{

// put it back into the table pile

// we have already tried the suit piles

// see if any other table pile can take card

for (int i = 0; i < Solitaire.no_table_piles; i++)

if (Solitaire.tableau[i].canTake(topCard))

{

return;

}

}

else // we got ourselves a build to play

{

topCard = build.top();

// see if any other table pile can take this build

for (int i = 0; i < Solitaire.no_table_piles; i++)

if (Solitaire.tableau[i].canTake(topCard))

{

while (!build.empty())

return;

}

// can’t play the build?

// then we must restore our pile

while (!build.empty())

}

}

private void stackDisplay(Graphics g)

{

// holds y-coordinate of cards in pile

int localy = y;

// get iterator for our list

ListIterator iterator = cardList.iterator();

// build reversed order list

while (!iterator.atEnd())

{

iterator.next();

}

// get iterator for reversed order list

iterator = reverseCardList.iterator();

// Go through the reversed order list

// and draw each card in the list

while (!iterator.atEnd())

{

((Card)iterator.current()).draw(g, x, localy);

localy += ydist;

iterator.next();

}

}

public void display (Graphics g)

{

stackDisplay(g);

}

}

///////////////////////////

// Defines Solitaire class

///////////////////////////

public class Solitaire extends Applet

{

final static int no_card_piles = 13;

final static int no_suit_piles = 4;

final static int no_table_piles = 7;

final static int topMargin = 40;

final static int leftMargin = 5;

final static int distTable = 5;

final static int distSuit = 10;

static DeckPile deckPile;

static TablePile tableau[];

static SuitPile suitPile[];

static CardPile allPiles[];

protected void initialize()

{

// first allocate the arrays

allPiles = new CardPile[no_card_piles];

suitPile = new SuitPile[no_suit_piles];

tableau = new TablePile[no_table_piles];

// then fill them in

int xDeck = leftMargin + (no_table_piles – 1) * (Card.width + distTable);

allPiles[0] = deckPile = new DeckPile(xDeck, topMargin);

topMargin);

for (int i = 0; i < no_suit_piles; i++)

allPiles[2+i] = suitPile[i] =

new SuitPile(leftMargin + (Card.width + distSuit) * i, topMargin);

for (int i = 0; i < no_table_piles; i++)

allPiles[6+i] = tableau[i] =

new TablePile(leftMargin + (Card.width + distTable) * i,

Card.height + distTable + topMargin, i+1);

showStatus(“Welcome to Solitaire!”);

}

protected boolean gameEnded()

{

if (!deckPile.empty())

return false;

return false;

for (int i = 0; i < no_table_piles; i++)

if (!tableau[i].empty())

return false;

// if we reached this point then all piles are empty

// notify the user in the status bar

showStatus(“Congratulations! You have won this game.”);

return true;

}

public void init()

{

// Create a new game button

Button b = new Button(“New game”);

// Define, instantiate, and register a listener to handle button presses

public void actionPerformed(ActionEvent e)

{ // start a new game

initialize();

repaint();

}

});

// Add the button to the applet

// Define, instantiate and register a MouseListener object.

public void mouseClicked(MouseEvent e)

{

if (gameEnded())

return;

showStatus(“Neat click!”);

int x = e.getX();

int y = e.getY();

for (int i = 0; i < no_card_piles; i++)

if (allPiles[i].includes(x, y))

{

allPiles[i].select(x, y);

repaint();

}

}

});

initialize();

}

public void paint(Graphics g)

{

for (int i = 0; i < no_card_piles; i++)

allPiles[i].display(g);

}

}

Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

## Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.