0
#include <iostream>
#include <stdlib.h>
#include <ctime>

class Minesweeper {
   public:
       void buildBoards();
       void printTopLvlBoard();
       void printBottomLvlBoard();
       void userEntry(int x, int y);
       void gameOver();
       bool stateGood();
   private:
       static const int CLMN_MAX_SIZE = 5;
       static const int ROW_MAX_SIZE = 5;
       char topLvlBoard[ROW_MAX_SIZE][CLMN_MAX_SIZE];
       char bottomLvlBoard[ROW_MAX_SIZE][CLMN_MAX_SIZE];
       bool gameState;
   };

void Minesweeper::printTopLvlBoard(){
    std:: cout << "Board" << std:: endl;
    for(int i = 0; i < ROW_MAX_SIZE; i++) {
       for(int j = 0; j < CLMN_MAX_SIZE; j++) {
          std::cout << topLvlBoard[i][j];
       }
    std::cout << std::endl;
    }
}

void Minesweeper::buildBoards(){
   for(int i = 0; i < ROW_MAX_SIZE; i++){
       for(int j = 0; j < CLMN_MAX_SIZE; j++){
           bottomLvlBoard[i][j] = 'O';
           topLvlBoard[i][j] = 'x';
       }
   }
   for(int k = 0; k < 5; k++) {
       bottomLvlBoard[**rand()%5**][**rand()%5**] = 'B';
   }
   gameState = true;
}

void Minesweeper::printBottomLvlBoard(){
   for(int i = 0; i < CLMN_MAX_SIZE; i++){
      for(int j = 0; j < ROW_MAX_SIZE; j++){
        std::cout << bottomLvlBoard[i][j];
      }
      std::cout << std:: endl;
   }
}  

void Minesweeper::userEntry(int x,int y){
   char bottomTmp = bottomLvlBoard[x-1][y-1];

   if(bottomTmp == 'O'){
      topLvlBoard[x-1][y-1] = '*';
      bottomLvlBoard[x-1][y-1] = 'C';
   }
   else if(bottomTmp == 'B'){
      gameOver();
   }
}
void Minesweeper::gameOver() {

  std::cout << std:: endl;
  std::cout << "**********************" << std:: endl;
  std::cout << "**********************" << std:: endl;
  std::cout << "*** BOMB BOMB BOMB ***" << std:: endl;
  std::cout << "*** BOMB BOMB BOMB ***" << std:: endl;
  std::cout << "*** BOMB BOMB BOMB ***" << std:: endl;
  std::cout << "*** BOMB BOMB BOMB ***" << std:: endl;
  std::cout << "**********************" << std:: endl;
  std::cout << "**********************" << std:: endl;
  std::cout << std:: endl;

  std::cout << "You have landed on a bomb!" << std:: endl;
  std::cout << std:: endl;

  printBottomLvlBoard();
  gameState = false;
}

bool Minesweeper::stateGood(){
   bool tmpYesNo = (gameState == true) ? true : false;
   return tmpYesNo;
}

Is there anyway to enforce explicit randomness via the rand() function? Every time I run this program I get the same predictable pattern of bombs across the grid. Even if I change the seed in main() I still only get a variation of about 3 patterns.

3 Answers3

1

First initialize the pseudo random generator (PRNG) with a seed, an easy way is to use the current time like below.

 #include <ctime>

 // and put the following before calling rand, and where it is only called once
 srand(time(NULL));

i.e. a good place to call srand can be the beginning of your main() function.

alexbuisson
  • 7,699
  • 3
  • 31
  • 44
0

See Why do I always get the same sequence of random numbers with rand()?.

In essence, rand() without a seed will always return the same array of pseudo-random numbers -- in this case, the seed is usually '1'. rand() with a fixed seed will always return the same array of pseudo-random numbers as well.

Community
  • 1
  • 1
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • I'm not using a fixed seed. It's included in my implementation. The probably is that I am getting a very small set of value variations even with seeding. – Eric Hondzinski Jul 20 '13 at 21:35
  • @user2069483: If you're not calling `srand()`, then you *are* using a fixed seed; it's equivalent to calling `srand(1)` at the beginning of your program. `srand(time(NULL))` should give you a distinct sequence each time your program runs, as long as your runs are more than 1 second apart. – Keith Thompson Jul 20 '13 at 21:48
0

If you run rand() repeatedly later in your program after initially calling srand(n) (srand() only called once at the beginning of main) with different values of n, and basically you are getting duplicates of a small set of results when you were expecting a more diverse set of results, then mathematically it's almost guaranteed that the problem isn't with rand() -- it's HOW you're using your random numbers. I.e., it's possible to use "true randomness" as part of your algorithm but still have your algorithm tend to converge to a common state (or one of a few common states) with high probability.

Looking at your code, you haven't provided a main() or described when/what you are printing that is giving the "variations of the same 3 patterns", or described what exactly your variations of 3 patterns means, or described what other types of patterns you would expect to see. Without this info, it will be hard to give any more insight into your question. Maybe all the "variations" of the 3 patterns is what one would expect to see, with high probability.

user2566092
  • 4,631
  • 15
  • 20