2 qq 30137777 qq_30137777 于 2016.05.08 22:30 提问

C程序写了一个简化的控制台的扫雷,想问下,怎么才能在每次运行时,动态随机埋雷?

```// Duplicate.cpp : Defines the entry point for the console application.
//

#include
#include

const int START_X = 27;
const int START_Y = 13;
const int MAX_X = 9;
const int MAX_Y = 9;

class Cube;
void setBomb();
void checkAndSetNearBomb(int, int);
void show();
void showXY();
void cursorGoTo(int, int);
void gameStart();
void autoOpen(int , int);
int player();
int ifWin();

int main()
{
//initialize
gameStart();
//display graphics
show();
//play
while (player()){}
return 0;
}

class Cube{
private:
int nearHaveBombNumber;
bool ifOpen;
bool ifHaveBomb;
public :
void setNearHaveBombNumber(int number){
this->nearHaveBombNumber = number;
}
int getNearHaveBombNumber(){
return this->nearHaveBombNumber;
}
void setOpen(){
this->ifOpen = true;
}
bool getIfOpen(){
return ifOpen;
}
void setHaveBomb(){
this->ifHaveBomb = true;
}
bool getIfHaveBomb(){
return this->ifHaveBomb;
}
void resetCube(){
this->ifOpen = false;
this->ifHaveBomb = false;
this->nearHaveBombNumber = 0;
}
};
Cube cube[MAX_X][MAX_Y];

//initialize game
void gameStart(){
//initialize cube object
for (int i = 0; i < MAX_X; i++){
for (int j = 0; j < MAX_Y; j++){
cube[j][i].resetCube();
}
}
//set random bomb of cube
setBomb();
//set near bomb number
for (int i = 0; i < MAX_X; i++){
for (int j = 0; j < MAX_Y; j++){
checkAndSetNearBomb(i ,j);
}
}
}
void setBomb(){
int counter = 0;
srand((unsigned)GetCurrentTime());
while (!(counter == 10)){
int nx = rand() % MAX_X;
int ny = rand() % MAX_Y;
if ((nx >= 0 && nx <= 8) && ((ny >= 0 && ny <= 8))){
cube[nx][ny].setHaveBomb();
counter ++;
}
}

}
void checkAndSetNearBomb(int x, int y){
int counter = 0;
if (cube[x][y].getIfHaveBomb() == true){
return;
}
for (int i = -1; i < 2; i++){
for (int j = -1; j < 2; j++){
if (!(i == 0 && j == 0) && cube[x + i][y + j].getIfHaveBomb() == true){
counter++;
}
}
}
cube[x][y].setNearHaveBombNumber(counter);
}

//display graphics
void show(){
//clear console
system("cls");
//display coordinate
showXY();
//display lid
for (int i = 0; i < MAX_X; i++){
cursorGoTo(START_X, START_Y-i);
for (int j = 0; j < MAX_Y; j++){
if (cube[i][j].getIfOpen() == true){
if (cube[i][j].getIfHaveBomb() == false){
if (cube[i][j].getNearHaveBombNumber() == 0){
printf(" ");
}
else{
printf("%d ", cube[i][j].getNearHaveBombNumber());
}
}
else{
printf("* ");
}
}
else{
printf("■");//the cube is not open display '鈻?
}
}
}
}
void showXY(){
//display coordinate
cursorGoTo(START_X, START_Y+2);
printf("X");
cursorGoTo(START_X-3, START_Y);
printf("Y");
//output X
for (int i = 0; i < 2*MAX_X; i+=2){
cursorGoTo(START_X+i, START_Y+1);
printf("%d", i/2);
}
//output Y
for (int i = 0; i < MAX_Y; i++){
cursorGoTo(START_X-1, START_Y-i);
printf("%d", i);
}
}
void cursorGoTo(int x, int y){
//obtain console cursor
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int player(){
cursorGoTo(0 , 0);
printf("Please enter (x,y), and the coordinate betweent 0 and 8, separate with whitespace");
int x, y, flag = 1, over = 0;
do{
cursorGoTo(START_X+3, START_Y+4);
scanf("%d%d", &y, &x, 2);
if (!((x >= 0 && x <= 8) && (y >= 0 && y <= 8)) && cube[x][y].getIfOpen()){
printf("Please enter again!");
}
else{
flag = 0;
}
} while (flag);
cube[x][y].setOpen();
show();
if (cube[x][y].getIfHaveBomb() == true){
printf("Unfortunately! you are losed!");
for(int i=0; i<MAX_X; i++){
for(int j=0; j<MAX_Y; j++){
cube[i][j].setOpen();
}
}
show();
return 0;
}
if (cube[x][y].getNearHaveBombNumber() == 0){
autoOpen(x, y);
show();
}
over = ifWin();
if (over){
return 0;
}
return 1;
}
void autoOpen(int x, int y){
for (int i = -1; i < MAX_X; i++){
for (int j = -1; j < MAX_Y; j++){
if ((!(i == 0 && j == 0)) && cube[x + i][y + j].getNearHaveBombNumber() == 0){
cube[x + i][y + j].setOpen();
autoOpen(x + i, y + j);
}
}
}
}
int ifWin(){
int counter = 0;
for (int i = 0; i < MAX_X; i++){
for (int j = 0; j < MAX_Y; j++){
if (cube[i][j].getIfOpen() == true && cube[i][j].getIfHaveBomb() == true){
counter++;
}
}
}
if (counter == 10){
printf("Conguratulation!you are wine!");
return 1;
}
return 0;
}

这个程序里,编译运行后,每次运行埋雷的位置都一样,怎么解决呢?使每次运行都重新计算埋雷的位置。

2个回答

devmiao
devmiao   Ds   Rxr 2016.05.08 23:49
havedream_one
havedream_one   2016.05.09 08:34

在程序一开始的地址加一个方法,这个方法实现随机计算埋雷的位置

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!