xiaoing.com©

开花即结果 提问即答案

写一个扫雷的小游戏
原始问题:

写一个扫雷的小游戏

include

include

include

using namespace std;
const int ROW = 9; //行数
const int COL = 9; //列数
int arrROW; //存放雷的位置

void init() {//初始化,将arr数组赋值为0

for (int i = 0; i < ROW; i++) {//行 

    for (int j = 0; j < COL; j++) {//列 

        arr[i][j] = 0;

    }

}

}

void setMine() {//设置地雷的位置,将arr中对应的元素赋值为9

srand((unsigned)time(NULL));//随机数种子函数,用当前时间来生成随机数。srand必须只能执行一次!!!  

int x, y, count = 0;;//x、y代表雷的坐标。count用来计数地雷的个数  

while (count != 10) { //设定10个地雷  

    x = rand() % 9 + 1 ; //产生1-9之间的随机整数  

    y = rand() % 9 + 1 ; //产生1-9之间的随机整数  

    if (arr[x][y] == 0) { //如果这个位置上没有地雷就赋值为9  

        arr[x][y] = 9 ;     //当arr中元素为9时代表这是一颗地雷。                                count++;              }          }      }       void showMine(){          cout << " 扫 雷 游 戏 在 第 " << ROW << " 行 " << COL <<" 列 的 棋 盘 上 :"<<endl<<endl ;           for(int i=0 ;i<ROW ;i++){              cout<<"\t";              for(int j=0 ;j<COL ;j++){                  if(arr[i][j]==9){                      cout<<"*";                  }else{                      cout<<arr[i][j];                  }              }              cout<<endl;}      cout<<endl;}       void playGame(){          int x,y,flag=1,num=0 ,score=100 ,row ,col ,count=0 ,tempX ,tempY ;          char c=' ';           while(flag==1){               showMine();               cout << endl << "\t请 输 入 您 要 排 除 的 坐 标 :" << endl \                                    << "\t\tx=" ;                 cin >> row ;                   if(row<1||row>ROW){                       flag=-1;                       break;}                 else{                     tempX=row-1;}                   cout<< "\t\ty=" ;;                    cin >> col ;;                    if(col<1||col>COL){                        flag=-2;                        break;}                  else{                      tempY=col-1;}                        num++;                         if(arr[tempX][tempY]==9 ){                             flag=-3;;                             break;;                         }else{                             score--;;                         }}                       showMine();                       if(-3==flag ){                           cout<<endl \
写一个扫雷的小游戏
https://www.xiaoing.com/zhidao/10064.html
AI / 模型DeepSeek
时间 / DATEApril 15, 2023, 6:34 AM
语言zh-CN
IP / 区域广东 广州