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 8411667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:28:45+00:00 2026-06-10T00:28:45+00:00

I think I have been staring at this code too long and I am

  • 0

I think I have been staring at this code too long and I am not seeing it. I am writing a small game, in which part of it generates a number of objects (currently shapes). As the shapes are created, they are added to an ArrayList as objects defined by a class. I noticed that as the number of shapes increases, the chance of overlapping shapes increases, which I want to fix. I am trying to do so by checking the Y coordinate against the Y coordinates of each of the existing objects in the ArrayList. But for the life of me I cannot remember the way to keep iterating the check function until I generate Y value that is unique.

My initial solution is a do…while loop, but for some reason it keeps returning true.

I simplified my code a bit, just keeping the parts essential to my question. What am I missing? Thanks in advance for any help.

Edit: Also keep in mind that objects are getting added to and removed from the ArrayList constantly as the game progresses. Every time an object (shape) is removed during the game, it is regenerated. This means that for every object being created during the game, it needs to be checked against the existing objects.

int x, y;
int numShapes = 10;
ArrayList<ShapeObject> soArray = new ArrayList<ShapeObject>();


private boolean checkY(int yy) {
  for(int i = 0; i < soArray.size(); i++) {
    if(soArray.get(i).oY == yy) {
      return true;
    }
  }
  return false;
}


public void makeShapes() {
  for(int i = 0; i < numShapes; i++) {
    if(soArray.size() < numShapes) {
      x = rand.nextInt(width - 45) + 45;

      do {
        y = rand.nextInt(height - 4) + 2;
      } while(checkY(y));

      soArray.add(new ShapeObject());
    }
  }
}

class ShapeObject {
  int oX = x;
  int oY = y;
}
  • 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-10T00:28:46+00:00Added an answer on June 10, 2026 at 12:28 am

    Since I basically figured out the issue on my own, I am posting it as an answer.

    It turns out that I forgot I was restricted by the bounds of my canvas. My cell size is 12px on a grid of 600px. I noticed that it would fail after 46 objects in my ArrayList. So basically all of the available Y coordinates were taken. I was testing with 100 objects so it just kept running.

    I also modified the checkY() to be checkXY() so that I could completely control the number of rows and columns (x and y coordinates) that are available for objects.

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

Sidebar

Related Questions

I have been trying to get this to work correctly and I think I
This has been driving me crazy. I think I have everything in place but
So i have been creating this framework thing that basically puts together source code
I've been thinking about this far too long and haven't gotten any idea, maybe
Sorry if this is too obvious but I’ve been too long on it and
I have been using code similar to this MessageDlg('', mtWarning, [mbOK], 0); throughout my
I have been writing code like the following enough a lot lately. I don't
I think it might have been asked before but i was unable to find
I have been struggling to think of some decent uses for things like vectors
I have been having the following problem, i think it's probably due to 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.