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

Create a Pile class that contains no more than five cards… some face down, and s

ID: 3823493 • Letter: C

Question

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)Overview You are to write a program that simulates playing the solitaire card game Clock. Part 1 - Required Classes 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 -- will make both debugging. 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, and will be a great help in your debugging process, and in our grading. 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.) 1. Print level - one of the following three values:

a. verbose
b. normal (the default) c.

silent2. 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

Explanation / Answer

import java.util.ArrayList;
import java.util.List;

class Pile {
  
   public List<Card> faceupCards = new ArrayList();

   public List<Card> facedownCards = new ArrayList();

   void addCardFaceDown(Card card) {
       facedownCards.add(card);
   }

   // removes and returns the “top” face down card
   Card removeCard() {
       int index = facedownCards.size() - 1;
       if (index < 0)
           return null;
       else
           return (Card) facedownCards.remove(index);

   }

   int getNumberOfFaceDown() {
       return facedownCards.size();

   }

   void addCardFaceUp(Card card) {
       faceupCards.add(card);
   }

   int getNumberOfFaceUp() {
       return faceupCards.size();

   }

   // ; print the contents of a pile on one or two lines;
   public String toString() {

       return getNumberOfFaceUp() + " of " + getNumberOfFaceDown();
   }

}

class Card {
   private int Suit;
   private int Value;
   Card(int s,int v ) {
       Suit=s;
       Value =v;
   }
   // returns the suit
   public int getSuit() {
       return Suit;
   }
//   public void setSuit(int suit) {
//       Suit = suit;
//   }
  
   // returns the value
   public int getValue() {
       return Value;
   }
//   public void setValue(int value) {
//       Value = value;
//   }
//  
   // converts the string and prints as "10H or QD"
   public String toString()
   {
       String val;
       String [] suitList ={ "", "C", "D", "H", "S" };
       if (Value == 1) val = "A";
       else if (Value == 11) val = "J";
       else if (Value == 12) val = "Q";
       else if (Value == 13) val = "K";
       else val = String.valueOf(Value); // int to String
       String s = val + suitList[Suit];
       for (int k=s.length()+1; k<=17; k++)
           s = s + " ";
       return s;
   }
  
}

class Deck {
   private Card[] deck;
   private int noOfCards;
  
   public Deck() {
       deck = new Card[52];
   }
   // shuffles the deck of cards
   public void shuffle() {
       for (int next = 0; next < noOfCards-1; next++)
       {
           int r = myRandom(next, noOfCards-1);
           Card temp = deck[next];
           deck[next] = deck[r];
           deck[r] = temp;
       }
   }

   // returns the random value after shuffling
   private int myRandom(int next, int i) {
       // TODO Auto-generated method stub
       return (int)((i+1-next)*Math.random()+next);
   }
   // returns the no of cards that are "dealt"
   Card dealCard()
   {
       if (noOfCards == 0)
           return null;
       noOfCards--;
       return deck[noOfCards];
   }
      
   // returns the no of cards that are "left"
   public int cardsLeft() {
       return noOfCards;
   }
  
   // prints the cards of deck
   public String toString(){
      
      
      
       return null;
      
   }
}

public class ClockSolitaire {
   Deck d=new Deck();
  
   private static final Card[] Deck = null;

   public static String printLevel="normal";
  
   public static int noOfgames=1;

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       if(args.length==2)   {
       printLevel=args[0];
      
       noOfgames=new Integer(args[1]);
      
       }
      
       final void dealDeck()
       {
           d.shuffle();
           int index = 0;
           for (int r = 1; r <= 13; r++)
           for (int s = 1; s <= 4; s++)
           {
               Deck[index] = new Card(r, s);
               index++;
           }
           int numCards = 52;
           }
       }
}