how can I check if the item to the left or right is same item as the first one.

I'm trying to make a match 3 & was wondering how to do the check to make them vanish before the clicking on them starts. I want them all to check their neighbors & up and down for the same. I was thinking of having 3 floats; float t1, t2, t3; t2 = x + 30; t3 = x + 30; if (type == type){ sq.remove(i); }

I don't think that would work, but that was how I was thinking. So is there a way to check them like that?

Answers

  • edited December 2017 Answer ✓

    Please post complete sketches. Code snippets are a poor substitute. Here is an example:

    color[] colors = { color(255, 255, 0), color(0, 0, 255), color(255, 128, 255) };
    
    class Block {
      float x, y;
      int type;
      Block(float ix, float iy) {
        x = ix; 
        y = iy;
        new_type();
      }
      void draw() {
        fill(colors[type]);
        rect(x, y, 100, 100);
      }
      void new_type() {
        type = int(random(colors.length));
      }
    }
    
    Block b0, b1, b2;
    
    void setup() {
      size(500, 300);
      rectMode(CENTER);
      b0 = new Block(100, 100);
      b1 = new Block(250, 100);
      b2 = new Block(400, 100);
    }
    
    void draw() {
      background(128);
    
      fill(255, 0, 0); 
      if ( b0.type == b1.type ) fill(0, 255, 0); 
      rect(175, 100, 100, 20);
    
      fill(255, 0, 0); 
      if ( b1.type == b2.type ) fill(0, 255, 0); 
      rect(325, 100, 100, 20);
    
      fill(255, 0, 0); 
      if ( b0.type == b1.type && b1.type == b2.type ) fill(0, 255, 0); 
      rect(250, 175, 400, 20);
    
    
      b0.draw();
      b1.draw();
      b2.draw();
    }
    
    void mousePressed() {
      b0.new_type();
      b1.new_type();
      b2.new_type();
    }
    

    Notice there are three blocks. They are call b0, b1, and b2. Since the Block class has a type variable, each instance of the Block class - and thus each of the Blocks objects - has it's own type variable.

    The type variable of a Block determines its color. There are thus three "types": Blue, Yellow, and Pink.

    Between the blocks a connection indicator is drawn. This indicates if the two Blocks are the same type. If the indicator is green, they are the same type. If the indicator is red, they are different types.

    There is also an indicator below all three blocks. This one is green only when all three blocks are the same type - otherwise it is red.

    Clicking the mouse will reassign each Block a random type.

    Notice that the colors for the indicators are being set by checking to see if the types for various Blocks are the same.

    THERE IS A LOT TO BE LEARNED FROM THIS EXAMPLE. Please make sure you fully understand how it works! If you have specific questions about it, ASK!

  • would this still work if there 25 or more blocks?

  • Answer ✓

    Yes, but hold up. How many blocks are you actually going for? When you say 25 blocks, I imagine a 5 by 5 grid of blocks:

    color[] colors = { color(255, 255, 0), color(0, 0, 255), color(255, 128, 255) };
    
    class Block {
      float x, y;
      int type;
      Block(float ix, float iy) {
        x = ix; 
        y = iy;
        new_type();
      }
      void draw() {
        fill(colors[type]);
        rect(x, y, 35, 35);
      }
      void new_type() {
        type = int(random(colors.length));
      }
    }
    
    Block[][] blocks = new Block[5][5];
    
    void setup() {
      size(500, 300);
      rectMode(CENTER);
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          blocks[i+2][j+2] = new Block(width/2 + 40 * i, height/2 + 40 * j);
        }
      }
    }
    
    void draw() {
      background(128);
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
          blocks[i][j].draw();
        }
      }
    }
    
    void mousePressed() {
      blocks[2][2].new_type();
    }
    

    Now each Block is stored in a single 2D array of Blocks. You can still access each block by its row and column - for example, I pick the middle block to get a new type when you click the mouse now.

    Now, which blocks are you trying to compare types between? Adjacent blocks? Rows? Columns? Are you looking for three blocks in a row that are the same type? Or three blocks anywhere? Or what? Give some details about what you are trying to accomplish...

    In any case, all the Blocks' types are blocks[some_row][some_col].type.

  • edited December 2017

    for (int i = 0; i < 5; i++){ for (int j = 0; j < 5; j++){ sq.add(new Square(50+(i*30), 50 +(j * 30), 15)); }}

    this the code i'musing to make them.

  • edited December 2017

    Yes, I have a couple of similar loop. They're lines 25 to 29:

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          blocks[i+2][j+2] = new Block(width/2 + 40 * i, height/2 + 40 * j);
        }
      }
    

    You still need to mention which blocks you are trying to do type comparisons between.

  • 3 blocks in a row but not diagonally. they wait till everything is stable then run a function checking the blocks around them each block checks 2 blocks in each direction. I got this code from another language so i didn't know how to transfer it to this.

  • Answer ✓

    Checks

    Okay, so here are some black bars that denote each of the three-in-a-row-s that you can check. Do you see a pattern?

    First the vertical checks are done. First the vertical checks starting on the top row. Then the next row. Then the next row. There's no point in checking the fourth row, because there are no more three in a rows to do!

    Then the horizontal checks, which follow a similar pattern.

  • nice so the code you sent will check them all that is good. Yes I do see a pattern.

  • The code I have posted demonstrates how you can access each block's type, and demonstrates comparing types. It's up to you to see the pattern and write the correct set of loops to check each possible three in a row.

    if you have some example code already that does this - which we have no idea about because you haven't posted it (HINT: POST IT) - then you can probably work it out without much trouble.

  • edited December 2017

    I asked this question just to see if possible. For your double array to make them like 6 6 or more I would just change the number in array right?

  • Answer ✓

    Yes. You might also modify the limits of various loops so that more Blocks are created/drawn.

  • ty, now to modify the gamemaker code to work here;

  • Where you placed blobk b0, b1 and b2 is that like different objects? I thought I would try your bar thing and modify it to check all and delete any that are 3 in row.

  • Answer ✓

    Yes. The class, Block, defines a new type of object. b0, b1, and b2 were different instances of that type of object. Essentially, b0 was one Block, b1 was another Block, and b2 was the third Block.

    When I switched to a 2D array, I was still using the same type of objects: Block. That is why it is a 2D array of Blocks. Each object in the array is its own Block object.

  • edited January 2018

    why aint this working?

    color[] colors = { color(255, 255, 0), color(0, 0, 255), color(255, 128, 255), color(255, 0, 0) };
    
    class Block {
      float x, y;
      int type;
      Block(float ix, float iy) {
        x = ix; 
        y = iy;
        new_type();
      }
    
      void draw() {
        fill(colors[type]);
        rect(x, y, 30, 30);
      }
    
      void new_type() {
        type = int(random(colors.length));
      }
    
      void check() { // i added this thinking it would do like your second picture, but it pops out of bound error
        for (int i = 0; i < hi; i++) {
          for (int j = 0; j < le; j++) {
            fill(255, 0, 0); 
            if ( blocks[i][j].type == blocks[i+1] [j+1].type && blocks[i+2] [j+2].type == blocks[i+1] [j+1].type ) fill(0, 255, 0); 
            rect(250, 175, 400, 20);
          }
        }
      }
    }
    
    int hi = 6;
    int le = 6;
    Block[][] blocks = new Block[hi][le];
    
    void setup() {
      size(500, 300);
      rectMode(CENTER);
      for (int i = 0; i < hi; i++) {
        for (int j = 0; j < le; j++) {
          blocks[i][j] = new Block(30 + (40 * i), 30 + (40 * j));
        }
      }
    }
    
    void check() {
      for (int i = 0; i < hi; i++) {
        for (int j = 0; j < le; j++) {
    
          fill(255, 0, 0); 
          if ( blocks[i] [j].type == blocks[i+1] [j+1].type && blocks[i+2] [j+2].type == blocks[i+1] [j+1].type ) fill(0, 255, 0); 
          rect(250, 175, 400, 20);
        }
      }
    }
    
    void draw() {
      background(128);
      for (int i = 0; i < hi; i++) {
        for (int j = 0; j < le; j++) {
          blocks[i][j].draw();
          blocks[i][j].check();
        }
      }
    }
    

    if this works I can make them have a boolean collide = false unless the line matches all 3.

    [Mod edit, code formatted]

  • https://paste.ofcode.org/ANPE6xUSnS5bYP7rPtac9H

    I tried to check collision but it keeps saying out of bounds. So why is the collision not working.

    They should really update this post thing even after using ctrl o it pasted wrong

  • Please fix your code's formatting. I refuse to see it unless you do. It hurts my eyes and my soul. @-) ~X(

  • it hurts my finger having to fix my code on a site that messes it up. I code was in great format before I posted it here. If you are programmers program a site that is good not one that messes up the format of what you post.

  • edited January 2018

    true words @admin
    otherwise it's not enough to say its a mad world we are living in, you have to DO something to make it better, in this case it would be to open a new forum where all the features are presented. but generally yes, this forum done by a non programmer noob and many users are unhappy : /
    buy a server or via crowdfunding, search for a forum software, modify it, few hours of work a thankful community is guaranteed

  • if you don't fix the code then we can't read it. if we can't read it we can't fix it.

    up to you.

  • edited January 2018

    i'm guessing i know one of the noobs who made the site it was probably koogs. I bet he sits at his computer thinking how can he make a forum to cause others greif?

  • That's just ... really, really silly.

    It has nothing to do with the volunteer mods having written the forum software -- they didn't, and the forum software is linked right there at the bottom of the page.

    The Vanilla forums method of indenting code blocks with four spaces isn't perfect or my favorite (I prefer fenced code blocks), but it isn't that hard -- or unusual. Basic Markdown does the same thing and you can find it literally everywhere. Vanilla even has a highlight shortcut to do indent all at once: Ctrl-o. That was in the formatting instructions that were linked.

    Github, Stackoverflow, and many other major sites for technical people each support their own methods of code formatting, as do WordPress plugins etc. etc., and those methods have changed over time -- there isn't one standard, you just need to do it right where you are. Currently more places are supporting three back-ticks...but that could change. If you cannot figure out how to indent even when given direction, you probably shouldn't be insulting people as noobs or griefers -- especially not the volunteers who help you.

    @Tfguy44 asked you to reformat your code after writing seven answer posts for you, and you won't press Ctrl-o because it "hurts your fingers"?

  • edited January 2018

    i did ctrl o and it messed the code up even more. when i did ctrl o it made my first line not even appear in the code it placed it above in plain. His dumb excuse was it hurt his eyes and soul.

  • After pasting the code into the forum ensure it has blank lines before and after.
    select the lines, and press Ctrl+o. (you can use the C button in the toolbar but select the lines first).
    This will indent the lines with four spaces, which is the way to mark lines as code.

    Sounds like you didn't put a blank line above and below.

Sign In or Register to comment.