# 2D Array mine game problem! C++ HELP

• 01-10-2013
grasshopper101
2D Array mine game problem! C++ HELP
Hi people, I have a problem with my mine game which uses two 2D arrays. I'd be much appreciated if you could help me with my problem

one 2D array generates random 0's and 1's like a grid..

the other 2D uses '?' and if the first 2D array equals 0 or 1... the second 2D array replaces the '?' with a m or e.. meaning empty or mine.

so when I get this to run the first time around it works perfectly and then when I enter my second set of x and y co-ordinates it
replaces the old 'e' or 'm' with a '?' .. It's super frustrating and I've been stuck with it for days.. I'm a newb to c++ and would greatly appreciate if anyone knows a solution or suggestion??

Much appreciated and Thanks! :)
• 01-10-2013
grasshopper101
BETTER QUESTION MAKES MORE SENSE.. how do i get the new integer value stored in the array to be skipped next time the grid loops!!!! :D!! please ANYBODY I've been stuck for days..and I really don't want to give up!
• 01-10-2013
std10093
too many words, too few (none) code... Most of the guys are programmers... I am a student, but yet can't understand you :)
• 01-10-2013
Salem
A better answer will be obtained if you post some relevant code.

Or you could try getting this to work.
Code:

```int grid[5] = { 0, 1, 0, 1, 0 }; char map[5] = { 0 }; if ( grid[0] == 0 ) map[0] = 'e';```
If you can successfully simulate the problem in 1D with a small array, then the jump to a larger 2D array shouldn't be that difficult.

There are all sorts of ways you could have messed it up to produce "the first time around it works perfectly".
- uninitialised variables
- mis-sized arrays
- buffer overruns

Without code, it's all wild guesswork.
• 01-10-2013
grasshopper101
umm still anyhelp
I think if i do what you said ^^ above but with another board i can solve the problem.
• 01-10-2013
Salem
Some thoughts.
1. Indentation. Getting into the habit of maintaining good indentation will save you a lot of grief in the long run.
SourceForge.net: Indentation - cpwiki
Code:

```#include <iostream> #include <cstdlib> using namespace std; int const row_array = 5;        //constants should be in caps (but is a personal preference not a rule) int const column_array = 5; int mines = 0; int value; char hidden_easy_game_board[row_array][column_array]; int easy_game_board[row_array][column_array]; //array global variable int entered_user_row = -1;      //the row integer and col integer the user enters int entered_user_col = -1; void mines_placement_easy(); void create_easy_board(); void hidden_easy_board(); int amountturns = 0; int amountmines = 0; int correctguesses = 0; int guessesremaining = 20; bool mine = false; bool notmine = false; int main() {   mines_placement_easy();   create_easy_board();          //delete this line after   do {     hidden_easy_board();        //this calls hidden easy board which is the 5by5 ? board.     cout << endl;     cout << "Please enter the row you would like to select 1-5: ";     cin >> entered_user_row;     entered_user_row -= 1;      //if user picks 1 it means 0 in the array ,and 2 means 1 and so on and so fourth will make sense later.     cout << endl;     cout << "Please enter the col you would like to select 1-5: ";     cin >> entered_user_col;     entered_user_col -= 1;      //if user picks 1 it means 0 in the area and 2 means 1 and so on and so fourth will make sense later.     cout << endl;     if (easy_game_board[entered_user_row][entered_user_col] == 1) {       cout << "well done you hit a bomb" << endl;       cout << endl;     } else if (easy_game_board[entered_user_row][entered_user_col] == 0) {       cout << "Woops you missed!" << endl;       cout << endl;     }     guessesremaining--;   } while (guessesremaining > 0); //_getch();   return 0; } void mines_placement_easy() {   //setting mines   do {     int row_array;              //declaring variables     int column_array;     do {       row_array = rand() % 5;  //initialising the declared variables with a random number POSITION with the modulus 5 woo!       column_array = rand() % 5;     } while (easy_game_board[row_array][column_array] != 0);     easy_game_board[row_array][column_array] = 1;     mines++;   } while (mines < 10); } void create_easy_board() {   //creating 5by5 board.   for (int row = 0; row < 5; row++) { //creating table using for loop     for (int column = 0; column < 5; column++) {       cout << easy_game_board[row][column] << " ";  // delete this line after     }     cout << endl;              // because each row is on a seperate line we have to endline!   } } void hidden_easy_board() {   char hidden_easy_game_board[row_array][column_array]; //array values!   char hidden_char = '?';   int row;   int column;   for (row = 0; row < 5; row++) { //creating table using for loop     for (column = 0; column < 5; column++) {       hidden_easy_game_board[row][column] = hidden_char;  //sets all grid to question mark       if ((row == entered_user_row) && (column == entered_user_col)) {         if (easy_game_board[entered_user_row][entered_user_col] == 1) {           hidden_easy_game_board[entered_user_row][entered_user_col] = 'b';         } else if (easy_game_board[entered_user_row][entered_user_col] == 0) {           hidden_easy_game_board[entered_user_row][entered_user_col] = 'e';         }       } else {         hidden_easy_game_board[row][column] = hidden_char;  //sets all grid to question mark       }       cout << hidden_easy_game_board[row][column] << " "; //printing out array table!     }     cout << endl;              // because each row is on a seperate line we have to endline!   } }```
2. Compiler warnings
Compile with as many warnings enabled as you can.
Code:

```\$ g++ -Wall -Wshadow foo.cpp foo.cpp: In function ‘void mines_placement_easy()’: foo.cpp:63:9: warning: declaration of ‘row_array’ shadows a global declaration [-Wshadow] foo.cpp:5:11: warning: shadowed declaration is here [-Wshadow] foo.cpp:64:9: warning: declaration of ‘column_array’ shadows a global declaration [-Wshadow] foo.cpp:6:11: warning: shadowed declaration is here [-Wshadow] foo.cpp: In function ‘void hidden_easy_board()’: foo.cpp:88:54: warning: declaration of ‘hidden_easy_game_board’ shadows a global declaration [-Wshadow] foo.cpp:9:6: warning: shadowed declaration is here [-Wshadow]```
You're using the same name in two different contexts. Are you sure you're referring to the one you think you are?

3. so when I get this to run the first time around it works perfectly
At the moment, your global variables like
char hidden_easy_game_board[row_array][column_array];
are implicitly declared as being
Code:

`char hidden_easy_game_board[row_array][column_array] = { { 0 } };`
That is, they're filled with zeros.

When you call something like mines_placement_easy(), it would be a good idea to put a loop at the start of this function to explicitly initialise the array to be all zeros before doing anything else. This would give you the consistency between runs.
• 01-10-2013
grasshopper101
thanks dude ill come back and report if I was able to fix this bugs or not :)!!