gpt4 book ai didi

c++ - 如何使用 rand() 强制执行显式随机性

转载 作者:行者123 更新时间:2023-11-28 07:30:48 24 4
gpt4 key购买 nike

<分区>

#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 种模式的变化。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com