A flood-it example [GAME]

Here is the Floodit game. Your goal is to fill all of the squares to the same color. The default number of colors is 3. Press the mouse on the square you want to fill and it will automatically tag (with a white square) the selected squares. Whenever you feel like you chose the right color (you can either click on the colorful rectangles at the bottom of the window or by pressing keys 0 to 9 (maximum number is the ceil(clrs)) and yeah. Press enter to reshuffle and ā€˜cā€™ to check if you won (Idk why I added it)

//flood it game
ArrayList<Integer> selection = new ArrayList<Integer>();
int w = 20, h = 20, clr = 0;
float scl, clrs = 3;
int grid[][] = new int[w][h];

void setup() {
  size(600, 700);
  scl = width/w;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      grid[i][j] = floor(random(clrs));
    }
  }
  colorMode(HSB);
}

void draw() {
  background(0);
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      fill(map(grid[i][j], 0, ceil(clrs), 0, 255), 255, 255);
      rect(i*scl, j*scl, scl, scl);
      fill(255);
      if (selection.contains(i+j*w)) {
        rect((i+0.25)*scl, (j+0.25)*scl, scl/2, scl/2);
      }
    }
  }
  for (int i = 0; i < ceil(clrs); i++) {
    fill(map(i, 0, ceil(clrs), 0, 255), 255, 255);
    //rect(0,h*scl,width/ceil(clrs),height - h*scl);
    rect(width/ceil(clrs)*i, h*scl, width/ceil(clrs), height-h*scl);
  }
}

void mousePressed() {
  if (mouseY < h*scl) {
    selection.clear();
    beginSelection( floor(map(mouseX, 0, w*scl, 0, w)), floor(map(mouseY, 0, h*scl, 0, h)));
  } else {
    clr = floor(map(mouseX, 0, width, 0, ceil(clrs)));
  }
}

void keyPressed() {
  if (key == ' ') {
    for (int i = 0; i < selection.size(); i++) {
      int gx = selection.get(i) % w, gy = floor((selection.get(i) - gx)/w);
      grid[gx][gy] = clr;
    }
    selection.clear();
  } else if (key == ENTER) {
    selection.clear();
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        grid[i][j] = floor(random(clrs));
      }
    }
  } else if (key == 'c') {
    boolean finished = true;
    for(int i = 0; i < w; i++) {
      for(int j = 0; j < h; j++) {
        if(grid[i][j] != grid[0][0]) {
          finished = false;
        }
      }
    }
    println( ((finished)? "You have finished" : "There are still spots to fill"));
  } else {
    if(clrs <= 9) {
      for(int i = 1; i < ceil(clrs)+1; i++) {
        if(key == (i+"").charAt(0)) {
          clr = i-1;
          println(clr);
        }
      }
    }
  }
}

void beginSelection(int x, int y) {
  selection.add(x+y*w);
  while (doStuff() > 0);
  println(selection);
}

int doStuff() {
  int selected = 0;
  for (int i = 0; i < selection.size(); i++) {
    int gx = selection.get(i) % w, gy = floor((selection.get(i) - gx)/w);
    if (gx > 0) {
      if (grid[gx][gy] == grid[gx-1][gy] && !selection.contains( (gx-1)+gy*w) ) {
        selection.add((gx-1)+gy*w);
        selected++;
      }
    }
    if (gx < w-1) {
      if (grid[gx][gy] == grid[gx+1][gy] && !selection.contains( (gx+1)+gy*w) ) {
        selection.add((gx+1)+gy*w);
        selected++;
      }
    }
    if (gy > 0) {
      if (grid[gx][gy] == grid[gx][gy-1] && !selection.contains( (gx)+(gy-1)*w)) {
        selection.add((gx)+(gy-1)*w);
        selected++;
      }
    } 
    if (gy < h-1) {
      if (grid[gx][gy] == grid[gx][gy+1] && !selection.contains( (gx)+(gy+1)*w)) {
        selection.add((gx)+(gy+1)*w);
        selected++;
      }
    }
  }
  return(selected);
}

here is the program

1 Like