JAVA please \"ERGENT\" in multiple class including driver class. Overview: You a
ID: 3833024 • Letter: J
Question
JAVA please "ERGENT" in multiple class including driver class.
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”)
Explanation / Answer
Card.java
public class Card {
String suite;
String value;
public Card(String suite, String value)
{
this.suite = suite;
this.value = value;
}
int cardNum;
int getCardNum()
{
return cardNum;
}
public Card(int card)
{
int v = card%13;
this.cardNum = v;
int s = card/13;
if(v == 0)
{
value = "A";
}
else if(v == 11)
{
value = "J";
}
else if(v == 12)
{
value = "Q";
}
else if(v == 13)
{
value = "K";
}
else
{
value = String.valueOf(v);
}
if (s == 0)
{
suite = "C";
}
else if(s == 1)
{
suite = "D";
}
else if(s == 2)
{
suite = "H";
}
else
{
suite = "S";
}
}
public String getSuite()
{
return suite;
}
public String getValue()
{
return value;
}
public String toString()
{
return value + suite;
}
}
Deck.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Deck {
private List<Card> cardList = new ArrayList<>();
private List<Card> dealtCard = new ArrayList<>();
private int cardsDealed;
public Deck()
{
for(int i = 0; i < 52; i++)
{
cardList.add(new Card(i));
}
cardsDealed = 0;
}
public void shuffle()
{
Collections.shuffle(cardList);
}
public Card dealCard()
{
if(cardList.size() <= 0)
return null;
Card c = cardList.get(0);
dealtCard.add(c);
cardList.remove(0);
return c;
}
int cardLeft()
{
return 52 - cardsDealed;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("dealt: ");
for(Card c : dealtCard)
{
sb.append(c.toString()).append(" ");
}
sb.append(" ");
sb.append("Remaining: ");
for(Card c: cardList)
{
sb.append(c.toString()).append(" ");
}
sb.append(" ");
return sb.toString();
}
}
Pile.java
import java.util.ArrayList;
import java.util.List;
public class Pile {
private List<Card> faceDown = new ArrayList<>();
private List<Card> faceUp = new ArrayList<>();
public Pile()
{
}
void addCardFaceDown( Card card)
{
faceDown.add(card);
}
Card removeCard()
{
if(faceDown.size() <= 0)
return null;
Card c = faceDown.get(0);
faceDown.remove(0);
return c;
}
int getNumberOfFaceDown()
{
return faceDown.size();
}
void addCardFaceUp( Card card)
{
faceUp.add(card);
}
int getNumberOfFaceUp()
{
return faceUp.size();
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Facedown: ");
for(Card c: faceDown)
{
sb.append(c.toString()).append(" ");
}
sb.append(" ");
sb.append("Faceup: ");
for(Card c: faceUp)
{
sb.append(c.toString()).append(" ");
}
return sb.toString();
}
}
ClockSolitrate.java
import java.util.ArrayList;
import java.util.List;
public class ClockSolitaire {
public static int getScore(List<Pile> pileList)
{
int score = 0;
for(int i = 0; i < 13; i++)
{
if(pileList.get(i).getNumberOfFaceDown() != 0)
{
score++;
}
}
return score;
}
public static void printPiles(List<Pile> pileList)
{
// print piles
System.out.println("Piles are");
for(int i = 0; i < 13; i++)
{
System.out.println("Clock position " + i + " Pile " + pileList.get(i));
}
}
public static void main(String[] args)
{
List<Pile> pileList = new ArrayList<>();
// setting up piles
for(int i = 0; i < 13; i++)
{
pileList.add(new Pile());
}
// creating deck and shuffling
Deck deck = new Deck();
deck.shuffle();
// adding cards to pile
for(Pile pile : pileList)
{
for(int i = 0; i < 4; i++)
{
pile.addCardFaceDown(deck.dealCard());
}
}
// print piles
printPiles(pileList);
int score = getScore(pileList);
System.out.println("score: " + score);
if(score == 0)
{
System.out.println("Game won");
return;
}
int counter = 0;
int pileNum = 12;
while(true)
{
counter++;
System.out.println("Round number: " + counter);
Card c = pileList.get(pileNum).removeCard();
if(c == null)
break;
pileNum = c.getCardNum();
pileList.get(pileNum).addCardFaceUp(c);
c = pileList.get(pileNum).removeCard();
if(c == null)
{
if(pileNum == 12)
{
System.out.println("Error");
break;
}
else
{
// print piles
printPiles(pileList);
score = getScore(pileList);
System.out.println("score: " + score);
System.out.println("Game won");
break;
}
}
// print piles
printPiles(pileList);
score = getScore(pileList);
System.out.println("score: " + score);
if(score == 0)
{
System.out.println("Game won");
break;
}
}
score = getScore(pileList);
System.out.println("Final score: " + score);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.