Randomizing levels

I’m trying to code a game in which clicking ‘play’ would result in the player getting a random level out of a few set levels. However, the problem is that the levels have to be non-repeating. Does anyone know how to achieve something like this?

1 Like

using the arraylist might help:

IntList levels;
int maxi = 5, use;

void setup() {
  levels = new IntList();                             // makes a empty list
  for ( int i = 0; i < maxi; i++) levels.append(i);   // fills it with ascending numbers
  //println(levels);
  levels.shuffle();                                   // makes it random
  println(levels);  
  use_next();
}

void draw() {
}

void keyPressed() {
  if ( key == ' ' ) use_next();
}

void use_next() {
  if ( levels.size() > 0 ) {
    use = levels.get(0);                               // use the first one
    levels.remove(0);                                  // and delete it from the list
    println("use: "+use+" array: "+levels);
  }
}

2 Likes