hyetpang 2016-05-08 14:30 采纳率: 39.4%
浏览 1239

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 2016-05-08 15:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)