欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言代碼實(shí)現(xiàn)掃雷小游戲

 更新時(shí)間:2020年02月07日 09:22:05   作者:格戮  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言代碼實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)掃雷小游戲的具體代碼,供大家參考,具體內(nèi)容如下

主頁(yè)面:

游戲頁(yè)面:

雖然頁(yè)面比較low,但我已經(jīng)盡力了

具體代碼實(shí)現(xiàn):

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int n;//記錄已經(jīng)探索的區(qū)域數(shù)
int x,y;//光標(biāo)的橫縱坐標(biāo)
int T;//判斷游戲是否失敗,T=1為失敗
int b[9][9];//區(qū)分每個(gè)位置的狀態(tài),0為未探索,1為已探索,2為插旗狀態(tài)
int a[9][9];//隨機(jī)生成的掃雷地圖,-1為雷
void setColor(unsigned short ForeColor=7,unsigned short BackGroundColor=0) {//控制局部區(qū)域的字體顏色和背景顏色
 HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);
}
void gotoxy(int x, int y) {
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle, pos);
}
void add(int p,int q) {//雷的一周數(shù)值+1
 for(int i=-1; i<2; i++)
 for(int j=-1; j<2; j++)
  if(p+i>=0&&p+i<9&&q+j>=0&&q+j<9&&a[p+i][q+j]>=0)
  a[p+i][q+j]++;
}
void start() {//初始化函數(shù)
 srand(time(0));
 for(int i=0; i<9; i++)
 for(int j=0; j<9; j++) {
  a[i][j]=0;
  b[i][j]=0;
 }
 x=0;
 y=0;
 T=0;
 n=10;
 while(n) {//由p和q隨機(jī)數(shù)產(chǎn)生10個(gè)坐標(biāo),生成10個(gè)雷
 int p,q;
 p=rand()%9;
 q=rand()%9;
 if(!a[p][q]) {
  n--;
  a[p][q]=-1;
  add(p,q);
 }
 }
}
void show() {//顯示圖
 gotoxy(0,0);
 for(int i=0; i<9; i++) {
 if(i==0)
  printf("┌───┬───┬───┬───┬───┬───┬───┬───┬───┐\n");
 else
  printf("├───┼───┼───┼───┼───┼───┼───┼───┼───┤\n");
 printf("│");
 for(int j=0; j<9; j++) {
  printf(" ");
  if(!b[i][j]) {
  setColor(0,7);
  if(i==x&&j==y)
   setColor(0,12);
  printf(" ");
  } else {
  if(i==x&&j==y)
   setColor(7,4);
  if(b[i][j]==-1)
   printf("│>");
  else if(a[i][j]>=0) {

   if(a[i][j])
   printf("%2d",a[i][j]);
   else
   printf(" ");
  } else
   printf("**");
  }
  setColor(7,0);
  printf("│");
 }
 printf("\n");
 if(i==8)
  printf("└───┴───┴───┴───┴───┴───┴───┴───┴───┘\n");
 }
 printf("1.探索 2.插旗\n");
}
void ss(int x1,int y1) {//探索函數(shù)
 if(b[x1][y1])
 return ;
 b[x1][y1]=1;
 n++;
 if(a[x1][y1]>0)
 return;
 if(a[x1][y1]<0) {
 T=1;
 return;
 }
 for(int i=-1; i<2; i++)
 for(int j=-1; j<2; j++)
  if(x1+i>=0&&x1+i<9&&y1+j>=0&&y1+j<9)
  ss(x1+i,y1+j);
}
void doing() {//游戲進(jìn)行流程函數(shù)
 start();
 while(n<71) {
 show();
 int t;
 t=_getch();
 switch(t) {
  case 72: {
  if(x)
   x--;
  break;
  }
  case 75: {
  if(y)
   y--;
  break;
  }
  case 77: {
  if(y<8)
   y++;
  break;
  }
  case 80: {
  if(x<8)
   x++;
  break;
  }
  case '1': {
  if(!b[x][y])
   ss(x,y);
  break;
  }
  case '2': {
  if(!b[x][y])
   b[x][y]=-1;
  else if(b[x][y]==-1)
   b[x][y]=0;
  break;
  }
 }
 if(T) {
  for(int i=0; i<9; i++)
  for(int j=0; j<9; j++)
   b[i][j]=1;
  break;
 }
 }
 show();
 if(T)
 printf("游戲失??!\n");
 if(!T)
 printf("挑戰(zhàn)成功!\n");
 printf("請(qǐng)按'0'鍵返回主頁(yè)!\n");
 int o=1;
 while(o!='0'){
 o=_getch();
 } 
}
int main() {
 int m=1;
 while(m) {
 system("cls");
 printf("┌───────────┐\n");
 printf("│  掃雷  │\n");
 printf("├───────────┤\n");
 printf("│ 1.開始游戲│\n");
 printf("├───────────┤\n");
 printf("│ 2.退出游戲│\n");
 printf("└───────────┘\n");
 int t;
 t=_getch();
 if(t=='1')
  doing();
 if(t=='2')
  break;
 }
 return 0;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論