Shift cells down to fill gaps?

This is the function I have to remove black cells and shift the rest down. Right now it is just moving down one row.

 void change(int i, int j){
        if(cellColor==#000000){
          for(f = 1; f < j; f++){
            color co = bubbleArray[i][f-1].getColor();
            if(co==#ffffff){
              cellColor = #ffffff;
              newBub(f,cellColor);
              visited=false;
            }else if(co==#ff0000){
              cellColor = #ff0000;
              newBub(f,cellColor);
            }else if(co==#00ff00){
              cellColor = #00ff00;
              newBub(f,cellColor);
            }else if(co==#0000ff){
              cellColor = #0000ff;
              newBub(f,cellColor);
            }else if(co==#ffff00){
              cellColor = #ffff00;
              newBub(f,cellColor);
            }else if(co==#000000){
              newBub(f,#00ffff);
            }
          }
        }
      }
Tagged:

Answers

  • I have to turn this in very soon so it would be very helpful if someone could just look through it and point out anything they notice. Ive been working so long there could easily be something i've missed

  • I suspect that you want to remove the check on line 2.

    But this is not enough code to diagnose your issue.

    Post it all.

  • I can't remove the check because then it removes parts of all of the rows, not just the ones I want it to. Here is the rest of the code, there are other unfinished areas but I just want help on the row shifting.

    Bubble[][] bubbleArray;
    int size = 35;
    int score = 0;
    void setup(){
      size(800,1030);
      background(#ffffff);
      bubbleArray = new Bubble[22][27];
      for(int i = 0; i < 22; i++){
        for(int j = 0; j < 27; j++){
          bubbleArray[i][j] = new Bubble(i*size,j*size+60,i,j,false);
          bubbleArray[i][j].randColor();
          bubbleArray[i][j].drawBub();
        }
      }
      drawRect();
    }
    void draw(){
      for(int i = 1; i < 21; i++){
        for(int j = 1; j < 26; j++){
          bubbleArray[i][j].mousePressed();
          bubbleArray[i][j].select();
          //bubbleArray[i][j].change(i,j);
        }
      }
    }
    void keyPressed(){
      if(key==ENTER){
        for(int i = 1; i < 21; i++){
          for(int j = 1; j < 26; j++){
            bubbleArray[i][j].change(i,j);
          }
        }
      }
    }
    void drawRect(){
      stroke(#000000);
      fill(#ffffff);
      rect(35,20,300,50);
      textSize(32);
      fill(#000000);
      text(score,3__5,57);
    }
    class Bubble{
      int x,y;
      int i,j;
      int rr;
      color cellColor;
      int total;
      boolean visited = false;
      int h,k,f;
      int group,newpoints;
      int white;
      Bubble(int tempx, int tempy, int tempi, int tempj, boolean tempVisited){
        x = tempx;
        y = tempy;
        i = tempi;
        j = tempj;
        visited = tempVisited;
      }
      void mousePressed(){
        if(mousePressed){
          if(mouseX>x && mouseX<x+size && mouseY>y && mouseY<y+size){
            bubbleArray[i][j].check(i,j);
            drawRect();
          }
        }
      }
      int getrr(){
        return rr;
      }
      void changeV(){
        visited=true;
      }
      boolean getV(){
        return visited;
      }
      color getColor(){
        return cellColor;
      }
      void changeVF(){
        visited=false;
      }
      void randColor(){
        float r = random(1,4);
        rr = round(r);
        if(rr==1){
          cellColor = #ff0000;
        }else if(rr==2){
          cellColor = #00ff00;
        }else if(rr==3){
          cellColor = #0000ff;
        }else if(rr==4){
          cellColor = #ffff00;
        }
        if(i==0 || j==0 || i==21 || j==26){
          cellColor = #ffffff;
          rr = 0;
        }
      }
      void drawBub(){
        ellipseMode(CORNER);
        fill(cellColor);
        stroke(#000000);
        if(cellColor==#ffffff){
          noStroke();
        }
        ellipse(x,y,size,size);
      }
      void check(int a, int b){
        bubbleArray[a][b].changeV();
        if(bubbleArray[a-1][b].getColor()==cellColor && bubbleArray[a-1][b].getV()==false){
          h=a-1;
          k=b;
          bubbleArray[h][k].changeV();
          total++;
          //println(h,k,total);
          check(h,k);
        }
        if(bubbleArray[a][b-1].getColor()==cellColor && bubbleArray[a][b-1].getV()==false){
          h=a;
          k=b-1;
          bubbleArray[h][k].changeV();
          total++;
          //println(total);
          check(h,k);
        }
        if(bubbleArray[a+1][b].getColor()==cellColor && bubbleArray[a+1][b].getV()==false){
          h=a+1;
          k=b;
          bubbleArray[h][k].changeV();
          total++;
          //println(total);
          check(h,k);
        }
        if(bubbleArray[a][b+1].getColor()==cellColor && bubbleArray[a][b+1].getV()==false){
          h=a;
          k=b+1;
          bubbleArray[h][k].changeV();
          total++;
          //println(total);
          check(h,k);
        }
        group = total+1;
        score(group);
      }
      void checkcol(){
        if(j<26 && i>0){
          if(bubbleArray[i][j+1].getColor()==#ffffff){
            white++;
            j = j+1;
            checkcol();
          }
          if(white==25){
            int co = bubbleArray[i-1][j].getColor();
            if(co==#ffffff){
              cellColor = #ffffff;
              newBub(f,cellColor);
              visited=false;
            }else if(co==#ff0000){
              cellColor = #ff0000;
              newBub(f,cellColor);
            }else if(co==#00ff00){
              cellColor = #00ff00;
              newBub(f,cellColor);
            }else if(co==#0000ff){
              cellColor = #0000ff;
              newBub(f,cellColor);
            }else if(co==#ffff00){
              cellColor = #ffff00;
              newBub(f,cellColor);
            }else if(co==#000000){
              newBub(f,#00ffff);
            }
          }
        }
      }
      void select(){
        if(visited){
          cellColor = #000000;
          drawBub();
          rr=5;
        }
      }
      void newBub(int f, color cellColor){
        ellipseMode(CORNER);
        fill(cellColor);
        stroke(#000000);
        if(cellColor==#ffffff){
          noStroke();
        }
        ellipse(x,f*size+60,size,size);
      }
      void change(int i, int j){
        if(cellColor==#000000){
          for(f = 1; f < j; f++){
            color co = bubbleArray[i][f-1].getColor();
            if(co==#ffffff){
              cellColor = #ffffff;
              newBub(f,cellColor);
              visited=false;
            }else if(co==#ff0000){
              cellColor = #ff0000;
              newBub(f,cellColor);
            }else if(co==#00ff00){
              cellColor = #00ff00;
              newBub(f,cellColor);
            }else if(co==#0000ff){
              cellColor = #0000ff;
              newBub(f,cellColor);
            }else if(co==#ffff00){
              cellColor = #ffff00;
              newBub(f,cellColor);
            }else if(co==#000000){
              newBub(f,#00ffff);
            }
          }
        }
        checkcol();
      }
      void score(int total){
        newpoints = total;
        score = score + newpoints;
        println(newpoints,score);
      }
    }
    
  • edited July 2015 Answer ✓

    Whoa. Ok. So that actually runs (after removing the __ on line 41).

    Great start!

    Here, I cleaned it up a bit.

    Bubble[][] bubbleArray;
    int rows = 27;
    int cols = 22;
    int size = 15; // You can change back.
    int score = 0;
    color[] colors = {
      color(255), color(0), color(255, 0, 0), color(0, 255, 0), color(255, 255, 0), color(0, 0, 255)
    };
    int g_pop_count = 0;
    
    void setup() {
      size(400, 515); // You can change back.
      background(#ffffff);
      bubbleArray = new Bubble[cols][rows];
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          bubbleArray[i][j] = new Bubble(i*size, j*size+60, i, j, false);
          bubbleArray[i][j].whiteBorder();
        }
      }
      drawRect();
    }
    void draw() {
      background(255);
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          bubbleArray[i][j].draw();
        }
      }
      drawRect();
    }
    
    void mousePressed() {
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          bubbleArray[i][j].mousePressed();
        }
      }
      for (int i = 0; i < cols; i++) {
        for (int j = rows-1; j >=0; j--) {
          bubbleArray[i][j].black_remove();
        }
      }
      score += g_pop_count;
    }
    
    void drawRect() {
      stroke(#000000);
      fill(#ffffff);
      rect(35, 20, 300, 50);
      textSize(32);
      fill(#000000);
      text(score, 35, 57);
    }
    
    class Bubble {
      int x, y;
      int i, j;
      int type;
      Bubble(int tempx, int tempy, int tempi, int tempj, boolean tempVisited) {
        x = tempx;
        y = tempy;
        i = tempi;
        j = tempj;
        // 0 is white border, 2 is black replacementable.
        type = int(random(2, colors.length)); 
      }
      void mousePressed() {
        if (mouseX>x && mouseX<x+size && mouseY>y && mouseY<y+size) {
          pop(true);
        }
      }
      void whiteBorder() {
        if (i==0 || j==0 || i==cols-1 || j==rows-1) {
          type = 0;
        }
      }
      void draw() {
        if(type==0) return;
        ellipseMode(CORNER);
        fill(colors[type]);
        stroke(0);
        ellipse(x, y, size, size);
      }
    
      void pop(boolean reset) {
        // White and black ones do not pop.
        if (type==0 || type == 1) return;
        // What was this one?
        int i_was_type = type;
        // It is black now.
        type = 1;
        // CLEAR THE COUNTER!
        if (reset) g_pop_count = 0;
        // Pop ones next to me that matched.
        // Note that white and black ones will not pop, so this ends eventually.
        if (i_was_type == bubbleArray[i-1][j].type ) { 
          bubbleArray[i-1][j].pop(false);
        } 
        if (i_was_type == bubbleArray[i+1][j].type ) { 
          bubbleArray[i+1][j].pop(false);
        } 
        if (i_was_type == bubbleArray[i][j-1].type ) { 
          bubbleArray[i][j-1].pop(false);
        } 
        if (i_was_type == bubbleArray[i][j+1].type ) { 
          bubbleArray[i][j+1].pop(false);
        } 
        // I popped!
        g_pop_count++;
      }
    
      void black_remove() {
        // Got to replace the black spots only.
        if (type != 1 ) return;
        int lookAt = 1;
        // Keep looking until we find a non-black one.
        while (type == 1) {
          type = bubbleArray[i][j-lookAt].type;
          lookAt++; // Got to move this up the line.
        }
        // Found one! Was assured to find one... at least a white one.
        if (type == 0) {
          // Oh crud I copied a white!
          // Quick, be random.
          type = int(random(2, colors.length));
        } else {
          // They have fallen to be me. Now they are empty.
          bubbleArray[i][j-lookAt+1].type = 1;
        }
      }
    }
    
  • wow thank you so much!

Sign In or Register to comment.