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

Problem specification You will model a particular type of card game. The game st

ID: 3816519 • Letter: P

Question

Problem specification You will model a particular type of card game. The game starts with 15 cards. (They need not be playing cards. Unmarked index cards work just as well.) Randomly divide them into some number of piles of random size. For example, you might start with piles of size 2, 10, 1 and 2. In each round, you take one card from each pile, forming a new pile with these cards. For example, the sample starting configuration would be transformed into piles of size 1, 9, 1 and 4. The game is over when the piles have size 1, 2, 3, 4, and 5, in any order. (It can be shown that you always end up with such a configuration.)
You will write a Solitaire class to implement this behavior.


The SolitaireTester class You are given a SolitaireTester class that uses Solitaire.
Source code is shown below, and can be downloaded from Blackboard, Course Documents, ‘Arrays and array lists’ folder, Example programs. SolitaireTester code must not be changed in any way.
public class SolitaireTester { public static void main(String[] args) { Solitaire s = new Solitaire();
System.out.println("Start: " + s.toString());
int rounds = 0; while (!s.over()) { s.round(); ++rounds; System.out.println(rounds + ": " + s.toString()); } System.out.println("Rounds: " + rounds); } }
SolitaireTester produces a random starting configuration and prints it. Then keeps applying the solitaire step and prints the result. Stops when the solitaire final configuration is reached and prints the number of rounds.




A run of the program will look something like:
Start: [2, 10, 1, 2] 1: [1, 9, 1, 4] 2: [8, 3, 4] 3: [7, 2, 3, 3] 4: [6, 1, 2, 2, 4] 5: [5, 1, 1, 3, 5] 6: [4, 2, 4, 5] 7: [3, 1, 3, 4, 4] 8: [2, 2, 3, 3, 5] 9: [1, 1, 2, 2, 4, 5] 10: [1, 1, 3, 4, 6] 11: [2, 3, 5, 5] 12: [1, 2, 4, 4, 4] 13: [1, 3, 3, 3, 5] 14: [2, 2, 2, 4, 5] 15: [1, 1, 1, 3, 4, 5] 16: [2, 3, 4, 6] 17: [1, 2, 3, 5, 4] Rounds: 17


The Solitaire class Here are an outline and some hints for Solitaire:

Explanation / Answer

Solitaire.java

/**
* Creates a game of Solitaire.
*
*/
import java.util.ArrayList;

/**
* Defining public class Solitaire
*/
public class Solitaire {
  
// constant for number of final piles.
int FINAL_NUMBER_OF_PILES = 5;
  
// field containing current piles
private ArrayList<Integer> piles;

  
public Solitaire()
{
// Randomly generating initial number of piles
int numberOfStartingPiles = (int) (Math.random() * 10) + 1;
  
// constant for number of cards
// Number of cards will be depending on number of final piles required.
// for example here 1,2,3,4,5 sum to 15 which n(n+1)/2 where n is final number of piles
int maxSize = FINAL_NUMBER_OF_PILES*(FINAL_NUMBER_OF_PILES+1)/2;
int size = maxSize;
  
boolean continueLoop = true;
int total = 0;
  
// Fill starting piles with random number of cards
while (continueLoop) {
piles = new ArrayList<Integer>();
for (int i = 0; i < numberOfStartingPiles; i++) {
int temp = getRandomPile(size - numberOfStartingPiles + i);
if (i == numberOfStartingPiles - 1) {
piles.add(size);
} else {
piles.add(temp);
size = size - temp;
}
}
for (int i = 0; i < piles.size(); i++) {
total += piles.get(i);
}
if (total == maxSize) {
continueLoop = false;
}
}
}

/**
* Method for creating random piles
* @param size size of the pile
* @return numbers in pile
*/
public int getRandomPile(int size) {
int numberOfStartingPiles = (int) (Math.random() * size) + 1;
return numberOfStartingPiles;
}

/**
* Returns a string representation of piles
*/
public String toString() {
return piles.toString();
}

/**
* Check for piles are same as final piles
* @return True if pile match required pile
*/
public boolean over() {
  
// create final piles configuration
int[] finalPile = new int[FINAL_NUMBER_OF_PILES];
for(int i = 0; i < FINAL_NUMBER_OF_PILES; i++)
finalPile[i] = i+1;
  
// check if current pile is same as final required piles(in any order)
boolean flag = true;
for (int i = 0; i < piles.size(); i++) {
if (! piles.contains(finalPile[i])) {
flag = false;
break;
}
}
return flag;
}

/**
* Method for creating a round
*/
public void round() {
int newPile = piles.size();
for (int i = 0; i < piles.size(); i++) {
piles.set(i, (piles.get(i)) - 1);
if (piles.get(i) == 0) {
piles.remove(i);
i--;
}
}
piles.add(newPile);
//Collections.sort(piles);
}
}


SolitaireTester.java

public class SolitaireTester
{
public static void main(String[] args)
{
Solitaire s = new Solitaire();
System.out.println("Start: " + s.toString());
int rounds = 0;
while (!s.over()) {
s.round();
++rounds;
System.out.println(rounds + ": " + s.toString());
}
System.out.println("Rounds: " + rounds);
}
}

Please provide positive feedback if this solved your question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote