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;
}

无论如何通过 rand() 函数来强制执行显式随机性?每次我运行这个程序时,我都会在网格上得到相同的可预测的炸弹模式。即使我更改 main() 中的种子,我仍然只能得到大约 3 种模式的变化。

4

3 回答 3

1

首先用种子初始化伪随机生成器(PRNG),一个简单的方法是使用当前时间,如下所示。

 #include <ctime>

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

即调用的好地方srand可以是您的 main() 函数的开始。

于 2013-07-20T21:24:02.170 回答
0

请参阅为什么我总是使用 rand() 获得相同的随机数序列?.

本质上,没有种子的 rand() 将始终返回相同的伪随机数数组——在这种情况下,种子通常是“1”。具有固定种子的 rand() 也将始终返回相同的伪随机数数组。

于 2013-07-20T21:23:26.430 回答
0

如果您在最初使用不同的 n 值调用 srand(n)(srand() 仅在 main 开始时调用一次)之后在程序中重复运行 rand(),那么基本上您会得到一小组结果的副本您期望得到更多样化的结果,然后从数学上几乎可以保证问题不在于 rand() - 这是您使用随机数的方式。即,可以使用“真正的随机性”作为算法的一部分,但您的算法仍然倾向于以高概率收敛到一个常见状态(或几个常见状态之一)。

查看您的代码,您没有提供 main() 或描述何时/打印什么是给出“相同 3 种模式的变体”,或描述您的 3 种模式的变体究竟意味着什么,或描述其他什么您希望看到的模式类型。如果没有这些信息,就很难对您的问题提供更多见解。也许这 3 种模式的所有“变化”都是人们期望看到的,而且概率很高。

于 2013-07-20T21:26:55.090 回答