🔒 Locked
Minesweeper problem
Advertisement
Original Post
Yesterday I almost completed my minesweeper game (done with Java).
But I have one problem (probably Very easy), but for some reason I just don''t get it (but I have tried some crap).
When I click in empty space, all empty space adjancent to it will be revealed.
Everyone knows how minesweeper works, don''t you?
Could someone help on this one?
btw. Don''t tell it all at once.. just give me "tips".
ohh.....I read the question wrong :|
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that REALLY matters is green"
-Peter Griffin
Edited by - matrix2113 on January 8, 2002 9:27:59 PM
Edited by - matrix2113 on January 8, 2002 9:46:16 PM
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that REALLY matters is green"
-Peter Griffin
Edited by - matrix2113 on January 8, 2002 9:27:59 PM
Edited by - matrix2113 on January 8, 2002 9:46:16 PM
Well, I''m afraid I can''t really give you tips about this problem, without telling it at once. Do you know what recursion is (that is, a function calling itself)?? If you do know that, that should be enough advice to get to work!
Use a recursive function to reveal all space around an empty space. However, if you''re not familiar with it, it can be kind of hard to solve this problem, because recursion isn''t a very easy subject (maybe others think it is easy, but it is considered an ''advanced subject'' of programming).
Hi, if I understand you correctly you want to reveal all empty spaces around the square you just clicked? If that is so, I see no point in using recursion, using it will only make your problem harder to solve. Instead use a simple for loop (2 actully), I guees you have your squares organized in a 2D matrix, like square[x][y]
Say you click on square x,y. That will make the left top square x-1,y-1 and the right most square x+1,y+1. I don''t want to give it al up, but I guess you already see the answer. Just use a x and a y loop, and reveal all the squares from x-1 to x+1 and y-1 to y+1, in order not to reveal the x,y square the one you clicked you should check for that square and perform neccessary action. Also remember that the grid will have borders... so you should check for off boudries...
SPOILER (c++ code)
---------------------------------------
***************************************
int xmin,ymin,xmax,ymax;
xmin = xmax = ymin = ymax = 1;
// Check for border clicks...
if (!xclicked)
xmin = 0;
if (xclicked == game_width)
xmax = 0;
if (!yclicked)
ymin = 0;
if (yclicked == game_height)
ymax = 0;
// Reveal the squares.
for (int x = xclicked - xmin, x < xclicked + xmax, x++)
for (int y = yclicked - ymin, y < yclicked + ymax, y++)
{
if ( !(x == xlicked) && !(y == yclicked) )
square[x,y].visible = true;
}
PS: You can also put the border check in the last if statement, this will make the code cleaner, but this way will probaly make it a little easier to understand.
/ Goldsword
Say you click on square x,y. That will make the left top square x-1,y-1 and the right most square x+1,y+1. I don''t want to give it al up, but I guess you already see the answer. Just use a x and a y loop, and reveal all the squares from x-1 to x+1 and y-1 to y+1, in order not to reveal the x,y square the one you clicked you should check for that square and perform neccessary action. Also remember that the grid will have borders... so you should check for off boudries...
SPOILER (c++ code)
---------------------------------------
***************************************
int xmin,ymin,xmax,ymax;
xmin = xmax = ymin = ymax = 1;
// Check for border clicks...
if (!xclicked)
xmin = 0;
if (xclicked == game_width)
xmax = 0;
if (!yclicked)
ymin = 0;
if (yclicked == game_height)
ymax = 0;
// Reveal the squares.
for (int x = xclicked - xmin, x < xclicked + xmax, x++)
for (int y = yclicked - ymin, y < yclicked + ymax, y++)
{
if ( !(x == xlicked) && !(y == yclicked) )
square[x,y].visible = true;
}
PS: You can also put the border check in the last if statement, this will make the code cleaner, but this way will probaly make it a little easier to understand.
/ Goldsword
goldsword, i don''t think that would work. Recursion would work. When they click an space, it reveals all around it and then all around the ones that were around the first and then all around those and so on and so on. You don''t know the amount of squares that would work around it so you must use recursion to solve that problem.
i''m sorry about the tangental post, but...
why is everyone so against using recursion? if done properly, you can do some nifty things with it, and in a simpler and more elegant way than if you didn''t use it. i understand that it can eat up all your stack space if you are not careful, but for smaller problems it works sweetly.
--- krez (krezisback@aol.com)
why is everyone so against using recursion? if done properly, you can do some nifty things with it, and in a simpler and more elegant way than if you didn''t use it. i understand that it can eat up all your stack space if you are not careful, but for smaller problems it works sweetly.
--- krez (krezisback@aol.com)
quote:
Original post by matrix2113
I have a feeling you didn't write that code…
Because that's how MineSweeper works.
Why wouldn't I have wroted it?
That's how minesweeper works?
First of all I'm (quite) beginner programmer.
I haven't needed recursion before this, and about everything is done. I think now that the only thing I need recursion for is this revealing thing.
I tried to do it (actually I tried many ways) by checking which squares can get to clicked square, and saving values to arrays, but it got too complicated (hope you understood that
And thanks for everyone.
This recursion sounds exactly the thing I need for this problem.
quote:
Original post by matrix2113
Is this a spam???
TROLL!
No this is not, but I don't see what else that your post could be than spam.
Edited by - mc3712 on January 7, 2002 4:22:09 PM
In general, Goldsword is right about the two-loop-function from ClickedFieldX - 1 to ClickedFieldX + 1 and from ClickedFieldY - 1 to ClickedFieldY + 1, but this function needs to be recursive!
You don''t just reveal the surrounding fields and stopo there. The function has to do the following:
1.) Check, if the current field is empty (at first, this would be the clicked field). If it''s not skip the rest!
2.) Reveal the current field (and only the current one!)
2.) Call itself for all the surrounding fields (this is where the two loops come in...)
If your minefield isn''t REALLY big, you shouldn''t run into any considerable memory problems...
You don''t just reveal the surrounding fields and stopo there. The function has to do the following:
1.) Check, if the current field is empty (at first, this would be the clicked field). If it''s not skip the rest!
2.) Reveal the current field (and only the current one!)
2.) Call itself for all the surrounding fields (this is where the two loops come in...)
If your minefield isn''t REALLY big, you shouldn''t run into any considerable memory problems...
I forgot to mention (but it should be obvious) that the function also needs to skip allready revealed fields! Otherwise it would just continue to call itself for always the same fields and never stop...
Recursion shouldn''t be any sort of problem in this instance, but if you want a good programming exercise you can create a virtual stack and push and pop values from there..
One of the reasons why minesweeper/tetris/pacman/(insert_name) is so much fun to start programming with is because of all the small things you have the opportunity to learn and understand at a fundamental level..
If you figure out how to do what is essentially a recursive operation without relying on the recursive capabilities of the language/compiler, you will be in a much better position to deal with some huge database(in your game programming future) where recursion is not an option at all..
Try it both ways..you''ll be a better programmer for it..
Just a suggestion in the interests of education..
"Like all good things, it starts with a monkey.."
One of the reasons why minesweeper/tetris/pacman/(insert_name) is so much fun to start programming with is because of all the small things you have the opportunity to learn and understand at a fundamental level..
If you figure out how to do what is essentially a recursive operation without relying on the recursive capabilities of the language/compiler, you will be in a much better position to deal with some huge database(in your game programming future) where recursion is not an option at all..
Try it both ways..you''ll be a better programmer for it..
Just a suggestion in the interests of education..
"Like all good things, it starts with a monkey.."
I used minsweeper as my first game as well. I did it with recursion, which worked very well. If you would like me to send you the code, let me know.
ps. I have not test the graphics on other machines, so the graphics may not work. But the board representation class will still work fine.
---
Make it work.
Make it fast.
ps. I have not test the graphics on other machines, so the graphics may not work. But the board representation class will still work fine.
---
Make it work.
Make it fast.
Original post by goldsword
My bad, I didn''t explain it in the best way, anyway your code was helpful.
Not all, just all the ones that are connected to each other.
An example:
???????|
???1111|
???1...|
?211...|
?1....x|
-------- // This if the lower right corner
''x'' is where you click,
''.'' is empty space
''?'' is unrevealed space
So if you click on x, the above happens.
The numbers and empty spaces are revealed.
quote:
Sorry, thought you only meant to reveal the squares next to the one you clicked. Since you didn''t, recursion is the way to go.
My bad, I didn''t explain it in the best way, anyway your code was helpful.
quote:
Question.... When do the minesweeper game need to reveal all the squares not revealed from where u clicked???
Not all, just all the ones that are connected to each other.
An example:
???????|
???1111|
???1...|
?211...|
?1....x|
-------- // This if the lower right corner
''x'' is where you click,
''.'' is empty space
''?'' is unrevealed space
So if you click on x, the above happens.
The numbers and empty spaces are revealed.
Alright I've never programmed graphics before (yet...just started learning....), but I imagine it would be something like this:
I hope I didn't ruin it (you said you figured it help, but maybe this is more effient? Who knows, maybe it's less, so I've been wasting my time (and making a complete idiot of myself, although that wouldn't be the first/last time) here when I could have (should have) been studying for my Semester Finals :p...)
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that REALLY matters is green"
-Peter Griffin
EDIT:
Note, that I made this in C++, and not in Java; it's for your own good (...ok I lie I just don't know Java). The 2 languages are close enough to alike, so you can figure it out. It might (probably
) won't even work, considering I just started C++ less than a year ago, and slapped that code together in less than 2 minutes. :p
Have fun :p
Edited by - matrix2113 on January 8, 2002 9:49:40 PM
Edited by - matrix2113 on January 8, 2002 9:54:16 PM
|
I hope I didn't ruin it (you said you figured it help, but maybe this is more effient? Who knows, maybe it's less, so I've been wasting my time (and making a complete idiot of myself, although that wouldn't be the first/last time) here when I could have (should have) been studying for my Semester Finals :p...)
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that REALLY matters is green"
-Peter Griffin
EDIT:
Note, that I made this in C++, and not in Java; it's for your own good (...ok I lie I just don't know Java). The 2 languages are close enough to alike, so you can figure it out. It might (probably
Have fun :p
Edited by - matrix2113 on January 8, 2002 9:49:40 PM
Edited by - matrix2113 on January 8, 2002 9:54:16 PM
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement