Sure, just write some simple code that makes sure newCoord is not already in mAppleList... put it right after the check for colliding with the snake location.
---------------------------------------------------------------------- There are only 10 types of people in the world... Those who know binary and those who don't. ---------------------------------------------------------------------- On Fri, Feb 26, 2010 at 1:40 PM, Mike <[email protected]> wrote: > > My first post here...hello! > > Can anyone tell me what is to prevent the Snake demo from putting 2 > apples in the same location? > When the game starts, it populates 2 apples. > > Here is the relevant method; > > private void addRandomApple() { > Coordinate newCoord = null; > boolean found = false; > while (!found) { > // Choose a new location for our apple > int newX = 1 + RNG.nextInt(mXTileCount - 2); > int newY = 1 + RNG.nextInt(mYTileCount - 2); > newCoord = new Coordinate(newX, newY); > > // Make sure it's not already under the snake > boolean collision = false; > int snakelength = mSnakeTrail.size(); > for (int index = 0; index < snakelength; index++) { > if (mSnakeTrail.get(index).equals(newCoord)) { > collision = true; > } > } > // if we're here and there's been no collision, then we > have > // a good location for an apple. Otherwise, we'll circle > back > // and try again > found = !collision; > } > if (newCoord == null) { > Log.e(TAG, "Somehow ended up with a null newCoord!"); > } > mAppleList.add(newCoord); > } > > -- > You received this message because you are subscribed to the Google > Groups "Android Beginners" group. > > NEW! Try asking and tagging your question on Stack Overflow at > http://stackoverflow.com/questions/tagged/android > > To unsubscribe from this group, send email to > [email protected]<android-beginners%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-beginners?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en

