Skip to main content
gamedev.net
🔒 Locked

Minesweeper problem

Started by mc3712 13 replies 1.9k views
Original Post
mc3712
mc3712
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".
matrix2113
matrix2113
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
nickm
nickm
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).
goldsword
goldsword
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
ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
kmsixpence
kmsixpence
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.
krez
krez
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)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
mc3712
mc3712
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
Master Jazzman
Master Jazzman
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...
"If I knew where I was I wouldn't be here, believe me!"
Master Jazzman
Master Jazzman
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...
"If I knew where I was I wouldn't be here, believe me!"
monkeyman
monkeyman
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.."
"Like all good things, it starts with a monkey.."
CaptainJester
CaptainJester
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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
mc3712
mc3712
It works now perfectly.
I studied recursion, and it really did the trick, but I''m still going to try it without recursion too.

Now to work on a pacman(or tetris).

Thanks again for everyone.
goldsword
goldsword
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.

Question.... When do the minesweeper game need to reveal all the squares not revealed from where u clicked???
ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
mc3712
mc3712
Original post by goldsword
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.



matrix2113
matrix2113
Alright I've never programmed graphics before (yet...just started learning....), but I imagine it would be something like this:
      
void ClickedOnThingy(int squareClicked[][]) {
if(squareClicked[x][y] == HASNT_BEEN_PUSHED_YET)//Figure out a way to do that, read below.

squareClicked[x][y] = OK_I_PUSHED_IT__CAN_WE_GO_HOME_AND_HAVE_SUPPER_NOW_QuestionMark;
/*So I went a little overboard*/

//an IF statement checking to see if its the square pressed is blank; if it is (blank), than go into this:

int *checkingCurrentSquare = new int[ThisMany][ByThisMany];
//for loop checking y axis
//for loop checking x axis
if (checkingCurrentSquare[x][y] != 0)//Checking the squares around the square pushed
//Its not blank, so push the button and display the number
else if (checkingCurrentSquare[x][y] == HASNT_BEEN_PUSHED_YET)
/*This means its a blank square and it hasn't been pushed yet (in case the sqaure we're on has been pushed before in this recursive function (by another blank square)) In this case, you'd need to figure out a way to check if its been pusshed yet (If it has been, add 8 to its number? Create a class with member varibles? Thats up to you)*/

ClickedOnThingy(checkingCurrentSquare[x][y]);
//Calls itself

//End the 2 FOR's and the IF




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'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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.