• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

detecting matches

Martina

Member
Hi guys,
Ran into another problem. I've been following an online tutorial on how to make a match 3 game. I typed over te code, but it's not working for me. The guy who made this tutorial is not responding to my questions so I thought I would ask it here. What I'm trying to do is make the game start without any matches already on the board (see my code below). But no matter what I try I can't get it working. Could you please help me? Thnx!

//initialize the game

width = 6;
height = 10;

offset = 32;

xstart = 88;
ystart = 216;

dot = noone;
maxLoops = 0;

for (i=0; i < width; i++){
for (j=0; j <height; j++){
instance_create(offset * i + xstart, offset * j + ystart,obj_background_tile)
var dot = instance_create(offset * i + xstart, offset * j + ystart,obj_dots)


if(i>1 && j>1)
{
//check both left and up
while((instance_position(dot.x - offset,dot.y,obj_dots).image_index == dot.image_index
&& instance_position(dot.x - 2*offset,dot.y,obj_dots).image_index == dot.image_index)
|| (instance_position(dot.x,dot.y-offset,obj_dots).image_index == dot.image_index
&& instance_position(dot.x,dot.y-2*offset,obj_dots).image_index == dot.image_index))
//&& maxLoops < 100)
{
//change the dot's image and increase the maxLoops
dot.image_index = irandom(8);
//maxLoops ++;
}
}
else if (i > 1){
//check left
while((instance_position(dot.x - offset,dot.y,obj_dots).image_index == dot.image_index
&& instance_position(dot.x - 2*offset,dot.y,obj_dots).image_index == dot.image_index))
//&& maxLoops < 100)
{
//change the dot's image and increase the maxLoops
dot.image_index = irandom(8);
//maxLoops ++;
}
}
else if (j > 1){
//check up
while((instance_position(dot.x,dot.y-offset,obj_dots).image_index == dot.image_index
&& instance_position(dot.x,dot.y-2*offset,obj_dots).image_index == dot.image_index))
//&& maxLoops < 100)
{
//change the dot's image and increase the maxLoops
dot.image_index = irandom(8);
//maxLoops ++;
}
}
}}


alarm[0]=20;

state = "Fill";
 

Nidoking

Member
You've got whiles in fors where you should have ifs.

How do you normally detect matches? Why not just use that? My general approach is just to reset the board in a loop until it meets the condition I want, but you can just change colors if you're careful about it.
 

Martina

Member
You've got whiles in fors where you should have ifs.

How do you normally detect matches? Why not just use that? My general approach is just to reset the board in a loop until it meets the condition I want, but you can just change colors if you're careful about it.

Isn't if a statement that only checks once? But even if I change the while's to if's the code doesn't work. If I would decide to reset the board in a loop, how should I change my code? I get the feeling that the code isn't checking for matches. I have tried my other code for detecting matches but that doesn't work either. The other code:

//check matches

var dot1,dot2,dot3,dot4;

//check horizontal matches

dot1 = instance_position (x-width,y,obj_dots);
dot2 = instance_position (x+width,y,obj_dots);
if (dot1 !=noone && dot2 !=noone)
{
if(dot1.image_index==id.image_index && dot2.image_index==id.image_index)
{
id.image_alpha = .2;
dot1.image_alpha = .2;
dot2.image_alpha = .2;
}
}

//check vertical matches

dot3 = instance_position (x,y-width,obj_dots);
dot4 = instance_position (x,y+width,obj_dots);
if (dot3 !=noone && dot4 !=noone)
{
if(dot3.image_index==id.image_index && dot4.image_index==id.image_index)
{
id.image_alpha = .2;
dot3.image_alpha = .2;
dot4.image_alpha = .2;
}
}

alarm[0] = 5;
 

Nidoking

Member
something like
GML:
do
{
  generate a board
  check for matches
  if there is a match, thereisamatch = true
}
until (!thereisamatch)
 

Martina

Member
something like
GML:
do
{
  generate a board
  check for matches
  if there is a match, thereisamatch = true
}
until (!thereisamatch)
oke, I've changed my code to this, but it still won't work. The code is in the create event

//initialize the game

width = 6;
height = 10;

offset = 32;

xstart = 88;
ystart = 216;

dot = noone;
match = false;



do
{

for (i=0; i < width; i++){
for (j=0; j <height; j++){
instance_create(offset * i + xstart, offset * j + ystart,obj_background_tile)
var dot = instance_create(offset * i + xstart, offset * j + ystart,obj_dots)
}
}

//check both left and up
if((instance_position(dot.x - offset,dot.y,obj_dots).image_index == dot.image_index
&& instance_position(dot.x - 2*offset,dot.y,obj_dots).image_index == dot.image_index)
|| (instance_position(dot.x,dot.y-offset,obj_dots).image_index == dot.image_index
&& instance_position(dot.x,dot.y-2*offset,obj_dots).image_index == dot.image_index))
{
match = true;
}


//check left
if((instance_position(dot.x - offset,dot.y,obj_dots).image_index == dot.image_index
&& instance_position(dot.x - 2*offset,dot.y,obj_dots).image_index == dot.image_index))
{
match = true;
}


//check up
if((instance_position(dot.x,dot.y-offset,obj_dots).image_index == dot.image_index
&& instance_position(dot.x,dot.y-2*offset,obj_dots).image_index == dot.image_index))
{
match = true;
}
}


until (!match)

alarm[0]=20;

state = "Fill";
 

Nidoking

Member
It doesn't look like you're removing the old instances when you make new ones.

It also doesn't look like you're checking for a match up and right.
 

Martina

Member
It doesn't look like you're removing the old instances when you make new ones.

It also doesn't look like you're checking for a match up and right.
As far as I know it doesn't need to check right or up, because the right dot is already checking to the left and the above dot is already checking for the dot underneath it, but I could be mistaken.

i've changed the code to this, but it still won't work:

//intialize the game

width = 6;
height = 10;

offset = 32;
XStart = 88;
YStart = 216;

match = false;
dot = noone;

//initialize the game

width = 6;
height = 10;

offset = 32;

xstart = 88;
ystart = 216;

dot = noone;
match = false;



do
{

for (i=0; i < width; i++){
for (j=0; j <height; j++){
instance_create(offset * i + xstart, offset * j + ystart,o_BackgroundTile)
var dot = instance_create(offset * i + xstart, offset * j + ystart,o_Dot)
}
}

if(i > 1 && j > 1){
//check both left and up
if((instance_position(dot.x - offset,dot.y,o_Dot).image_index == dot.image_index
&& instance_position(dot.x - 2*offset,dot.y,o_Dot).image_index == dot.image_index)
|| (instance_position(dot.x,dot.y-offset,o_Dot).image_index == dot.image_index
&& instance_position(dot.x,dot.y-2*offset,o_Dot).image_index == dot.image_index))
{
match = true;
with(o_Dot) {instance_destroy()}
with(o_BackgroundTile) {instance_destroy()}
}
}else

if(i > 1 && j < 2){

//check left
if((instance_position(dot.x - offset,dot.y,o_Dot).image_index == dot.image_index
&& instance_position(dot.x - 2*offset,dot.y,o_Dot).image_index == dot.image_index))
{
match = true;
with(o_Dot) {instance_destroy()}
with(o_BackgroundTile) {instance_destroy()}
}
}else

if(i < 2 && j > 1){
//check up
if((instance_position(dot.x,dot.y-offset,o_Dot).image_index == dot.image_index
&& instance_position(dot.x,dot.y-2*offset,o_Dot).image_index == dot.image_index))
{
match = true;
with(o_Dot) {instance_destroy()}
with(o_BackgroundTile) {instance_destroy()}
}
}
}


until (!match)

alarm[0]=20;

state = "Fill";



alarm[0]=20;

state = "Fill";
 
Last edited:

Pixel-Team

Master of Pixel-Fu
There's a book called Actionscript 3.0 Game Programming University by Gary Rosenweig which lays out the match3 algorithms perfectly. I've implemented it in Flash, as well as IOS from this book. You may be able to find a tutorial elsewhere, but this has helped me tremendously.
 

Martina

Member
There's a book called Actionscript 3.0 Game Programming University by Gary Rosenweig which lays out the match3 algorithms perfectly. I've implemented it in Flash, as well as IOS from this book. You may be able to find a tutorial elsewhere, but this has helped me tremendously.
thnx, I will take a look at it
 
Top