unable to delete 2D array

4 Replies - 939 Views - Last Post: 04 July 2008 - 08:30 PMRate Topic:

Deleting Dynamic 2DArray in Minesweeper

Posted 04 July 2008 - 04:09 AM

Hi Guys,
I've been working on this game for weeks now. I'm using gcc as my compiler and I am having an issue with two things. I'd love to get some help with this if I could please.
1) I can not play the same difficulty game twice in a row. I suspect that when the first game ends, the dynamic array is not being properly deleted. When the game ends either Win() or Lose() is called, and before those functions kick you back to the main menu they try to delete using two different ways. But I don't think either are working.
2) My Uncover() is not working properly, I want it to recursively check for zones with neighboring mines and ouput those. At the moment it's just looked for neighboring Zones with 0 mines in the surronding 8 squares, and sometimes it does not even do that correctly.

Sorry for the global variables etc, this is my first 8 weeks programming c++!
Thanks,

Replies To: Deleting Dynamic 2DArray in Minesweeper

Re: Deleting Dynamic 2DArray in Minesweeper

Posted 04 July 2008 - 10:22 AM

The way i've always stored and deleted dynamically:

int *board = new int[SIZE];
delete [] board;

It's kinda hard to tell exactly what is going on but I'm assuming you're not creating a new board after you lose. Instead of creating it globally, just create a new one when the user starts a new game. If you are creating a new one just slap me and show me where you put it .

However, there is something you could do until you figure out what the bug is though. Make it to where the user doesn't have to put in uppercase letters only in the menus. It was a bit of a pain to have to push the shift key the whole time. Also, a normal user won't know whats wrong when he pushes 'q' and the menu just displays again. Other than that VERY cool idea, the banner looks awesome. Looks like you've already put in a lot of hard work, keep it up.

Note that cells is a one dimensional array. This is because I find two dimensional arrays to be really annoying to deal with. Also, it's nice to be able to look at your array in a single loop, at times. To make life easier, you can use a nice little helper function, like this:

Re: Deleting Dynamic 2DArray in Minesweeper

Posted 04 July 2008 - 03:46 PM

tortillaboy05, on 4 Jul, 2008 - 10:22 AM, said:

The way i've always stored and deleted dynamically:

int *board = new int[SIZE];
delete [] board;

It's kinda hard to tell exactly what is going on but I'm assuming you're not creating a new board after you lose. Instead of creating it globally, just create a new one when the user starts a new game. If you are creating a new one just slap me and show me where you put it .

However, there is something you could do until you figure out what the bug is though. Make it to where the user doesn't have to put in uppercase letters only in the menus. It was a bit of a pain to have to push the shift key the whole time. Also, a normal user won't know whats wrong when he pushes 'q' and the menu just displays again. Other than that VERY cool idea, the banner looks awesome. Looks like you've already put in a lot of hard work, keep it up.

Thanks,
Well a newboard is being created each time, in theory. When a player loses wins they are thrown to Loses() this does the below
delete [] *minefield_board;
delete [] minefield_board;
delete [] *state_board;
delete [] state_board;
then they are passed back to ShowMenu(). And if they selecet a new game, the cycle starts over again in the form of Placemines() Mineboard is initialized and rand is generated --> Placemine()Mines are placed --> InitializeState() state board is initialized and populated etc.

A similar thing happens when a user wins, at the Win() function I tried deleting the 2D dynamically allocated memory in the way below (and above) I've found that neither works. Because if I use my DisplayMineBoard() debugger, I can see that there is a pointer address in place of when a mine should be generated.

Re: Deleting Dynamic 2DArray in Minesweeper

Posted 04 July 2008 - 08:30 PM

Okay I see it now. I don't see anything wrong with your delete, but maybe you're deleting the wrong thing? Pointers can be very confusing, it always helps me to draw a diagram showing where my pointers are going. Maybe your pointers aren't pointing where you think they are. I'm sorry I couldn't be more help, good luck!