Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enforce explicit Randomness with rand()
    primarykey
    data
    text
    <pre><code>#include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;ctime&gt; 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 &lt;&lt; "Board" &lt;&lt; std:: endl; for(int i = 0; i &lt; ROW_MAX_SIZE; i++) { for(int j = 0; j &lt; CLMN_MAX_SIZE; j++) { std::cout &lt;&lt; topLvlBoard[i][j]; } std::cout &lt;&lt; std::endl; } } void Minesweeper::buildBoards(){ for(int i = 0; i &lt; ROW_MAX_SIZE; i++){ for(int j = 0; j &lt; CLMN_MAX_SIZE; j++){ bottomLvlBoard[i][j] = 'O'; topLvlBoard[i][j] = 'x'; } } for(int k = 0; k &lt; 5; k++) { bottomLvlBoard[**rand()%5**][**rand()%5**] = 'B'; } gameState = true; } void Minesweeper::printBottomLvlBoard(){ for(int i = 0; i &lt; CLMN_MAX_SIZE; i++){ for(int j = 0; j &lt; ROW_MAX_SIZE; j++){ std::cout &lt;&lt; bottomLvlBoard[i][j]; } std::cout &lt;&lt; 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 &lt;&lt; std:: endl; std::cout &lt;&lt; "**********************" &lt;&lt; std:: endl; std::cout &lt;&lt; "**********************" &lt;&lt; std:: endl; std::cout &lt;&lt; "*** BOMB BOMB BOMB ***" &lt;&lt; std:: endl; std::cout &lt;&lt; "*** BOMB BOMB BOMB ***" &lt;&lt; std:: endl; std::cout &lt;&lt; "*** BOMB BOMB BOMB ***" &lt;&lt; std:: endl; std::cout &lt;&lt; "*** BOMB BOMB BOMB ***" &lt;&lt; std:: endl; std::cout &lt;&lt; "**********************" &lt;&lt; std:: endl; std::cout &lt;&lt; "**********************" &lt;&lt; std:: endl; std::cout &lt;&lt; std:: endl; std::cout &lt;&lt; "You have landed on a bomb!" &lt;&lt; std:: endl; std::cout &lt;&lt; std:: endl; printBottomLvlBoard(); gameState = false; } bool Minesweeper::stateGood(){ bool tmpYesNo = (gameState == true) ? true : false; return tmpYesNo; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload