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

2 Answers 2

1

The easiest way to do this is probably to add another loop.

A do...while loop is suited to cases where you always need at least one iteration. Something like:

  boolean overlapped;
  do {
      // create your new cabbage here

      overlapped = /* check whether it overlaps another cabbage here */;
  } while (overlapped);

  cabbage.draw();
  cabbages.add(i, cabbage);
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.