2D Array mine game problem! C++ HELP

This is a discussion on 2D Array mine game problem! C++ HELP within the C++ Programming forums, part of the General Programming Boards category; Hi people, I have a problem with my mine game which uses two 2D arrays. I'd be much appreciated if ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    Exclamation 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!
    Last edited by grasshopper101; 01-10-2013 at 10:11 AM.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    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!!!! !! please ANYBODY I've been stuck for days..and I really don't want to give up!

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    too many words, too few (none) code... Most of the guys are programmers... I am a student, but yet can't understand you
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    umm still anyhelp
    I think if i do what you said ^^ above but with another board i can solve the problem.
    Last edited by grasshopper101; 01-10-2013 at 11:36 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    thanks dude ill come back and report if I was able to fix this bugs or not !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-19-2011, 09:55 AM
  2. Question on sound in a game of mine.
    By Vanished in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2002, 04:46 PM
  3. Question on sound in a game of mine
    By Vanished in forum Game Programming
    Replies: 2
    Last Post: 12-09-2002, 12:56 PM
  4. An old good DirectX game of mine
    By Sang-drax in forum Game Programming
    Replies: 8
    Last Post: 07-25-2002, 11:05 AM
  5. Another game of mine...
    By valar_king in forum Game Programming
    Replies: 7
    Last Post: 01-03-2002, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21