Mistake in the source code, no error, game.

Pages: 12
I have to take exception to TheIdeasMan suggestion to use a union.

A struct would be better suited here and would not be subject to possible misuse. We're not talking about very much memory here.
struct Cell 
{  char Mine;            
    unsigned Count;   
    char Empty;         

Another suggestion I have is to make array1 and array_mine the same data type.
When debugging your program, I found it very useful to add the following function:
void Display_Grid (char * grid)
{ for (int i=0; i<5; i++)
   { for (int j=0; j<5; j++)
        cout <<  grid[i*5+j]; 
      cout << endl;

Not only can this be used to display the player's view of the board replacing lines 104-108, it can also be used to display the array of mines for debugging purposes. The above code assumes you don't adopt the struct/union idea.
Thank you for responses, but all you have posted is mostly how to improve the code looks, and such, and much of what you told me is something i don't really understand such as unions, i, for now really just want to correct the code so it finally does what it is intended to do, and after im done, to re-write parts of code to improve it, for learning purposes. So i would really appreciate if anyone spoted why hint displays huge numbers instead of intended = (number of mines around the tile). I really appreciate your help, but again, all i want is to make the code work for now, then i will go trough rest of suggestions and improve code so its really pretty, alright ?
The suggestions I've been making have not been about how your code looks, but rather about your programming technique. Better technique means easier to debug. One of the reasons I haven't debugged your proximity problem is that I would have to go back and remake many of the changes I've already suggested.

Here's an example. I'm not clear if you're using array_mine[0-24] or array_mine[1-25]. In function random, you possibly place mines in array_mine[0-25] for a total of 26 possible mines. This is just sloppy. Yes, I understand wanting to get it working first, then cleaning it up, but better programming in the first place means less to clean up later.

Likewise in check(), you're checking locations 0-25 for a total of 26. It's okay to use 1-25, but you have to be consistent about it. The convention is C and C++ is that arrays start at 0. Again, a for loop makes the 1-25 usage clearer,

You're asking other people to read and understand your code. If it's not clear to the reader you're less likely to get help.

As for your proximity problem, I'm not getting large numbers, but I'm not getting the correct numbers either. I'm also blowing up on cells that don't have mines.

If you're getting large nonsensical numbers, you're probably trying to print an uninitialized value.


I had originally thought of using a struct, but then realised a cell only needs to hold 1 value at a time. I am not sure what you mean by "misuse of a union" (maybe I have more to learn here), and the struct could be misused by setting several of the variables - which doesn't make sense. For example, one could set the Mine and the Adjacency count, which could cause problems.

Clearly this boils down to proper programming, but I could say the same thing about the union.


As AbstractionAnon was saying you probably have an uninitialized value. One really good basic rule in programming is to always initialise your variables. The hint variable could be initialised to something clearly wrong but sane like 100 say. So this tells you that your variable is not being set to it's proper value yet.

Another reason to write code with good elegant form, is that it makes it easier to find errors like this.

Any way I should leave you in the very good hands of AbstractionAnon.
I thought about that it is uninitialized integer as well, but if you see into the globals i made, its set to display 0, unless it gets changed as it is a variable..
I also know its better for reader, but i been making this for 2 weeks now due to not much time on my hands, and so im loosing the feel of the code, and i get into the position of reader. Now i just want to clean up the code i already started, as i don't feel like re-writing it right now, when its not even working.
Topic archived. No new replies allowed.
Pages: 12