• entries
686
1952
• views
386019

# I am teh 1

68 views

I was thinking a problem I had awhile ago with a game idea. I've always wanted to make a puzzle game, using the generic concept of matching blocks of the same color together in order to eliminate them. Problem is, I could never think of an algorithm to eliminate all of the possible matching blocks. Today I thought about it, and it seems rather simple. Take for instance, that the game has a grid of blocks. These values are stored as a struct. The struct has a boolean value to tell whether it actually is there, and an integer constant saying what color it is. So here would be the definition:
const int RED = 0;const int BLUE = 1;const int GREEN = 2;struct{   bool alive;   int color;} grid[10][10];

Now we use fun old recursion[smile]. I thought up a function that uses recursion to eliminate every block of the same color that is touching the block specified. Here would be the prototype:

void Check(int x, int y, int color);

The x and y parameters specify what block to check for. So here would be the implementation:
void Check(int x, int y, int color){    // Check to see if current block should be eliminated    if(grid[y][x].alive && grid[y][x].color == color)    {       // Eliminate block       grid[y][x].alive = false;       // Check all blocks touching this block       Check(x - 1, y, color);       Check(x + 1, y, color);       Check(x, y - 1, color);       Check(x, y + 1, color);    }}

Now I realize that one main problem with this code is that a block that is checked will refer back to a previously checked block.

Hope someone finds this useful[smile]

I had to deal with this same thing when we did "Minesweeper" in class -- when you click on a 0, it clears everything around it.

Basically, you have to come up with some way to set what you have checked as "checked", and just "return" when you are checking something that has already been checked.

Its a pretty good method.

Looking back at the code, I think the fact that it checks the previous block wouldn't be too bad. The previous block wouldn't pass the if statement, and would return after that. Hopefully that wouldn't take a big hit on the speed of the game.

You are definately teh 1! Methinks this would work for a paint-style fill function. I'll try it out in the morning and report back.

## Create an account

Register a new account