I’m creating a random item generator from an array list. When I get down to an empty list, I want a ‘gameOver’ function to run, but I get an Array Index Out of Bounds error. I’m wondering if I have a variable issue when the list is empty. I’m posting all the code here (sorry, first post, don’t know if there’s a better way to do this):
StringList names;
String [] list = {"apple","orange","grape","banana","lemon"};
String item;
int s = 1;
int i;
int x;
void setup(){
size(300,300);
names = new StringList(list);
textAlign(CENTER);
textSize(60);
fill(255);
background(0);
}
void randomSelect(){
int s = names.size();
print("(" + s + ") ");
println(list);
int x = int(random(s));
item = names.get(x);
println("Name Removed: " + item);
fill(random(255),random(255),random(255));
text(item, width/2,height/2);
names.remove(x);
s = s - 1;
String[] newList = names.array();
list = newList;
print("(" + s + ") ");
println(list);
println();
}
void mouseClicked(){
background(0);
if(s > 0){
randomSelect();
}else{
gameOver();
}
}
void gameOver(){
background(0);
textSize(35);
text("Out of Names", width/2,height/2);
}
void draw(){
}
I’m sure that someone will tell you to format your code, you can do that by highlighting your code and pressing Control + Shift + C. What I did was add to your code:
You’re getting an array index out of bound error, because when you remove the last item in your list, and then you try removing the object at the index of x, there is nothing to be removed. And then I just had an if statement say that if the size is zero, then to show your gameOver() function.
Hopefully this helps? If you have any more questions I would love to help, and please don’t mind keeping us updated on your project. (P.S. Do not forget to format your code next time )
Good to point out!
I remember there was a thread last week about an implementation of the game of life where the behavior was weird because of that shallow copy.
Great help. Clone works well, but I wasn’t able to make arrayCopy work. I did patch together a way to get it to restart. But it only runs one more time, so something is amiss.
Here something that is working with explanations. Hope it helps:
StringList originalFruits; // Will always contain the list of all the fruits
StringList fruits; // Will be the array that we use to get a new random fruit
void setup(){
// Initializing both list
originalFruits = new StringList("apple","orange","grape","banana","lemon");
fruits = originalFruits.copy(); //It works because we are using string, It would't with custom classes
}
void randomSelect(){
int idx = (int)random(fruits.size()); // Get a random index from 0 to the size of the array
println(fruits.get(idx)); // Write in the console the name of the random picked fruit
fruits.remove(idx); // Remove that fruit from the list since we dan't want to pick it again
}
void mouseClicked(){
if(fruits.size() > 0) { // While there is still some fruit to pick we want to pick one
randomSelect(); // So we pick one
} else { // On the other case it means that we picked everything
println("GAME OVER"); // So we are game over
fruits = originalFruits.copy(); // And we need to fill the list of fruit again
}
}
void draw(){
}