int block_type = Block_Well[x][y];
//checks across
if( (Block_Well[x+1][y] == block_type) && (Block_Well[x+2][y] == block_type) && (Block_Well[x+3][y] == block_type) && (x<7) )
{
//Delete the Blocks that matched in here
}
Does anyone have any suggestions or ideas on how I could loop through the array and with some sort of algoritm check for at least 4 matching blocks which will then be set for deletion? Or am I better off just righting out all the various checks like above?
thanks.
Need a hand w/ Matching Blocks Algorithm
For the puzzle game I am making which is similar to dr.mario/tetris where you drop varied colored blocks and have to match a certain number....I have it match at least 3 blocks before they *disappear*, I don''t use diagnols so I basically check all the possible matches, which comes out to only 6 block arrangments.
To get to the pnt...I have been trying to add in capablities to check for at least four matching blocks to increase the difficulty, but the number of arrangments is a great deal larger then simply 3 matches...so I am trying to come up with some simple algortim to check for the matches, instead of doing this for each and every type of match:
The array which searched for matches is [10][8]
One idea is to use a "flood-fill" like recursion to move between blocks, invalidating them (and moving onward) as long as they are still the same type as the original. I think the code explains it better. Here goes:
--Edited-- after reviewing this I realized that it would fail when enough adjacencies weren't found, so here's the minor fix
The idea is to set your Block_Well to some marker value whenever a similar block has been reached. At the end, you can cleanup these guys.
My apologies if this doesn't work. I haven't tested it or anything, but in my experience recursion is ideally suited for searches of this nature.
Good luck!
Edited by - byondo on October 17, 2000 9:16:28 AM
// assumes globals Block_Well// array boundaries MAX_X, MAX_Y// marks guys that match, returns number markedint match(int block_type, int x, int y) { if(x<0 || y<0 || x>=MAX_X || y>=MAX_Y) return 0; // out of bounds if(Block_Well[x][y]!=block_type) return 0; // wrong type // we got a match! Mark it accordingly Block_Well[x][y] *= -1; // mark it in a way that allows us to restore it later if necessary // recursively descend the tree to match adjacent guys return 1 + // add me to the total count match(block_type,x-1,y) + match(block_type,x+1,y) + match(block_type,x,y-1) + match(block_type,x,y+1);}// run your check at point (x,y)void checkadjs(x,y){ int ct = match(Block_Well[x][y],x,y) if(ct>=4) { // or whatever you want for(x=0;x<MAX_X;x++) { for(y=0;y<MAX_Y;y++) { if(Block_Well[x][y] < 0) { // delete this block } } } } else { // restore marked blocks for(x=0;x<MAX_X;x++) { for(y=0;y<MAX_Y;y++) { if(Block_Well[x][y] < 0) { Block_Well[x][y] *= -1; } } }}
--Edited-- after reviewing this I realized that it would fail when enough adjacencies weren't found, so here's the minor fix
The idea is to set your Block_Well to some marker value whenever a similar block has been reached. At the end, you can cleanup these guys.
My apologies if this doesn't work. I haven't tested it or anything, but in my experience recursion is ideally suited for searches of this nature.
Good luck!
Edited by - byondo on October 17, 2000 9:16:28 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement