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

  • Home
  • SEARCH
  • 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 3494928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:03:53+00:00 2026-05-18T12:03:53+00:00

I’m making a game for a class and one element of the game is

  • 0

I’m making a game for a class and one element of the game is displaying a number of cabbages, which are stored in an ArrayList. This ArrayList must be a fixed number of 20, 10 of Good Cabbage and 10 of Bad Cabbage.

As the cabbages are created, I want to make sure they don’t overlap when they are displayed. Where I’m running into trouble with this is that when I find a cabbage that overlaps, I’m not sure how to go back and create a new cabbage in its place. So far when the code finds an overlap, it just stops the loop. I guess I’m having trouble properly breaking out of a loop and restarting at the index that goes unfilled.

Here’s what I have so far for this. Any suggestions would be much appreciated.

        // Initialize the elements of the ArrayList = cabbages
    // (they should not overlap and be in the garden) ....
    int minX = 170 ;
    int maxX = 480;
    int minY = 15;
    int maxY = 480;
    boolean r = false;
    Cabbage cabbage;
    for (int i = 0; i < N_GOOD_CABBAGES + N_BAD_CABBAGES; i++){
        if (i % 2 == 0){
        cabbage = new GoodCabbage((int)(Math.random()* (maxX-minX + 1))+ minX,
                (int)(Math.random()*(maxY-minY + 1))+ minY,window);
        } 
        else {
            cabbage = new BadCabbage((int)(Math.random()* (maxX-minX + 1))+ minX,
                    (int)(Math.random()*(maxY-minY + 1))+ minY,window);
            }
        if (i >= cabbages.size()){
        // compares the distance between two cabbages
            for (int j = 0; j < cabbages.size(); j++){
                Point c1 = cabbage.getLocation();
                Cabbage y = (Cabbage) cabbages.get(j);
                Point c2 = y.getLocation();
                int distance = (int) Math.sqrt((Math.pow((c1.x - c2.x), 2) + Math.pow((c1.y - c2.y),2)));
                if (distance <= (CABBAGE_RADIUS*2) && !(i == j)){
                    r = true;
                }
            }
        if (r){
            break;
            }
        cabbage.draw();
        cabbages.add(i, cabbage);
        }       
    }
  • 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-05-18T12:03:53+00:00Added an answer on May 18, 2026 at 12:03 pm

    It looks like you are making cabbage objects and then throwing them away, which is a (trivial) waste.

    Why not pick the random X and Y, check if there is room at that spot, then make the cabbage when you have a good spot? You’ll just churn through numbers, rather than making and discarding entire Objects. Plus you won’t have to repeat the random location code for good and bad cabbages.

    int x, y
    do {
    // pick x and y
    } while (cabbageOverlaps(x,y,list)

    // create a cabbage at that x,y, and add it to list

    boolean cabbageOverlaps(int x, int y, ArrayList existingCabbages)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.