Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:28:11+00:00 2026-06-13T11:28:11+00:00

I the following assignment due in a few days in my AP Computer Science

  • 0

I the following assignment due in a few days in my AP Computer Science course:

“In this assignment you will model the game of Bulgarian Solitaire. The game starts with 45 cards. Randomly divide them into some number of piles of random size. For example, you might start with piles of size 20, 5, 1, 9, and 10. 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 19, 4, 8,10, and 5. The solitaire is over when the piles have size 1, 2, 3, 4, 5, 6, 7, 8, and 9, in some order.

In your program, produce a random starting configuration and print it. Then keep applying the solitaire step and print the result. Stop when the solitaire final configuration is reached.”

I have come up with a program that solves this, but the problem is that it takes a VERY long time sometimes. Other times it will solve it almost instantly, as I expected, but other times it can 18,000 iterations or more.

According to http://mancala.wikia.com/wiki/Bulgarian_Solitaire the solution can be found in (k^2)-k steps or less, with k being 9 in this case. I’m definitely not finding the solution in 72 steps or less a lot of the time. I’ve been looking at this program for hours, messing with different things in order to see if I can make it faster and I just cannot get it working in a sufficient number of iterations. So now I’ve come to Stack Overflow to see if any of you can help to push me in the right direction.

Here is my code:

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;
    }
}

So I basically start by generating a random number between 0 and 45 and add that to the list of cards. Then I continue generating random numbers and putting them in the list as long as the sum is less than 45, such that the random numbers generated are between 0 and 45-the sum of the numbers in it in the last iteration. The zeros in the list are removed as it goes along as well.

Once the list has been generated, it will run through the step of subtracting 1 from every number in the list, removing zeros and adding a new value equal to the number of stacks that were decreased. It also checks an ordered version of the card stack against the list {1, 2, 3, 4, 5, 6, 7, 8, 9} and once it finds a match, it sets the boolean cont2 to false so that it will stop performing that solitaire step.

That’s about it. I give my thanks to anyone that can help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:28:12+00:00Added an answer on June 13, 2026 at 11:28 am

    Your flaw lies in your removeZeros method.

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

    If you remove the element at j, then the list size decreases by 1. You have to decrease j, too.

    Change that for this:

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

    Your checking method is also overly complicated.

    In your solitaire step, set all of the values that should be equal to zero, to zero.

    Then, remove zeros (with the revised method) outside of the loop.

    Then, sort the array.

    Then, in your checking method, since the array is sorted:

    public boolean checkCards() {
        for(int i = 0; i < cards.size(); i++) {
            if(cards.get(i) != i + 1) {
                return false;
            }
        }
        return true;
    }
    

    Much simpler. And it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For an assignment, I had to code a due date which is 30 days
Given this model in legacy code, with RoR 2.3.11: class Assignment < ActiveRecord::Base belongs_to
What is the meaning of the following assignment, please? var self = this, textDropZone;
I have the following assignment: Implement a basic shopping basket without using any predefined
I have the following assignment: Write a complete 8086 program to perform the calculator
I got a segmentation fault when invoked a function immediately following a pointer assignment.
The following code is returning an expression unused warning on the assignment operation in
For the following code snippets why would I use one assignment vs another? thx
In my assignment i have a problem with reading a file. See the following
For a programming assignment, we have the following requirements: It needs to be a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.