Game Development Community

Recursion problem

by Scott Jaworski · in Torque Game Builder · 03/21/2006 (11:38 am) · 1 replies

This is more a problem with logic i think, than with TorqueScript, but I didn't know where else to post it. I've created a simple "Collapse" style game and almost have it working. I'm at a point where I'm trying to stop it from allowing me to click on a single block. I can click groups all day long, but unfortunately I can click on single pieces and the same funcationality occurs (nothing should happen when I click on a single block with no surrounding like colors). Here's what I have:

%count=0;
FindMatches(%lookx, %looky, $board[%lookx, %looky].value, %lookx, %looky);


function FindMatches(%lookx, %looky, %value, %masterx, %mastery)
{
if($debugMsg::mouse::callBacks)
echo("I am working:" SPC %lookx SPC %looky SPC $board[%lookx, %looky].value);

//if the spot we're checking is the same value as the initial spot clicked
if ($board[%lookx, %looky].value == %value)
{

//if the spot we're checking is different from the spot we started on
//increment the counter; we've got a block to remove
if ((%lookx != %masterx) || (%looky != $mastery)) $count++;

//if the counter has been incremented, clear this spot out
if ($count > 0)
{
$board[%lookx, %looky].value = -1;
$board[%lookx, %looky].setImageMap(circlewhiteImageMap);
}


//rinse and repeat for the surrounding tiles
if (%lookx > 0) FindMatches(%lookx-1, %looky, %value, %masterx, %mastery);
if (%lookx < 18) FindMatches(%lookx+1, %looky, %value, %masterx, %mastery);
if (%looky > 0) FindMatches(%lookx, %looky-1, %value, %masterx, %mastery);
if (%looky < 12) FindMatches(%lookx, %looky+1, %value, %masterx, %mastery);

}
else { }
}

%lookx and %looky are the x and y coordinates on the board where I've clicked (this all works as it should). Basically, I think something's wrong with my logic in figuring out how to increment the counter. Here's how I read the code above, someone tell me if i'm not reading something right...

I find a spot to find matches for, I send the starting x and y, the value i'm searching for matches for and what will ultimately become a master x and y (when I'm resursively calling this function from other spaces). So I click on spot 5, 5 and it's a red block. I send 5, 5, 0 (red), 5, 5. I first call FindMatches and 5, 5's value equals the value I sent (0) so I do the then clause. I'm not in a different spot than I started and the count isn't > 0, so i skip down and start calling myself with the left spot. That does match as well and it IS a different location, so I increment count and change the value and graphic for the left spot. I go left again and don't find a match, I go right (where I started from) and this now matches and count is >0 (since it's global it stays the same through iterations, right? So then this tile is emptied and life continues through the right, upper and lower spaces.

Typing that out, the only problem I can see is that count isn't staying through recursive calls to this function and something's definitely wrong because when I click on single tiles, life is good; when I click on groups, the game and editor crash. If the problem is my count variable, how do I achieve what I'm trying to do?

thanks.

#1
03/21/2006 (10:08 pm)
The first thing that I see that might be causing you problems is that you don't reset $count back to zero. If you look at the first line of your code, you set a local variable '%count' to zero. You should put '$count = 0' there instead.

It does look like you have the infinite recursion that you mention, too. The problem arises because you don't set the %masterx and %mastery value to '-1'. The recursion will "wiggle" back and forth. I think something like the following might work better (WARNING, I DIDN'T EVEN TRY THIS, LIKELY IS BUGGY):

%lookValue = $board[%lookx,%looky].value;
$count = 0;
FindMatches( %lookx, %looky, %lookValue );
if( $count == 1 ) // Only Original Matched
{
  $board[%lookx,%looky].value = %lookValue;
  $board[%lookx, %looky].setImageMap(/*RESET IMAGE MAP HERE*/);
}

// NEVER EVER EVER PASS -1 FOR %value TO THIS FUNCTION!
function FindMatches(%lookx, %looky, %value )
{
  //if the spot we're checking is the same value as the initial spot clicked
  if ($board[%lookx, %looky].value == %value) 
  {
    $count++;
    $board[%lookx, %looky].value = -1;
    $board[%lookx, %looky].setImageMap(circlewhiteImageMap);
    //rinse and repeat for the surrounding tiles
    if (%lookx > 0) FindMatches(%lookx-1, %looky, %value);
    if (%lookx < 18) FindMatches(%lookx+1, %looky, %value);
    if (%looky > 0) FindMatches(%lookx, %looky-1, %value);
    if (%looky < 12) FindMatches(%lookx, %looky+1, %value);
  }
}