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

I need to create a bulgarian solitaire game that Write the code to implement the

ID: 3740939 • Letter: I

Question

I need to create a bulgarian solitaire game that

Write the code to implement the game.should start with picking a random number piles and distributing the 45 cards among those piles.

Then call a method to start playing the game.

For each iteration of the game, print the piles. For example if you have 3 piles, one with 40, one with 2, one with 3 print this:

40 2 3

Now for the starting i dont always equal 45 this is what i got but my real question is whats causing my infinite loop run it a few times if it works perfect the first time

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class BulgarianSolitaire {

ArrayList<Integer> cards = new ArrayList<Integer>();
Random rand = new Random();
boolean cont = true;
boolean cont2 = true;

public static void main(String[] args) {
BulgarianSolitaire game = new BulgarianSolitaire();
}

public BulgarianSolitaire() {
System.out.println("Started");
int sum = 0;
while (cont) {
if (sum < 45) {
cards.add(rand.nextInt(46 - sum));
} else {
cont = false;
}

sum = 0;
for (int i = 0; i < cards.size(); i++) {
sum += cards.get(i);
}

removeZeros(cards);

System.out.println(cards);
}

System.out.println("Finished Generating Start");

while (cont2) {
solitaireStep();
System.out.println(cards);
if (checkCards()) {
cont2 = false;
}
}

Collections.sort(cards);
System.out.println("Cards are sorted");
System.out.println(cards);
}
  
  

public void removeZeros(ArrayList<Integer> list) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j) == 0) {
list.remove(j);
}
}
}

public void solitaireStep() {

int numberRemoved = 0;

for (int i = 0; i < cards.size(); i++) {
int value = cards.get(i);
cards.set(i, value - 1);
removeZeros(cards);
numberRemoved++;
}

cards.add(numberRemoved);
}

public boolean checkCards() {
ArrayList<Integer> expectedCards = new ArrayList<Integer>();

for (int i = 1; i < 10; i++) {
expectedCards.add(i);
}

ArrayList<Integer> sortedCards = cards;
Collections.sort(sortedCards);
boolean equal = true;
if (sortedCards.size() != expectedCards.size()) {
equal = false;
}

for (int i = 0; i < sortedCards.size(); i++) {
if (sortedCards.size() == expectedCards.size()) {
if (sortedCards.get(i) != expectedCards.get(i)) {
equal = false;
}
}
}

return equal;
}
}

Explanation / Answer

public BulgarianSolitaire() {
System.out.println("Started");
int sum = 0;
while (cont) {
if (sum < 45) {
cards.add(rand.nextInt(46 - sum));
} else {
cont = false;
}

sum = 0;
for (int i = 0; i < cards.size(); i++) {
sum += cards.get(i);
}

removeZeros(cards);

System.out.println(cards);
}

System.out.println("Finished Generating Start");

while (cont2) {
solitaireStep();
System.out.println(cards);
if (checkCards()) {
cont2 = false;
}
}

Collections.sort(cards);
System.out.println("Cards are sorted");
System.out.println(cards);
}
  
  

public void removeZeros(ArrayList<Integer> list) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j) == 0) {
list.remove(j);
}
}
}

public void solitaireStep() {

int numberRemoved = 0;

for (int i = 0; i < cards.size(); i++) {
int value = cards.get(i);
cards.set(i, value - 1);
removeZeros(cards);
numberRemoved++;
}

cards.add(numberRemoved);
}

public boolean checkCards() {
ArrayList<Integer> expectedCards = new ArrayList<Integer>();

for (int i = 1; i < 10; i++) {
expectedCards.add(i);
}

ArrayList<Integer> sortedCards = cards;
Collections.sort(sortedCards);
boolean equal = true;
if (sortedCards.size() != expectedCards.size()) {
equal = false;
}

for (int i = 0; i < sortedCards.size(); i++) {
if (sortedCards.size() == expectedCards.size()) {
if (sortedCards.get(i) != expectedCards.get(i)) {
equal = false;
}
}
}

return equal;
}

solution is :

It is possible that THIS game will be end if we occur in same position of piles again,

so if you use infinite loop it is not possible always , iterative method will be optional but it takes so much time to execute , code may be lenthy and time responsible, so recursion may be optimized.

your removeZeros method.

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