Computer Science Canada Minesweeper help |
Author: | cavetroll [ Wed Jul 25, 2007 11:26 pm ] |
Post subject: | Minesweeper help |
I have a working minesweeper game except for the one feature that is in the windows version. You know when you click a place that has empty spaces all around it shows all the empty places around it. How do I do that, I have researched pathfinding algorithms and have been thinking about ways to modify them. Any source is greatly appreciated. |
Author: | Saad [ Thu Jul 26, 2007 2:53 am ] |
Post subject: | RE:Minesweeper help |
It isnt pathfinding. The algorithm is that for the current cell, if the number of mines around it is not 0 then halt and show the current block, If the value of the current block is > 0 then show the current cell, and for every cell around it call the same algorithm. |
Author: | McKenzie [ Thu Jul 26, 2007 7:16 am ] |
Post subject: | Re: Minesweeper help |
Try looking at the "flood-fill" algorithm, it is a very similar problem. |
Author: | cavetroll [ Thu Jul 26, 2007 11:38 am ] |
Post subject: | Re: Minesweeper help |
Thanks for the quick responses, and yes you were right I wasn't looking for pathfinding but I didn't know the proper term for what I was looking for. I looked up flood-fill algorithms and found how to do it. Again thanks for the response. |
Author: | cavetroll [ Thu Jul 26, 2007 12:17 pm ] | ||
Post subject: | Re: Minesweeper help | ||
Sorry I thought I had it but now I can't get the code to work, I am not sure why I tried to do it using recursion(http://en.wikipedia.org/wiki/Flood_fill) based on the first pseudo-code example. Here is the code,
|
Author: | Aziz [ Thu Jul 26, 2007 12:23 pm ] |
Post subject: | RE:Minesweeper help |
There's a recursive and iterative method. I can post more help later on, when I'm not so swamped at work. |
Author: | Saad [ Thu Jul 26, 2007 5:21 pm ] |
Post subject: | RE:Minesweeper help |
Cant diagnose based on the code you have, What error are you getting? |
Author: | cavetroll [ Thu Jul 26, 2007 7:08 pm ] |
Post subject: | Re: Minesweeper help |
No error messages the code just doesn't do what it is intended to do. Have you read the earlier posts? They explain what the code is supposed to do but it doesn't work. I am just trying to figure out where it went wrong. |
Author: | CodeMonkey2000 [ Thu Jul 26, 2007 8:35 pm ] |
Post subject: | RE:Minesweeper help |
How have you implemented your minesweeper game? |
Author: | cavetroll [ Fri Jul 27, 2007 12:34 am ] |
Post subject: | Re: Minesweeper help |
I can post the code if you want but it is very messy right now so I'd rather not, but basically I have an array(squares(1..18,1..18,1..3)) which stores the data(count, type, state) for every square. When the square is click the state value is changed to 2 if it is left-clicked and 3 if it is right-clicked. Then I draw accordingly. The code given is what I want to run if the square clicked has a count of 0. Post if this doesn't help. |
Author: | Saad [ Fri Jul 27, 2007 4:05 am ] |
Post subject: | RE:Minesweeper help |
That alogorithm seems right, only problem I can see is that every square will have set to 2. Since it only checks if that square is 2 because of this all squares will end up as 2. You want to check the amount of mines around the current cell aswell. If its not= 0 then return |
Author: | Aziz [ Fri Jul 27, 2007 7:50 am ] | ||||
Post subject: | RE:Minesweeper help | ||||
I can see a major problem in your logic. Why does your array have three dimensions? Array indices should represent position, not value. Then value is store in a position of an array. So it does not make sense to have a 3D array for Minesweeper. If a user clicks a square, it changes the state, which changes the position, not the value. What you should do is have a 2D array to store the state. I'm assume the array values already hold whether the square holds a mine or not? So squares(x,y,s) is 0 for mine, 1 for clear? There's to approaches you could make. Both involved store both the state and the mine/not-mine data in the array (actually, you COULD have parallel arrays, but I'm not even going to bring it up to do horribleness) 1: The value store at a position (x,y) in the array would be an integer. 1,2,3 would be the state. a negative would represent a mine So a state of 1 would be (I'm assuming) a non-clicked square the is empty. A square with state of -1 would be a non-clicked square that contains a mine. 2: Better, IMO Declare a type like so:
and have an array to store the blocks:
|
Author: | cavetroll [ Fri Jul 27, 2007 1:54 pm ] |
Post subject: | Re: Minesweeper help |
I think you misinterpreted what I said, if not I misinterpreted your message. I have 3 dimensions because the first 2 hold the x and y position on the grid. the second one stores state(whether it has been left or right clicked), count(how many mines are around this square), and type(whether it is a mine(1) or non-mine square(2)). These are accessed using the constants state, count, and typ. All squares start with the state value of 1(2 is left clicked, 3 is right clicked). the count value is different for each square. |
Author: | Aziz [ Fri Jul 27, 2007 2:01 pm ] |
Post subject: | RE:Minesweeper help |
Could you post your source code? An array's indices should only represent position. Having an array with indices (x, y, state) represents squares in terms of x,y and state positions each state (1,2,3) is a different value. The line squares (nodex, nodey, state) := 2 is "set the square at x position nodex, y position nodey, and state position state to 2. It does not mean set the state of square x,y to 2. Its a bit confounding, so please post your source code and I can better understand it. |
Author: | cavetroll [ Fri Jul 27, 2007 2:46 pm ] | ||
Post subject: | Re: Minesweeper help | ||
K here's the code,
Again I apologize for the messy code. |
Author: | Aziz [ Fri Jul 27, 2007 2:56 pm ] |
Post subject: | RE:Minesweeper help |
I understand know. The third dimension represent what aspect you're trying to access of a certain square Declare your constants with CAPS, it is a good style and helps determining that they're constants rather than variables. So, to set the type it would be squares(x, y, TYPE) := type to set state: squares(x, y, STATE) := state to set count: squares(x, y, COUNT) := count At first look, it seems it should work, though I haven't gone into it deep. As I don't have Turing at work (as my copy may be less than legal, not sure if I am still a student or not ![]() PS. Comment-as-you-go. Works wonders. |
Author: | CodeMonkey2000 [ Fri Jul 27, 2007 3:25 pm ] |
Post subject: | RE:Minesweeper help |
I didn't look at your code, but your procedure doesn't do anything. All the variables are local, therefore you aren't making any changes to the variables outside of the procedure. Anything you do within the procedure, doesn't affect your main program. |
Author: | Aziz [ Fri Jul 27, 2007 3:27 pm ] |
Post subject: | RE:Minesweeper help |
I believe the 5th line sets the state of the square to 2 |
Author: | CodeMonkey2000 [ Fri Jul 27, 2007 3:30 pm ] |
Post subject: | RE:Minesweeper help |
Opps. Silly me I overlooked that ![]() |
Author: | CodeMonkey2000 [ Fri Jul 27, 2007 4:11 pm ] | ||
Post subject: | Re: Minesweeper help | ||
I managed to make it even more messy!!!!!! I'm too lazy to clean it up myself, but you should be able to.
|
Author: | Saad [ Fri Jul 27, 2007 4:12 pm ] |
Post subject: | RE:Minesweeper help |
Get rid of the 3rd dimension, and have a type for cell which contains the number of mines around it and its click status |
Author: | CodeMonkey2000 [ Fri Jul 27, 2007 4:22 pm ] |
Post subject: | RE:Minesweeper help |
I basically redid your procedure. I'm not familiar with this algorithm ( or any path finding algorithm) but wikipedia is a great resource ![]() |
Author: | Aziz [ Sat Jul 28, 2007 7:49 am ] |
Post subject: | RE:Minesweeper help |
a100: that would be the preferred route. perhaps he does not have experience with types though. Great tutorials to learn here however. Anyways, I'm in Niagara Falls so I'm not wasting my time this weekend on the computer, Peace |
Author: | Ultrahex [ Sun Jul 29, 2007 12:29 am ] | ||
Post subject: | Re: Minesweeper help | ||
I created a little short way to do it by command (so basically no GUI, just an engine) just to show how you would implement something like it... only reason i did it that way cause i was really confused by your code because it was so messy and the lack of comments! hopefully this will help you out a bit.
|
Author: | cavetroll [ Mon Jul 30, 2007 7:35 pm ] | ||
Post subject: | Re: Minesweeper help | ||
Thanks for all your help, I got it working. Here is the finished procedure.
|