Need some help with minesweeper code

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
This is the code for the Board class; it's where I'm having my main problem now. If you have any questions about what a certain line is supposed to be doing, please ask. I'm having a problem where each cell on the board is doing one of the following:
1) Not instantiating to the right type of object (empty or mine)
2) Not giving the right return when isMine() method is called
3) Having trouble putting Mines and Empty's in an array of Space (interface of Mine and Empty)


The code is attached. I put all of them just in case they were needed, but my main problem is only with the Board class, and the problem area is marked off with comments.


Thanks for any help!
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You really should have only one object for the space.

Each object could have five drawing states:
Unknown
Empty
Mine
Flag
Exploded.

Each object has two internal states
Mine
Empty

Allocate all the objects based on the board size.
Randomly assign the state of each object.
During instantation of the object, the drawing state is set to Unknown.
 

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
This may seem silly, but we have not leared how to implement a GUI. All of our applications are through console. This is why I ask for a row and column by number (by user input).

I don't think I will be dealing with flags. It's already a little bit too complicated for me. This does not have to be played, but just to show that I am somewhat competent in J#.

And thank you for the advice with the Space object having different states: empty, mine, and uncovered. I think I will definitely change that!
 

sumguy1

Member
May 23, 2007
86
0
0
I'm not sure if the way you are determining which spaces are empty and mines is accurate. You are getting a random number between 0 and 99 on each iteration and comparing it to the value of the passed in parameter "percentMines." Lets say somebody creates a 5 x 5 grid with only 20% mines. That means you want 5 mines and 20 empties out of 25 total spaces. But what if your random generator does not give you 5 values that are less than 20 out of 25 iterations? Then you don't have 5 mines. In fact, you could potentially have all empties, could you not?


EDIT: Also is this line correct?

if(grid[rows-1][cols-1].isMine())

are you sure you don't mean:


...
 

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
sumguy1,

I do realize that the number of mines will not always (actually, barely ever) be the exact amount of mines of the percent of the board (err... WOW I worded that poorly). I will go back and fix that if I have time, but most likely I will not have the time to go back and fix minor things like this. Right now I'm just trying to get a working board that actually contains both mines and empty spaces. :p

Also, about the other code that you pointed out. That is just my debug code that I forgot to take out. Disregard that.

Thank you for the help, guys, but my main problem is still not solved. :)
 

sumguy1

Member
May 23, 2007
86
0
0
I think that in your getMines() method, your outer for loop is going through cols and your inner loop is going through rows. Isn't that the reverse order of how it should be? This could explain why you are not seeing the expected output when you call isMine() if you are not actually looking at the node on the grid you intended to be looking at.
 

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
The thing is, when I output, say, a 5x5 array list (the board), it tells me that each and every space, when called with the .isMine() method, returns false. However, I can manually set cells in the grid to be a mine, and it works fine.
 

MSCoder610

Senior member
Aug 17, 2004
831
0
71
Originally posted by: sumguy1
EDIT: Also is this line correct?

if(grid[rows-1][cols-1].isMine())

are you sure you don't mean:
if (grid[ j ][ i ].isMine())

Seconded, that's the problem that I see

Edit: Oh, you said not that. It also seems like you're missing something like this in your constructor:
this.rows = rows;
this.cols = cols;
getMines() uses 'rows' and 'cols', but I never see them initialized.
 

MSCoder610

Senior member
Aug 17, 2004
831
0
71
Also in getMines, you should have, at the innermost level
if (grid[ j ][ i ].isMine())
not
if (grid[ j ][ i ] == mine)
as you currently do. Checking if the current grid location '== mine' basically checks if the objects are the same (i.e. same memory address) and everything, not that the location is a mine like you want. Basically since you do 'mine = new Mine()' only once, and never use that instance again, none of the mines on your board would be the same object (same memory location). Which is why you might think it's not finding any mines.

I'm pretty sure those are the main issues.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: DyslexicHobo
The thing is, when I output, say, a 5x5 array list (the board), it tells me that each and every space, when called with the .isMine() method, returns false. However, I can manually set cells in the grid to be a mine, and it works fine.

not fully understanding Java; but you have code where you declare isMine to return false, not a state.

You may wish to revisit your space object code with a eye toward using a set of flags indicating the condition.

Otherwise, you are trying to unravel a strand of spaghetti

 

sumguy1

Member
May 23, 2007
86
0
0
Originally posted by: MSCoder610
Originally posted by: sumguy1
EDIT: Also is this line correct?

if(grid[rows-1][cols-1].isMine())

are you sure you don't mean:
if (grid[ j ][ i ].isMine())

Seconded, that's the problem that I see

Edit: Oh, you said not that. It also seems like you're missing something like this in your constructor:
this.rows = rows;
this.cols = cols;
getMines() uses 'rows' and 'cols', but I never see them initialized.

rows and cols are method arguments.
 

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
Originally posted by: MSCoder610
Originally posted by: sumguy1
EDIT: Also is this line correct?

if(grid[rows-1][cols-1].isMine())

are you sure you don't mean:
if (grid[ j ][ i ].isMine())

Seconded, that's the problem that I see

Edit: Oh, you said not that. It also seems like you're missing something like this in your constructor:
this.rows = rows;
this.cols = cols;
getMines() uses 'rows' and 'cols', but I never see them initialized.

They are extraneous. I just forgot to remove them. I changed my code up quite a bit, and forgot that I wasn't using them anymore.
 

DyslexicHobo

Senior member
Jul 20, 2004
706
1
81
Originally posted by: DyslexicHobo
Originally posted by: MSCoder610
Originally posted by: sumguy1
EDIT: Also is this line correct?

if(grid[rows-1][cols-1].isMine())

are you sure you don't mean:
if (grid[ j ][ i ].isMine())

Seconded, that's the problem that I see

Edit: Oh, you said not that. It also seems like you're missing something like this in your constructor:
this.rows = rows;
this.cols = cols;
getMines() uses 'rows' and 'cols', but I never see them initialized.

They are extraneous. I just forgot to remove them. I changed my code up quite a bit, and forgot that I wasn't using them anymore.

Edit: WOW! I am so stupid. I didn't realize what you guys were pointing out with the line "if(grid[rows-1][cols-1].isMine()) ", which sumguy1 first pointed out. The array was actually being initialized correctly, I was just stupid and did not use the foor loop iterator.

The program is on it's way to being complete now! Thank you. :)
 

MSCoder610

Senior member
Aug 17, 2004
831
0
71
Originally posted by: sumguy1
Originally posted by: MSCoder610
Originally posted by: sumguy1
EDIT: Also is this line correct?

if(grid[rows-1][cols-1].isMine())

are you sure you don't mean:
if (grid[ j ][ i ].isMine())

Seconded, that's the problem that I see

Edit: Oh, you said not that. It also seems like you're missing something like this in your constructor:
this.rows = rows;
this.cols = cols;
getMines() uses 'rows' and 'cols', but I never see them initialized.

rows and cols are method arguments.

'row' and 'col' are the arguments (i.e. the position). 'rows' and 'cols' are the total # rows and cols for the playing field, class-level.