Ideas for creating a part of a game.
Ok, I''m pretty new at game programming, but not programming in general. I''ve decided for my first game to try a minesweeper-type game. I know how to get it all working except the part where it is blank under a square (no number or bomb) and when clicked, all other squares next to it disappear until squares with numbers under them disappear, then it stops. What kind of algorithm should I be using?
I''d try a recursive function that works like this..
Let''s say you have a function to clear a tile when clicked..for the clear tiles you should have something like this:
//assuming you know it''s clear before you pass it
void clear_tile(int x, int y)
{
//do whatever to clear this tile
blablabla
//now check ALL the surrounding tiles,
//my bad psuedocode checks only one
if(tiles[x+1][y].mines == 0)//or whatever
{
clear_tile(x+1,y);//call this same function
}
//etc.
}
Here''s what happens..
When you process a tile, it checks ALL of the surrounding tiles..if any of them are clear, the function is called recursively and THAT tile checks all of it''s surrounding tiles..
BEWARE!
You''ll need some sort of flag to tell you if a tile has already been cleared, otherwise the recursion will go on forever, because one will recurse for it''s neighbor, and THAT one will try to recurse on the original, etc.
If you need help with recursion(kind of a heavy subject)feel free to email me, I can save you some pain![](smile.gif)
"Like all good things, it starts with a monkey.."
Let''s say you have a function to clear a tile when clicked..for the clear tiles you should have something like this:
//assuming you know it''s clear before you pass it
void clear_tile(int x, int y)
{
//do whatever to clear this tile
blablabla
//now check ALL the surrounding tiles,
//my bad psuedocode checks only one
if(tiles[x+1][y].mines == 0)//or whatever
{
clear_tile(x+1,y);//call this same function
}
//etc.
}
Here''s what happens..
When you process a tile, it checks ALL of the surrounding tiles..if any of them are clear, the function is called recursively and THAT tile checks all of it''s surrounding tiles..
BEWARE!
You''ll need some sort of flag to tell you if a tile has already been cleared, otherwise the recursion will go on forever, because one will recurse for it''s neighbor, and THAT one will try to recurse on the original, etc.
If you need help with recursion(kind of a heavy subject)feel free to email me, I can save you some pain
![](smile.gif)
"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."
Just thought I''d mention I came up with this for a paint program(I''m sure it''s some standard algorithm, I don''t know)..
I used it to do the paint-can thing like you see in MS Paint..
When you clicked in the graphic, I''d check the color of that pixel..then I would start from that pixel, and recursively check the surrounding pixels, ignoring those that didn''t match the color that I originally clicked on..
I went nuts trying to get that fill function working before I figured out how to do it with recursion..
Anyway, I just wanted to show another example of where this could be used..
"Like all good things, it starts with a monkey.."
I used it to do the paint-can thing like you see in MS Paint..
When you clicked in the graphic, I''d check the color of that pixel..then I would start from that pixel, and recursively check the surrounding pixels, ignoring those that didn''t match the color that I originally clicked on..
I went nuts trying to get that fill function working before I figured out how to do it with recursion..
Anyway, I just wanted to show another example of where this could be used..
"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement