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

JAVA please Overview: You are to write a program that simulates playing the soli

ID: 3830409 • Letter: J

Question

JAVA please Overview: You are to write a program that simulates playing the solitaire card game Clock. We provide several sites at the end of this document that describe and demonstrate the rules; we suggest you familiarize yourself with the game first. Here’s how we want you to implement the simulation. Part 1 - Required Classe Create a Card class that can represent each of the 52 playing cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King ... in each of the four suits: Spades, Hearts, Diamonds, and Clubs. Implement the following methods: constructors getSuit() getValue() String toString(): to print, e.g., “10 of Hearts” or “Queen of Diamonds” or “Ace of Spades” Update: We are thinking that printing cards more compactly -- e.g., “10H” or “QD” or “AS” -- i.e. a one (or two) character value, followed by a one-character suit Create a Deck class that represents a standard deck of 52 playing cards. Implement the following methods: constructors void shuffle(): randomly interchanges cards in the deck to simulate shuffling Card dealCard() int cardsLeft() String toString(): iterate through your deck array, printing all the cards, in a way that’s convenient to check what cards are there; we think it’ll be best to print all the cards on one line (or print them on four lines, if you want to get fancy); for cards that have been dealt, either don’t print them, or print them separately, labeled as “dealt” Create a Pile class that contains no more than five cards... some face down, and some face up. Implement the following methods: constructors void addCardFaceDown( Card card) Card removeCard() - removes and returns the “top” face down card (null if there are none) int getNumberOfFaceDown() void addCardFaceUp( Card card) int getNumberOfFaceUp() String toString() - print the cards in the Pile; print the contents of a pile on one or two lines; label the portion of the pile that’s face up versus face down Part 2 - The Clock Solitaire Game Implement the Clock Solitaire Game simulation, with all the following capabilities. We strongly suggest you don’t ignore handling command line arguments, as this feature is simply to implement. Name your primary class -- the one with the main() method -- ClockSolitaire . Command-Line Arguments Your program will accept two command line arguments. (These arguments are optional, and your program will use the “default” values if the argument(s) are not specified. And, implementing handling of command line arguments is not optional.) Print level - one of the following three values: a. verbose b. normal (the default) c. silent Number of games to play (default: 1) Setup Tasks create thirteen Piles these should be stored in an array of Piles (rather than thirteen discrete variables) create and shuffle a Deck of Cards deal out the deck to populate (initialize) the thirteen Piles with four face-down cards each You may deal one card for each of the thirteen Piles, and then a second card, and so on... or, you may deal four cards to fill the first Pile, four for the second Pile, and so on... print the thirteen Piles -- the game board (this will mainly uses Pile.toString() ), if the print level is not “silent”; label each pile with its clock position; for simplicity, you can print one pile per line (you can get more creative, if you like) print the “score”: the number of Piles with at least one face-down card, if the print level is not “silent”; note a game is won when the “score” is zero this will be 13 at the beginning of each game Playing the Game remove (top, face-down) card from Pile 13, the Kings Pile add it, face-up, to the (“bottom” of the) correct Pile if the print level is “verbose”, print the game board -- the thirteen Piles -- as above a. also, keep track of and print the “step number,” a counter that you increment each time you “remove-and-add” a card remove (top, face-down) card from that same Pile one needs to check that there are face-down cards remaining if there are no face-down cards remaining, the game is over, if you’re on Pile 13 (if there are no face-down cards remaining, and you’re not on Pile 13, then there’s an error) repeat this process by going to step 2 When Each Game is Done print the thirteen Piles -- the game board (as above), if the print level is not “silent” print the “score”: the number of Piles with at least one face-down card, if the print level is not “silent” a score of “zero” is a “win”; note this is rare increment a counter in a Scores array for the number of Piles with at least one face-down card repeat, from the top (the Setup), according to how many games to play, as specified in the command-line arguments When You’ve Completed Playing the Required Number of Games print the number of games played print the Scores array, along with percentages, as follows; this will be 14 lines, with both the number and percentage of games that resulted in each score (print this for all print levels, including “silent”) Here's what I have so far, I just need help to do the second part if someone could help me finished it.

package solitairecardgameclock;

public class card{

    private String face;
    private int value;
    public card(String face,int value){
        this.face = face;
        this.value = value;
    }

    public String getFace() {
        return face;
    }

    public int getValue(){
        return value;
    }
   
    public String toString(){
         if (value == 1) {
            return "Ace of " + face;
        }
        else if(value==11){
            return "Jack of " + face;
        }
        else if(value==12){
            return "Queen of " + face;
        }
        else if(value==13){
            return "King of " + face;
        }
        else{
            return value + "of" + face;
        }
    }

}

package solitairecardgameclock;
import java.util.Arrays;
public class deck
{
    card[] cards;
    String[] faces = new String[]{"Heart", "Diamond", "Club", "Spade"};
    int numberofCards;
    int cardsUsed;
   
    public deck(){
        cards = new card[52];
       
        for (String face : faces) {
            for (int i = 1; i <= 13; i++) {
                cards[numberOfCards++] = new Card(face, i);
            }
        }
        shuffle();
    }
   
    public void shuffle(){
        for(int i = cards.length -1; i> 0; i--){
            int rand = (int)(Math.random()* (i +1));
            card temp = cards[i];
            cards[i] = cards[rand];
            cards[rand] = temp;
        }
        cardsUsed = 0;
    }
   
    public card dealCard(){
        cardsUsed++;
        return cards[cardsUsed - 1];
    }
   
    public int cardsLeft(){
        return cards.length - cardsUsed;
    }
}

package solitairecardgameclock;
public class ClockSolitaireGame
{
    Pile[] piles;
    Deck deck;
    public ClockSolitaireGame() {
        piles = new Pile[13];
        deck = new Deck();
        int count=0;
        for(int i=1;i<=13;i++){
            piles[i-1]=new Pile();
            for(int j= 0;j<4;j++){
                piles[i-1].addCardFaceDown(deck.dealCard());
            }
        }
    }
}

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Link
{
private Object valueField;
private Link   nextLink;

public Link (Object newValue, Link next)
{
    valueField = newValue;
    nextLink   = next;
}

// get Object part of link
public Object value()
{
    return valueField;
}

// get Link part of link
public Link next()
{
    return nextLink;
}
}

class LinkedList
{
private Link firstLink;

public LinkedList()
{
    firstLink = null;
}

// true if empty, false otherwise
public boolean empty()
{
    return firstLink == null;
}

// add an Object to our list
public void add (Object newValue)
{
    firstLink = new Link(newValue, firstLink);
}

// inspect front of list
public Object front()
{
    if (firstLink == null)
      return null;

    return firstLink.value();
}

// pop front of list
public Object pop()
{
    if (firstLink == null)
      return null;

    Object result = firstLink.value();
    firstLink = firstLink.next();
    return result;
}

// return an iterator for the list
public ListIterator iterator()
{
    return new ListIterator(firstLink);
}
}

class ListIterator
{
private Link currentLink;

public ListIterator (Link firstLink)
{
    currentLink = firstLink;
}

// true if we reached end of list, false otherwise
public boolean atEnd()
{
    return currentLink == null;
}

// move to next link
public void next()
{
    if (currentLink != null)
      currentLink = currentLink.next();
}

// return value of current link
public Object current()
{
    if (currentLink == null)
      return null;

    return currentLink.value();
}
}
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);
   }
else if (suit() == spade)
   {
     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);
      }
}
}
class CardPile
{
// coordinates of the card pile
protected int x;
protected int y;

// linked list of cards
protected LinkedList cardList;

CardPile (int xl, int yl)
{
    x = xl;
    y = yl;
    cardList = new LinkedList();
}
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();
}

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
public void addCard (Card aCard)
{
    cardList.add(aCard);
}

// 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;
}
}
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++)
{
   pileOne.addCard(new Card(i, 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++)
   pileTwo.addCard(pileOne.pop());

// then add the card found there
// to our LinkedList object: cardList
addCard(pileOne.pop());

// now put back into pileOne the cards
// that we poped into pileTwo
while (!pileTwo.empty())
   pileOne.addCard(pileTwo.pop());
    }
}

public void select(int tx, int ty)
{
    // if deck becomes empty, refill from discard pile
    if (empty())
      {
while (!Solitaire.discardPile.empty())
   {
     addCard(Solitaire.discardPile.pop());
     top().flip();
   }
      }
    else
      Solitaire.discardPile.addCard(pop());
}
}
class DiscardPile extends CardPile
{
DiscardPile (int x, int y)
{
    super (x, y);
}

public void addCard (Card aCard)
{
    if (!aCard.faceUp())
      aCard.flip();

    super.addCard(aCard);
}

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))
{
   Solitaire.suitPile[i].addCard(pop());
   return;
}

    // then check the TablePile's
    for (int i = 0; i < Solitaire.no_table_piles; i++)
      if (Solitaire.tableau[i].canTake(topCard))
{
   Solitaire.tableau[i].addCard(pop());
   return;
}
}
}

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))
{
   Solitaire.tableau[i].addCard(pop());
   return;
}
}
}
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++)
      addCard(Solitaire.deckPile.pop());

    // 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))
{
   Solitaire.suitPile[i].addCard(pop());
   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;

build.addCard(pop());
      }

    // 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())
   addCard(build.pop());
return;
      }   

    // if we have to play only one card
    if (build.top() == topCard)
      {
// put it back into the table pile
addCard(build.pop());

// 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))
            {
              Solitaire.tableau[i].addCard(pop());
              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())
  Solitaire.tableau[i].addCard(build.pop());
      
       return;
     }

// can't play the build?
// then we must restore our pile
while (!build.empty())
   addCard(build.pop());
      }
}

private void stackDisplay(Graphics g)
{
    // holds y-coordinate of cards in pile
    int localy = y;

    LinkedList reverseCardList = new LinkedList();

    // get iterator for our list
    ListIterator iterator = cardList.iterator();

    // build reversed order list
    while (!iterator.atEnd())
      {
reverseCardList.add(iterator.current());
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);
}
}

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 DiscardPile discardPile;
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);
    allPiles[1] = discardPile = new DiscardPile(xDeck - Card.width - distSuit,
      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;

    if (!discardPile.empty())
      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
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
{ // start a new game
   initialize();
   repaint();
}
    });

    // Add the button to the applet
    add(b);

    // Define, instantiate and register a MouseListener object.
    addMouseListener(new MouseAdapter() {
      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);
}
}