match 3 removing from ArrayList

I'm trying to create a match 3 & made a check function that works & tell me when there are 3 in a row. I tried giving each one a float but when I go to remove the letters assign to each i that met nothing happened. Can anyone tell me why? here the code for the block.

`class block{ float x,y,w,type,a,d,c; boolean first, second = false; block(float x, float y, float w, float type){ this.x = x; this.y = y; this.w = w; this.type = type; } void show(){ if (type == 0){fill(255,0,0);} if (type == 1){fill(0,255,0);} if (type == 2){fill(0,0,255);} if (type == 3){fill(255,0,255);} if (type == 4){fill(255,255,0);} rect(x,y,w,w); }

void update(){ textSize(15); for (int i = 0; i < Block.size(); i++){ block b = Block.get(i); if (b.type == type){ if (b.x == x && b.y == y){ c = i; } if (b.x + 50 == x && b.y == y){ first = true; text("1",15,15); a = i; } if (b.x + 100 == x && b.y == y && first == true){ second = true; text("2",15,30); fill(255,0,0); rect(x-100,y,150,10); d = i; } } } Block.remove(a); Block.remove(d); Block.remove(c); } }`

Tagged:

Answers

  • Please post in the new forum. Also, ensure you properly format your code the forum. As it is, your code is unreadable and it could be missing characters due to this forum's markdown.

    Kf

  • this dang site should stop messing up people's codes.

  • Well, 1000s can post code beautifully

    You can too but please in the new forum

  • The new forum is here: https://discourse.processing.org/

    To format code on the new forum, highlight the code in your post and press the </> button.

  • edited June 2018

    Remark I.

    Naming:

    • class names like Block are with a Capital: Block by convention

    • ArrayList and arrays are with a small letter and plural (holding several blocks): blocks

    you mixed that up

    Remark II.

    I would never loop over the arraylist of a class Block INSIDE that class. Because that defies the logic of a class. A class takes care of ONE block and nothing outside of it. The for loop should be in a function updateBlocks() OUTSIDE the class Block.

    Remark III.

    ok, match3... but why remove the entire data cell? Shouldn't the empty cell stay there in the grid (it still has its position but is empty)? So you shouldn't use Block.remove(a); but say something like Block.get(a).type = empty; or Block.get(a).type = 0;

    It's always better to post a runnable version of your code.

    Code

    better Formatted code without real changes

    class block {
    
      float x, y, 
        w, 
        type, 
        a, d, c;
    
      boolean first, second = false;
    
      // constr
      block(float x, float y, float w, float type) { 
        this.x = x; 
        this.y = y; 
        this.w = w; 
        this.type = type;
      } // constr 
    
      void show() { 
        if (type == 0) {
          fill(255, 0, 0);
        } else if (type == 1) {
          fill(0, 255, 0);
        } else if (type == 2) {
          fill(0, 0, 255);
        } else if (type == 3) {
          fill(255, 0, 255);
        } else if (type == 4) {
          fill(255, 255, 0);
        } 
        rect(x, y, w, w);
      }//method 
    
      void update() {
    
        textSize(15);
    
        for (int i = 0; i < Block.size(); i++) {
    
          block b = Block.get(i);
    
          if (b.type == type) { 
            if (b.x == x && 
              b.y == y) { 
              c = i;
            } 
            if (b.x + 50 == x && 
              b.y == y) { 
              first = true; 
              text("1", 15, 15); 
              a = i;
            } 
            if (b.x + 100 == x &&
              b.y == y && 
              first == true) {
    
              second = true; 
              text("2", 15, 30); 
              fill(255, 0, 0); 
              rect(x-100, y, 150, 10); 
              d = i;
            }
          }
        } 
        Block.remove(a); 
        Block.remove(d); 
        Block.remove(c);
      }//method
    }//class
    //
    
  • edited June 2018

    I would never loop over the arraylist of a class Block INSIDE that class. Because that defies the logic of a class. A class takes care of ONE block and nothing outside of it. The for loop should be in a function updateBlocks() OUTSIDE the class Block.

    Yes. Specifically, in a class Grid or Board, for example, which loops over a group of Blocks.

  • Please continue this discussion here: https://discourse.processing.org/t/match-3-remove-from-arraylist/1167/7

    @Koogs, if you are around, please close this post.

    Kf

This discussion has been closed.