扫雷C源代码

合集下载

C语言扫雷游戏源代码

C语言扫雷游戏源代码

/*模拟扫雷游戏*/#include <graphics.h>#include <math.h>#include <stdio.h>#include <dos.h>#include <stdlib.h>#include <conio.h>#include <alloc.h>union REGS regs;int size=15;/*用于表示每个方块的大小(正方形的边长)*/int pix,piy=50;/*pix,piy是矩阵的偏移量*/char b[2]="1";/*用于显示方格周围的雷的个数*/int pan[30][16];/*用于记录盘面的情况:0:没有、9:有雷、1~8:周围雷的个数*/int pan1[30][16];/*pan1[][]纪录当前的挖雷情况,0:没有操作、1:打开了、2:标记了*/int tt;/*纪录时间参数*/int Eflags;/*用于标记鼠标按钮的有效性,0:有效,1:无效,2:这是鼠标的任意键等于重新开始*/int Msinit();void Draw(int x,int y,int sizex,int sizey);void Facedraw(int x,int y,int sizel,int k);void Dead(int sizel,int x,int y);void Setmouse(int xmax,int ymax,int x,int y);int Msread(int *xp,int *yp,int *bup,struct time t1,int k);void Draw1(int x,int y);int Open(int x,int y);float Random();void Have(int sum,int x,int y,int xx,int yy);void Help();void Coread();void Ddraw2(int x,int y);/*下面是主函数*/main(){int mode=VGAHI,devices=VGA;/*图形模式初始化的变量*/char ams; /*鼠标操作中的标志变量*/int xms,yms,bms; /*鼠标的状态变量*/int i,j,k,k1=0; /*i,j,k是循环变量*/int x=9,y=9,flags=0; /*x,y矩阵的大小*/int sum=10; /*sum 盘面的雷的总数目,是个x,y的函数*/int x1=0,y1=0; /*用于记录光标当前的位置*/int x11=0,y11=0; /*暂时保存鼠标位置的值*/int sizel=10; /*脸的大小*/int cflags=1; /*这是菜单操作标志变量,没有弹出1,弹出0*/struct time t1={0,0,0,0}; /*时间结构体,头文件已定义*/int co[3]; /*暂时纪录历史纪录*/void far *Map; /*用于保存鼠标图片*/char name[3][20]; /*名字字符串,用于记录名字*/FILE * p; /*文件指针用于文件操作*/Msinit(); /*鼠标初始化*//*registerbgidriver(EGAVGA_driver);*/initgraph(&devices,&mode,"C:\\tc"); /*图形模式初始化*//*为图片指针分配内存*/if((Map=farmalloc(imagesize(0,0,20,20)))==NULL)/*图片的大小是20*20*/{printf("Memory ererr!\n");printf("Press any key to out!\n");exit(1);}/*用于检验文件是否完整*/while((p = fopen("score.dat", "r")) == NULL) /*如果不能打开就新建一个*/{if((p = fopen("score.dat", "w")) == NULL)/*如果不能新建就提示错误并推出*/ {printf("The file cannot open!\n");printf("Presss any key to exit!\n");getch();exit(1);}/*写入初始内容*/fprintf(p,"%d %d %d,%s\n%s\n%s\n",999,999,999,"xiajia","xiajia","xiajia"); fclose(p);}/*暂时读出历史纪录。

扫雷C源代码

扫雷C源代码

/*5.3.4源程序*/#include<graphics.h>//图形函数库 #include<stdlib.h>#include<dos.h> #defineLEFTPRESS0xff01//鼠标左键 #defineLEFTCLICK0xff10//左键单击 #defineLEFTDRAG0xff19#defineMOUSEMOVE0xff08struct{introundnum;/*统计格子周围有多少雷*/intflag;/*右键按下显示红旗的标志,0没有红旗标志}Mine[10][10];intgameAGAIN=0;/*是否重来的变量*/intgamePLAY=0;/*是否是第一次玩游戏的标志*/ intmineNUM;/*统计处理过的格子数*/charrandmineNUM[3];/*显示数字的字符串*/ intKeystate; intMouseExist; intMouseButton; intMouseX; intMouseY;voidInit(void);/*图形驱动*/voidMouseOn(void);/*鼠标光标显示*/ voidMouseOff(void);/*鼠标光标隐藏*/ voidMouseSetXY(int,int);/*设置当前位置*/intLeftPress(void);/*左键按下*/ intRightPress(void);/*鼠标右键按下*/ voidMouseGetXY(void);/*得到当前位置*/ voidControl(void);/*游戏开始,重新,关闭*/ voidGameBegain(void);/*游戏开始画面*/ voidDrawSmile(void);/*画笑脸*/voidDrawRedflag(int,int);/*显示红旗*/voidDrawEmpty(int,int,int,int);/*两种空格子的显示voidGameOver(void);/*游戏结束*/ voidGameWin(void);/*显示胜利*/intMineStatistics(int,int);/*统计每个格子周围的雷数 intShowWhite(int,int);/*显示无雷区的空白部分 voidGamePlay(void);/*游戏过程*/ voidClose(void);/*图形关闭*/ voidmain(void){Init(); Control(); Close();intnum;/*格子当前处于什么状态,1有雷,0已经显示过数字或者空白格*/,1有红旗标志 */*/*/*/口voidInit(void)/*图形开始*/{intgd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");}voidClose(void)/*图形关闭*/{closegraph();}voidMouseOn(void)/*鼠标光标显示*/{_AX=0x01;geninterrupt(0x33);}voidMouseOff(void)/*鼠标光标隐藏*/{_AX=0x02;geninterrupt(0x33);}voidMouseSetXY(intx,inty)/*设置当前位置*/{_CX=x;_DX=y;_AX=0x04;geninterrupt(0x33);}intLeftPress(void)/*鼠标左键按下*/{_AX=0x03;geninterrupt(0x33);return(_BX&1);}intRightPress(void)/*鼠标右键按下*/{_AX=0x03;geninterrupt(0x33);return(_BX&2);}voidMouseGetXY(void)/*得到当前位置*/{_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;口voidControl(void)/*游戏开始,重新,关闭*/{intgameFLAG=1;/*游戏失败后判断是否重新开始的标志*/ while(1){if(gameFLAG)/*游戏失败后没判断出重新开始或者退出游戏的话就继续判断*/{GameBegain();/*游戏初始画面*/GamePlay();/*具体游戏*/if(gameAGAIN==1)/*游戏中重新开始*/{gameAGAIN=0;continue;}}MouseOn();gameFLAG=0;if(LeftPress())/*判断是否重新开始*/{MouseGetXY();if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85){ gameFLAG=1;continue;}}if(kbhit())/*判断是否按键退出*/ break;}MouseOff();}voidDrawSmile(void)/*口笑脸*/{setfillstyle(SOLID_FILL,YELLOW);fillellipse(290,75,10,10);setcolor(YELLOW);setfillstyle(SOLID_FILL,BLACK);/*眼睛*/fillellipse(285,75,2,2);fillellipse(295,75,2,2);setcolor(BLACK);/*嘴巴*/bar(287,80,293,81);口voidDrawRedflag(inti,intj)/*显示红旗*/{setcolor(7);setfillstyle(SOLID_FILL,RED);bar(198+j*20,95+i*20,198+j*20+5,95+i*20+5);setcolor(BLACK);line(198+j*20,95+i*20,198+j*20,95+i*20+10);}voidDrawEmpty(inti,intj,intmode,intcolor)/*两种空格子的显示*/{ setcolor(color);setfillstyle(SOLID_FILL,color);if(mode==0)/*没有单击过的大格子*/bar(200+j*20-8,100+i*20-8,200+j*20+8,100+i*20+8);elseif(mode==1)/*单击过后显示空白的小格子*/bar(200+j*20-7,100+i*20-7,200+j*20+7,100+i*20+7);}voidGameBegain(void)/*游戏开始画面*/{inti,j;cleardevice(); if(gamePLAY!=1){MouseSetXY(290,70);/*鼠标一开始的位置MouseX=290; MouseY=70; }gamePLAY=1;/*下次按重新开始的话鼠标不重新初始化mineNUM=0; setfillstyle(SOLID_FILL,7); bar(190,60,390,290);for(i=0;i<10;i++)/*画格子*/ for(j=0;j<10;j++)DrawEmpty(i,j,0,8); setcolor(7);DrawSmile();/*画脸*/randomize();for(i=0;i<10;i++)/*100个格子随机赋值有没有地雷for(j=0;j<10;j++){Mine[i][j].num=random(8);/*如果随机数的结果是if(Mine[i][j].num==1)mineNUM++;/*现有雷数加1*/elseMine[i][j].num=2;Mine[i][j].flag=0;/*表示没红旗标志 }sprintf(randmineNUM,"%d",mineNUM); */setcolor(1);settextstyle(0,0,2);outtextxy(210,70,randmineNUM);mineNUM=100-mineNUM;/*变量取空白格数量MouseOn();口voidGameOver(void)/*游戏结束画面*/{inti,j;setcolor(0); for(i=0;i<10;i++) for(j=0;j<10;j++)if(Mine[i][j].num==1)/*显示所有的地雷*/{DrawEmpty(i,j,0,RED);setfillstyle(SOLID_FILL,BLACK);fillellipse(200+j*20,100+i*20,7,7); }}voidGameWin(void)/*显示胜利*/{/*显示这次总共有多,并作为它的初始坐标*/*/*/1表示这个格子有地雷*/*/ 少雷数*/setcolor(11);settextstyle(0,0,2);outtextxy(230,30,"YOUWIN!");}intMineStatistics(inti,intj)/*统计每个格子周围的雷数*/{intnNUM=0;if(i==0&&j==0)/*左上角格子的统计*/{if(Mine[0][1].num==1)nNUM++;if(Mine[1][0].num==1)nNUM++;if(Mine[1][1].num==1)nNUM++;口elseif(i==0&&j==9)/*右上角格子的统计*/{if(Mine[0][8].num==1)nNUM++;if(Mine[1][9].num==1)nNUM++;if(Mine[1][8].num==1)nNUM++;}elseif(i==9&&j==0)/*左下角格子的统计*/{if(Mine[8][0].num==1)nNUM++;if(Mine[9][1].num==1)nNUM++;if(Mine[8][1].num==1)nNUM++;}elseif(i==9&&j==9)/*右下角格子的统计*/{if(Mine[9][8].num==1)nNUM++;if(Mine[8][9].num==1)nNUM++;if(Mine[8][8].num==1)nNUM++;}elseif(j==0)/*左边第一列格子的统计*/{if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;}elseif(j==9)/*右边第一列格子的统计*/口if(Mine[i][j-1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;}elseif(i==0)/*第一行格子的统计*/{if(Mine[i+1][j].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;}elseif(i==9)/*最后一行格子的统计*/{if(Mine[i-1][j].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;}else/*普通格子的统计*/{if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;}return(nNUM);/*把格子周围一共有多少口数的统计结果返回*/ShowWhite(inti,intj)/*显示无雷区的空白部分*/{if(Mine[i][j].flag==l||Mine[i][j].num==0)/*如果有红旗或该格处理过就不对该格进行*/return;mineNUM --;/*显示过数字或者空格的格子就表示多处理了一个格子,当所有格子都处理过了表示胜利口int 任何判断*/if(Mine[i][j].roundnum==0&&Mine[i][j].num!=1)/*显示空格*/{DrawEmpty(i,j,1,7);Mine[i][j].num=0; } elseif(Mine[i][j].roundnum!=0)/*输出口数*/{ DrawEmpty(i,j,0,8);sprintf(randmineNUM,"%d",Mine[i][j].roundnum); setcolor(RED);outtextxy(195+j*20,95+i*20,randmineNUM);Mine[i][j].num=0;/*已经输出口数的格子用0表示已经用过这个格子*/ return }/*8个方向递归显示所有的空白格子*/if(i!=0&&Mine[i-1][j].num!=1)ShowWhite(i-1,j);if(i!=0&&j!=9&&Mine[i-1][j+1].num!=1)ShowWhite(i-1,j+1); if(j!=9&&Mine[i][j+1].num!=1) if(j!=9&&i!=9&&Mine[i+1][j+1].num!=1) if(i!=9&&Mine[i+1][j].num!=1) if(i!=9&&j!=0&&Mine[i+1][j-1].num!=1)if(j!=0&&Mine[i][j-1].num!=1) if(i!=0&&j!=0&&Mine[i-1][j-1].num!=1)口voidGamePlay(void)/*游戏过程*/{inti,j,Num;/*Num 用来接收统计函数返回一个格子周围有多少地雷for(i=0;i<10;i++)for(j=0;j<10;j++)Mine[i][j].roundnum 二MineStatistics(i,j);/*统计每个格子周围有多少地雷*/while(!kbhit()){if(LeftPress())/*鼠标左键盘按下*/{ MouseGetXY();if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)/*重新来*/{MouseOff();gameAGAIN=1;break;}if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置格*/{j 二(MouseX-190)/20;/*x 坐标*/ i 二(MouseY-90)/20;/*y 坐标*/if(Mine[i][j].flag==1)/*如果格子有红旗则左键无效*/continue;if(Mine[i][j].num!=0)/*如果格子没有处理过*/{ShowWhite(i,j+1); ShowWhite(i+1,j+1);ShowWhite(i+1,j); ShowWhite(i+1,j-1); ShowWhite(i,j-1);*/子范if(Mine[i][j].num==1)/*鼠标按下的格子是地雷*/{MouseOff();GameOver();/*游戏失败*/break;}else/*鼠标按下的格子不是地雷*/{MouseOff();Num=MineStatistics(i,j);if(Num==0)/*周围没地雷就用递归算法来显示空白格子*/ ShowWhite(i,j);else/*按下格子周围有地雷*/{sprintf(randmineNUM,"%d",Num);/*输出当前格子周围的雷数*/setcolor(RED);outtextxy(195+j*20,95+i*20,randmineNUM);mineNUM-;}MouseOn();Mine[i][j].num=0;/*点过的格子周围雷数的数字变为0表示这个格子已经用过*/if(mineNUM<1)/*胜利了*/{GameWin();break;}}}}}if(RightPress())/*鼠标右键键盘按下*/{MouseGetXY();if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置在格范围*/{j二(MouseX-190)/20;/*x坐标*/i二(MouseY-90)/20;/*y坐标*/MouseOff();if(Mine[i][j].flag==0&&Mine[i][j].num!=0)/*本来没红旗现在显示红旗*/{DrawRedflag(i,j);Mine[i][j].flag=1;}elseif(Mine[i][j].flag==1)/*有红旗标志再按右键就红旗消失*/{DrawEmpty(i,j,0,8);Mine[i][j].flag=0;}}MouseOn();sleep(1);}}}文案大全。

c扫雷课程设计源代码

c扫雷课程设计源代码

c 扫雷课程设计源代码一、教学目标本课程的目标是让学生掌握C语言编程中的扫雷游戏开发。

通过本课程的学习,学生将能够理解并运用C语言的基本语法和数据结构,编写出功能完整的扫雷游戏。

知识目标:使学生掌握C语言的基本语法、数据类型、运算符、控制结构、函数、数组和指针等知识。

技能目标:培养学生运用C语言解决实际问题的能力,学会设计并实现一个简单的扫雷游戏。

情感态度价值观目标:培养学生对计算机编程的兴趣,增强其自主学习和创新意识,使其体验到编程带来的成就感。

二、教学内容本课程的教学内容主要包括C语言的基本语法、数据类型、运算符、控制结构、函数、数组和指针等知识,以及如何运用这些知识编写扫雷游戏的源代码。

教学大纲安排如下:1.第一章:C语言基础知识,介绍C语言的基本语法、数据类型、运算符和控制结构。

2.第二章:函数与指针,讲解函数的定义和调用、指针的基本概念和运用。

3.第三章:数组与字符串,介绍数组的概念和应用、字符串的操作。

4.第四章:数据结构,讲解链表、栈和队列的基本概念及其在扫雷游戏中的应用。

5.第五章:扫雷游戏设计与实现,引导学生运用所学知识编写扫雷游戏的源代码。

三、教学方法本课程将采用讲授法、案例分析法和实验法等多种教学方法相结合的方式进行教学。

1.讲授法:用于讲解C语言的基本语法、数据类型、运算符、控制结构、函数、数组和指针等知识。

2.案例分析法:通过分析典型的扫雷游戏案例,使学生掌握数据结构在游戏开发中的应用。

3.实验法:引导学生动手编写扫雷游戏的源代码,培养其实际编程能力。

四、教学资源本课程的教学资源包括教材、参考书、多媒体资料和实验设备等。

1.教材:选用《C程序设计》等相关教材,作为学生学习的基础资源。

2.参考书:推荐《C语言深度剖析》等参考书,帮助学生深入了解C语言的内部机制。

3.多媒体资料:提供在线编程教程、扫雷游戏案例视频等,丰富学生的学习方式。

4.实验设备:为学生提供计算机、编程环境等实验设备,以便进行编程实践。

原创C语言扫雷程序大作业代码

原创C语言扫雷程序大作业代码
{
int a;
a=srdng(x,y);
if(a!=0)
{
tru[x-1][y-1]=a;
tbl[x+1][y+1]=a;
}
else
{
tru[x-1][y-1]='O';
tbl[x+1][y+1]='O';
jdg_4(x-1,y-1);
jdg_4(x,y-1);
jdg_4(x+1,y-1);
}
return 0;
scanf("%d",&z);
if (z==3)//--------------------------------待定
{
tbl[x+1][y+1]='?';
dspl();
}
else
if(z==2)//-----------------------------标记
{
tbl[x+1][y+1]='P';
{
if((x>=1)&&(x<=10)&&(y>=1)&&(y<=10)&&(tru[x-1][y-1]='_'))
anls_1(x,y);
return 0;
}
int anls_2(int x,int y)//-------------------------------------上方境况分析(空白点的迭代)
{1,'|','_','_','_','_','_','_','_','_','_','_'},

c语言扫雷程序代码

c语言扫雷程序代码
for(j=0;j<2;j++) if(b[x+i][y+j]==11) n++;
if(n==0) {
for(i=0;i<2;i++) for(j=0;j<2;j++) { if(b[x+i][x+j]==10) { b[x+i][y+j]=0; printf_0(b,x+i,y+j); } }
} else b[x][y]=n; } else if(x==0&&y<Map_list-1&&y>0) { n=0; for(i=0;i<2;i++)
for(j=-1;j<2;j++) if(b[x+i][y+j]==11) n++;
if(n!=0) b[x][y]=n;
else for(i=0;i<2;i++)
for(j=-1;j<2;j++) {
if(b[x+i][y+j]==10) {
b[x+i][y+j]=0; printf_0(b,x+i,y+j); } } } else if(x==0&&y==Map_list-1) { n=0; for(i=0;i<2;i++) for(j=-1;j<1;j++) if(b[x+i][y+j]==11) n++; if(n!=0) b[x][y]=n; else for(i=0;i<2;i++) for(j=-1;j<1;j++) { if(b[x+i][y+j]==10) { b[x+i][y+j]=0; printf_0(b,x+i,y+j); } } } else if(y==Map_list-1&&x!=0&x!=Map_line-1) { n=0; for(i=-1;i<2;i++) for(j=-1;j<1;j++) if(b[x+i][y+j]==11) n++; if(n!=0) b[x][y]=n; else for(i=-1;i<2;i++) for(j=-1;j<1;j++) { if(b[x+i][y+j]==10) {

扫雷c语言程序设计

扫雷c语言程序设计

扫雷c语言程序设计以下是一个简单的扫雷游戏的C语言程序设计示例:c复制代码#include <stdio.h>#include <stdlib.h>#include <time.h>#define ROWS 9#define COLS 9#define MINE 10#define UNKNOWNS 0#define EMPTY 0#define MARKED 255int board[ROWS][COLS];int numMines;int numFlags;int numUnknowns;int revealed[ROWS][COLS];void initBoard() {srand(time(NULL));numMines = ROWS * COLS - 1;for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {board[i][j] = (rand() % (MINE + 1)) + 1; if (board[i][j] == MINE) {numMines--;} else if (rand() % 2 == 0) {board[i][j] = UNKNOWNS;numUnknowns++;} else {board[i][j] = EMPTY;}}}void reveal(int row, int col) {if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] != MARKED) { if (board[row][col] == MINE) {printf("Game over!\n");exit(0);} else if (board[row][col] == UNKNOWNS) {numUnknowns--;for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {if (revealed[i][j] == 0 && isSafe(i, j)) {reveal(i, j);}}} else if (board[row][col] == EMPTY) { revealed[row][col] = 1;} else {printf("You hit a mine!\n");exit(0);}}}int isSafe(int row, int col) {int count = 0;for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {if ((i == row && j == col) || board[i][j] ==MARKED) continue;if ((i - row) * (i - row) + (j - col) * (j - col) <= numFlags * numFlags) {count++;} else if (board[i][j] != EMPTY) {return 0; // not safe, there is a mine here.}}}return count == numFlags + 1; // safe if all adjacent cells are either mines or flags.}。

扫雷游戏源代码

扫雷游戏源代码
int boo[maxcell][maxcell];
int x,y,z,cc;
bool lei=false;
now_time = time(NULL);
h=now_time;
cc=c;
//初始化数组
for(int i=0;i<maxcell;i++)
for(int y=0;y<maxcell;y++)
void cell(int a,int b,int c)
{
//
time_t now_time;
time_t h;
srand( (unsigned)time( NULL ) );
initgraph((a+2)*cel,(b+5)*cel);
MOUSEMSG m;
int mimi[maxcell][maxcell];
case WM_RBUTTONDOWN:
x=m.x/cel-1;
y=m.y/cel-1;
if(mimi[x][y]==0)
{
mimi[x][y]=2;
cc--;
}
else if(mimi[x][y]==2)
{
/////////////////////////
//@挚爱
//vc6.0 EasyX库
///////////////////////////
#include<graphics.h>
#include <conio.h>
#include<time.h>
#include <stdio.h>

扫雷算法c语言

扫雷算法c语言

扫雷算法c语言扫雷算法的C语言实现可以有很多种,以下是一个简单的示例:```cinclude <>include <>define SIZE 10typedef struct {int mines;int numbers;int cells;} Game;void initializeGame(Game game) {game->mines = SIZE SIZE - SIZE;game->numbers = 0;game->cells = (int )malloc(SIZE SIZE sizeof(int));for (int i = 0; i < SIZE SIZE; i++) {game->cells[i] = -1;}for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {if (rand() % 2 == 0) { // Place a minegame->cells[i SIZE + j] = 0;game->mines--;} else { // Place a number or a blank spacegame->cells[i SIZE + j] = rand() % 3 + 1;game->numbers++;}}}}void printGame(Game game) {for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {if (game->cells[i SIZE + j] == -1) { // Blank space printf(" ");} else if (game->cells[i SIZE + j] == 0) { // Mineprintf("");} else { // Number or question markprintf("%d", game->cells[i SIZE + j]);}}printf("\n");}}void solveGame(Game game) {for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {if (game->cells[i SIZE + j] == -1) { // Blank space, check surrounding cells to see if they are mines or numbersif (i > 0 && game->cells[(i - 1) SIZE + j] != 0 && game->cells[(i - 1) SIZE + j] != game->cells[i SIZE + j]) { // Check above cellgame->cells[i SIZE + j] = game->cells[(i - 1) SIZE + j]; // Set this cell to the same value as the above cell (mine or number)} else if (i < SIZE - 1 && game->cells[(i + 1) SIZE + j] != 0 && game->cells[(i + 1) SIZE + j] != game->cells[i SIZE + j]) { // Check below cellgame->cells[i SIZE + j] = game->cells[(i + 1) SIZE + j]; // Set this cell to the same value as the below cell (mine or number) } else if (j > 0 && game->cells[i SIZE + (j - 1)] != 0 && game->cells[i SIZE + (j - 1)] != game->cells[i SIZE + j]) { // Check left cellgame->cells[i SIZE + j] = game->cells[i SIZE + (j - 1)]; // Set this cell to the same value as the left cell (mine or number)} else if (j < SIZE - 1 && game->cells[i SIZE + (j + 1)] != 0 && game->cells[i SIZE + (j + 1)] != game->cells[i SIZE + j]) { // Check right cellgame->cells[i SIZE + j] = game->cells[i SIZE + (j + 1)]; // Set this cell to the same value as the right cell (mine or number)} else if (game->numbers > 0) { // If there are still numbers in the game, place a number here if it's a blank space, and reduce the number of numbers by one. Otherwise, place a mine and reduce the number of mines by one.game->cells[i SIZE + j] = rand() % 3 + 1; // Place a number here if it's a blank space, and reduce the number。

用c写的扫雷

用c写的扫雷

#include<stdio.h>#include<time.h>#include<stdlib.h>#define NUM 10typedef struct _mine{char val;int show;int safe;}mine;void init(mine arr[10][10]){int i,j,n,k,h;for(i=0;i<10;i++){for(j=0;j<10;j++){arr[i][j].safe=0;arr[i][j].show=0;arr[i][j].val=' ';}}srand((int)time(0));n=0;while(n<NUM){i=rand()%10;j=rand()%10;if(arr[i][j].val == '@'){continue;}arr[i][j].val='@';n++;}for(i=0;i<10;i++){for(j=0;j<10;j++){if(arr[i][j].val==' '){n=0;for(k=((i-1)>0 ? (i-1):0);k<=((i+1)>9 ? 9 :(i+1));k++){for(h=((j-1)>0?(j-1):0);h<=((j+1)>9?9:(j+1));h++){if(arr[k][h].val == '@'){++n;}}}if(n!=0){arr[i][j].val = 48+n ;}}}}}void show(mine arr[10][10]){int i,j;printf(" ");for(i=0;i<10;i++){printf("%4d",i);}printf("\n");printf(" -----------------------------------------\n");for(i=0;i<10;i++){printf(" %d | ",i);for(j=0;j<10;j++){if(arr[i][j].show==0){printf("%c",'#');}else if(arr[i][j].show==1){printf("%c",arr[i][j].val);}else if(arr[i][j].show ==2 ){printf("c",'?');}printf(" | ");}printf("\n");printf(" +---+---+---+---+---+---+---+---+---+---+\n");}}int jieshu(mine arr[10][10]){int i,j,sum=0;for(i=0;i<NUM;i++){for(j=0;j<NUM;j++){if(arr[i][j].show == 1)sum++;}}return sum;}void kongbaiqu(mine arr[10][10],int x,int y){int k,h;if(arr[x][y].safe==0){arr[x][y].safe = 1;for(k=((x-1)>0 ? (x-1):0);k<=((x+1)>9 ? 9 :(x+1));k++){for(h=((y-1)>0?(y-1):0);h<=((y+1)>9?9:(y+1));h++){if(arr[x][y].val==' '){arr[x][y].show=1;kongbaiqu( arr, k,h);}else if(arr[x][y].val != '@'){arr[x][y].show = 1;}}}}}int NUM_char_mine(mine arr[10][10]){int i,j,sum =0;for(i=0;i<NUM;i++){for(j=0;j<NUM;j++){if( arr[i][j].val=='?'){sum = sum+1;}}}return (NUM-sum);}void main(){mine arr[10][10];int oper,x,y;char ch;time_t start_time,end_time;init(arr);time(&start_time);while(1){time(&end_time);system("cls");show(arr);printf("\n 地雷:%d\t\t\t 时间:%d 秒\n",NUM_char_mine(arr),end_time-start_time);printf("\n提示: 1为安全,0为地雷,格式(1/0 x y)");scanf("%d %d %d",&oper,&x,&y);if(oper==1){arr[x][y].show =1;if(arr[x][y].val == '@'){printf("\n 恭喜你,你运气真好。

c语言扫雷源代码

c语言扫雷源代码

}
void printField() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (revealed[i][j] == 1) { if (mineField[i][j] == -1) { printf("* ");
} else { printf("%d ", mineField[i][j]);
} } else {
printf(". "); } } printf("\n"); } }
int countMines(int row, int col) { if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || mineField[row][col] !=
initializeField(); placeMines();
int gameOver = 0; while (!gameOver) {
printField();
int row, col; printf("Enter row and column (0-%d): ", SIZE - 1); scanf("%d %d", &row, &col);
void initializeField() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { mineField[i][j] = 0; revealed[i][j] = 0; } }

扫雷小游戏代码c版

扫雷小游戏代码c版

intlei;intline,arrange,thunder1,thunder2,space;voidsaolei(char**&a,char**&c,int**&b,int**&d,int**&e,int**&g,int&line,int&a rrange,int&thunder1,int&thunder2,int&space){intall=1;while(all){inti,j;a=newchar*[line],c=newchar*[line],b=newint*[line],d=newint*[line],e=newint*[ line],g=newint*[line];for(i=0;i<=line-1;i++)a[i]=newchar[arrange],c[i]=newchar[arrange],b[i]=newint[arrange],d[i]=newint[arrange],e[i]=newint[arrange],g[i]=newint[arrange];for(i=0;i<line;i++)for(j=0;j<arrange;j++)a[i][j]='.',b[i][j]=0,c[i][j]='.',e[i][j]=0,g[i][j]=0;intk;srand(int(time(0)));intz=1;while(z){k=rand()%(thunder2+1);if(k>=thunder1)z=0;}srand(int(time(0)));for(i=1;i<=k;i++){ints,t;L:{s=rand()%line;t=rand()%arrange;}if(!(s>=0&&s<line&&t>=0&&t<arrange&&a[s][t]!='#')) gotoL;for(intm=0;m<line;m++)for(intn=0;n<arrange;n++){if(m==s&&n==t&&a[m][n]!='#')a[m][n]='#';}}for(i=0;i<line;i++)for(j=0;j<arrange;j++){if(j-1>=0&&a[i][j-1]=='#')b[i][j]++;if(j+1<arrange&&a[i][j+1]=='#')b[i][j]++;if(i-1>=0&&a[i-1][j]=='#')b[i][j]++;if(i+1<line&&a[i+1][j]=='#')b[i][j]++;if(i-1>=0&&j+1<arrange&&a[i-1][j+1]=='#') b[i][j]++;if(i-1>=0&&j-1>=0&&a[i-1][j-1]=='#')b[i][j]++;if(i+1<line&&j+1<arrange&&a[i+1][j+1]=='#') b[i][j]++;if(j-1>=0&&i+1<line&&a[i+1][j-1]=='#')b[i][j]++;}intf=0;for(i=0;i<line;i++)for(j=0;j<arrange;j++){if(a[i][j]=='#')d[i][j]=2;else{if(j-1>=0&&a[i][j-1]=='.')f++;if(j+1<arrange&&a[i][j+1]=='.')f++;if(i-1>=0&&a[i-1][j]=='.')f++;if(i+1<line&&a[i+1][j]=='.')f++;if(i-1>=0&&j+1<arrange&&a[i-1][j+1]=='.')f++;if(i-1>=0&&j-1>=0&&a[i-1][j-1]=='.')f++;if(i+1<line&&j+1<arrange&&a[i+1][j+1]=='.')f++;if(j-1>=0&&i+1<line&&a[i+1][j-1]=='.')f++;if(i-1>=0&&i+1<line&&j-1>=0&&j+1<arrange){if(f==8)d[i][j]=1;elsed[i][j]=0;}elseif(i==0&&j==0||i==0&&j==arrange-1||i==line-1&&j==0||i==line-1&&j==arrange-1){if(f==3)d[i][j]=1;elsed[i][j]=0;}else{if(i==0&&j!=0&&j!=arrange-1||i==line-1&&j!=arrange-1&&j!=0||i!=line-1&&i!=0&&j==0||i!=line-1&&i!=0&&j==arrange-1){if(f==5)d[i][j]=1;elsed[i][j]=0;}}f=0;}}intx,y,left=0,right=0,hang,shu,duan=0,jishu=0;z=1;for(i=1;i<=space;i++)cout<<" ";for(i=0;i<=line;i++)if(i<=9)cout<<i<<" ";elsecout<<i;cout<<endl;for(i=0;i<line;i++){if(i<9){for(k=1;k<=space;k++)cout<<" ";}if(i>=9){for(k=1;k<=space-1;k++)cout<<" ";}cout<<i+1<<" ";for(j=0;j<arrange;j++)cout<<c[i][j]<<" ";cout<<endl;}while(z){cout<<"请输入你要翻开的位置(如:6 0或5 6 1):"; cin>>x>>y>>lei;x--,y--;if(a[x][y]=='#')e[x][y]=1;if(a[x][y]=='.'){if(b[x][y]!=0)e[x][y]=1;if(b[x][y]==0){for(i=0;i<line&&duan==0;i++)for(j=0;j<arrange&&duan==0;j++)if(d[i][j]==1){d[i][j]=3;intdir=3,fu=0,pan=1,ci=0;intm=i,n=j;do{switch(dir){case1 ://向左走{if(m-1>=0&&d[m-1][n]==1) {d[m-1][n]=3;m--;dir=2;}//检测所在位置右边elseif(n-1>=0&&d[m][n-1]==1) {d[m][n-1]=3;n--;dir=1;}//检测所在位置前方elseif(m+1<line&&d[m+1][n]==1) {d[m+1][n]=3;m++;dir=4;}//检测所在位置左边elseif(n+1<arrange&&d[m][n+1]==1){d[m][n+1]=3;n++;dir=3;}//检测所在位置后方elseif(m-1>=0&&d[m-1][n]==3) {m--;dir=2;}//检测所在位置右边elseif(n-1>=0&&d[m][n-1]==3) {n--;dir=1;}//检测所在位置前方elseif(m+1<line&&d[m+1][n]==3) {m++;dir=4;}//检测所在位置左边else{if(n+1<arrange&&d[m][n+1]==3){n++;dir=3;}}//检测所在位置后方}break;case2 ://向上走elseif(m-1>=0&&d[m-1][n]==1) {d[m-1][n]=3;m--;dir=2;}//检测所在位置前方elseif(n-1>=0&&d[m][n-1]==1) {d[m][n-1]=3;n--;dir=1;}//检测所在位置左边elseif(m+1<line&&d[m+1][n]==1){d[m+1][n]=3;m++;dir=4;}//检测所在位置后方elseif(n+1<arrange&&d[m][n+1]==3) {n++;dir=3;}//检测所在位置右边elseif(m-1>=0&&d[m-1][n]==3) {m--;dir=2;}//检测所在位置前方elseif(n-1>=0&&d[m][n-1]==3) {n--;dir=1;}//检测所在位置左边else{if(m+1<line&&d[m+1][n]==3){m++;dir=4;}}//检测所在位置后方}break;case3 ://向右走{if(m+1<line&&d[m+1][n]==1) {d[m+1][n]=3;m++;dir=4;}//检测所在位置右边elseif(n+1<arrange&&d[m][n+1]==1) {d[m][n+1]=3;n++;dir=3;}//检测所在位置前方elseif(m-1>=0&&d[m-1][n]==1) {d[m-1][n]=3;m--;dir=2;}//检测所在位置左边elseif(n-1>=0&&d[m][n-1]==1){d[m][n-1]=3;n--;dir=1;}//检测所在位置后方elseif(m+1<line&&d[m+1][n]==3) {m++;dir=4;}//检测所在位置右边elseif(n+1<arrange&&d[m][n+1]==3) {n++;dir=3;}//检测所在位置前方elseif(m-1>=0&&d[m-1][n]==3) {m--;dir=2;}//检测所在位置左边else{if(n-1>=0&&d[m][n-1]==3){n--;dir=1;}}//检测所在位置后方}break;case4 ://向下走{if(n-1>=0&&d[m][n-1]==1) {d[m][n-1]=3;n--;dir=1;}//检测所在位置右边方elseif(n+1<arrange&&d[m][n+1]==1) {d[m][n+1]=3;n++;dir=3;}//检测所在位置左边elseif(m-1>=0&&d[m-1][n]==1){d[m-1][n]=3;m--;dir=2;}//检测所在位置后方elseif(n-1>=0&&d[m][n-1]==3) {n--;dir=1;}//检测所在位置右边elseif(m+1<line&&d[m+1][n]==3) {m++;dir=4;}//检测所在位置前方elseif(n+1<arrange&&d[m][n+1]==3) {n++;dir=3;}//检测所在位置左边else{if(m-1>=0&&d[m-1][n]==3){m--;dir=2;}}//检测所在位置后方}break;}if(d[m][n]==3)ci++;if(ci>=line*arrange)pan=0;if(m==i&&n==j){fu++;if(fu==4)pan=0;}if(m==x&&n==y)left=i+1,right=j+1,duan=1;}while(pan);if(left==0&&right==0)for(hang=0;hang<line;hang++)}left=0,right=0,duan=0;for(i=0;i<line;i++)for(j=0;j<arrange;j++)if(d[i][j]==3)e[i][j]=1;for(i=0;i<line;i++)for(j=0;j<arrange;j++)if(d[i][j]==3){if(j-1>=0)e[i][j-1]=1;if(j+1<arrange)e[i][j+1]=1;if(i-1>=0)e[i-1][j]=1;if(i+1<line)e[i+1][j]=1;if(i-1>=0&&j+1<arrange)e[i-1][j+1]=1;for(shu=0;shu<arrange;shu++) if(d[hang][shu]==3)d[hang][shu]=1;if(i-1>=0&&j-1>=0)e[i-1][j-1]=1;if(i+1<line&&j+1<arrange)e[i+1][j+1]=1;if(j-1>=0&&i+1<line)e[i+1][j-1]=1;}}}//当b[x][y]==0时system("cls");//清屏for(i=0;i<line;i++)for(j=0;j<arrange;j++)if(d[i][j]==3)d[i][j]=1;if(a[x][y]=='.'&&b[x][y]!=0&&lei!=2) {for(i=1;i<=space;i++)cout<<" ";for(i=0;i<=line;i++)if(i<=9)cout<<i<<" ";elsecout<<i;cout<<endl;for(i=0;i<line;i++){if(i<9){for(k=1;k<=space;k++) cout<<" ";cout<<i+1<<" ";}if(i>=9){for(k=1;k<=space-1;k++) cout<<" ";cout<<i+1<<" ";}for(j=0;j<arrange;j++)if(g[i][j]==1){if(a[i][j]=='#')cout<<a[i][j]<<" "; elsecout<<b[i][j]<<" "; }elseif(i==x&&y==j)cout<<b[i][j]<<" ";elsecout<<c[i][j]<<" ";cout<<endl;}if(lei==1){jishu++;if(jishu<=3)cout<<"提示:判断错误"<<jishu<<"次,若判断错误超过三次,你将输掉游戏,注意哦o(︶︿︶)o"<<endl;}}//第一种情况if(a[x][y]=='.'&&b[x][y]==0&&lei!=2){for(i=1;i<=space;i++)cout<<" ";for(i=0;i<=line;i++)if(i<=9)cout<<i<<" ";elsecout<<i;cout<<endl;for(i=0;i<line;i++){if(i<9){for(k=1;k<=space;k++)cout<<" ";cout<<i+1<<" ";}if(i>=9){for(k=1;k<=space-1;k++)cout<<" ";cout<<i+1<<" ";}for(j=0;j<arrange;j++)if(g[i][j]==1){if(a[i][j]=='#')cout<<a[i][j]<<" ";elsecout<<b[i][j]<<" ";}elseif(e[i][j]==1)cout<<b[i][j]<<" ";elsecout<<c[i][j]<<" ";cout<<endl;}if(lei==1){jishu++;if(jishu<=3)cout<<"提示:判断错误"<<jishu<<"次,若判断错误超过三次,你将输掉游戏,注意哦o(︶︿︶)o"<<endl;}}//第二种情况if(a[x][y]=='#'&&lei==0) {for(i=1;i<=space;i++) cout<<" ";for(i=0;i<=line;i++)if(i<=9)cout<<i<<" ";elsecout<<i;cout<<endl;for(i=0;i<line;i++){if(i<9){for(k=1;k<=space;k++) cout<<" ";cout<<i+1<<" ";}if(i>=9){for(k=1;k<=space-1;k++) cout<<" ";cout<<i+1<<" ";}for(j=0;j<arrange;j++)if(g[i][j]==1){if(a[i][j]=='#')cout<<a[i][j]<<" ";elsecout<<b[i][j]<<" ";}elseif(e[i][j]==1)cout<<a[i][j]<<" ";elseif(a[i][j]=='#')cout<<a[i][j]<<" ";elsecout<<c[i][j]<<" ";cout<<endl;}cout<<"oh my god你输了!所有雷的位置已显示出,请再接再厉哦(*^__^*)嘻嘻……"<<endl;z=0;}//第三种情况if(a[x][y]=='#'&&lei==1){for(i=1;i<=space;i++)cout<<" ";for(i=0;i<=line;i++)if(i<=9)cout<<i<<" ";elsecout<<i;for(i=0;i<line;i++){if(i<9){for(k=1;k<=space;k++) cout<<" ";cout<<i+1<<" ";}if(i>=9){for(k=1;k<=space-1;k++) cout<<" ";cout<<i+1<<" ";}for(j=0;j<arrange;j++)if(g[i][j]==1){if(a[i][j]=='#')cout<<a[i][j]<<" "; elsecout<<b[i][j]<<" "; }elseif(e[i][j]==1)cout<<a[i][j]<<" "; elsecout<<c[i][j]<<" ";}}//第四种情况if(lei==2||lei==3||lei==4)z=0,all=0;for(i=0;i<line;i++)for(j=0;j<arrange;j++)if(e[i][j]==1)g[i][j]=e[i][j];for(i=0;i<line;i++)for(j=0;j<arrange;j++)e[i][j]=0;intsum=1;for(i=0;i<line;i++)for(j=0;j<arrange;j++)sum*=g[i][j];if(sum!=0){cout<<"oh good你赢了耶!( ^_^ )不错嘛"<<endl; z=0;}inttotal=1;for(i=0;i<line;i++)for(j=0;j<arrange;j++)if(a[i][j]=='#'){if(g[i][j]==1)total*=1;if(g[i][j]!=1)total*=0;}if(total!=0&&sum==0){cout<<"oh good你赢了耶!( ^_^ )不错嘛"<<endl;z=0;}if(jishu>3){cout<<"how pitty!错误判断超过三次,你输了,下次注意哦(*^__^*)"<<endl; z=0;}}//循环并判断是否继续循环}}intmain(){L:{cout<<"游戏名称:扫雷"<<'\n'<<"--------------------------------------------------------------------------------"<<'\n'<<"说明:.代表未翻开的地方;#表示雷;翻开地方显示的数字表示:该地方四周的八个相邻的地方含有雷的总数"<<'\n'<<"--------------------------------------------------------------------------------"<<'\n'<<"规则:根据翻开地方显示的数字判断雷所在的地方"<<'\n'<<"--------------------------------------------------------------------------------"<<'\n'<<"操作:根据判断,请输入位置(如:<<'\n'<<"--------------------------------------------------------------------------------"<<'\n'<<"解释:输入的三个数字中,前两个数字表示位置,如:表示行数,表示列数;第三个表示判断与选择,--无雷,--有雷,--再来一局,--结束游戏,--重启整个游戏系统"<<'\n'<<"--------------------------------------------------------------------------------"<<'\n'<<"例如:5 6 0表示游戏者认为该处无雷,6 1表示游戏者认为该处有雷,6 2表示再来一局,6 3表示结束游戏,6 4表示重启游戏系统"<<'\n'<<"--------------------------------------------------------------------------------"<<endl;intchoice,i;char**a=NULL,**c=NULL;int**b=NULL,**d=NULL,**e=NULL,**g=NULL;cout<<"游戏等级有五:"<<'\n'<<"1--茅塞未开(方格x7,雷数-->5)"<<'\n'<<"2--七窍通六(方格x10,雷数-->10)"<<'\n'<<"3--闲庭信步(方格x13,雷数-->15)"<<'\n'<<"4--炉火纯青(方格x15,雷数-->30)"<<'\n'<<"5--偶滴神呀(方格x25,雷数-->100)"<<'\n'<<"6--自定义难易程度"<<endl;cout<<"请选择:";cin>>choice;if(choice==1)line=7,arrange=7,thunder1=3,thunder2=5,space=33;if(choice==2)line=10,arrange=10,thunder1=7,thunder2=10,space=30;if(choice==3)line=13,arrange=13,thunder1=10,thunder2=15,space=27;if(choice==4)line=15,arrange=15,thunder1=15,thunder2=30,space=25;if(choice==5)line=25,arrange=25,thunder1=50,thunder2=100,space=15;if(choice==6){cout<<"请输入方格的行(行<=39):";cin>>line;cout<<"请输入方格的列(列<=39):";cin>>arrange;cout<<"希望出现雷的个数的范围(如:5):";cin>>thunder1>>thunder2;space=40-arrange;}if(choice<=0||choice>6||line<=0||arrange<=0||thunder2<thunder1||thunder2 >line*arrange){lei=3;gotoM;}saolei(a,c,b,d,e,g,line,arrange,thunder1,thunder2,space);if(lei==2)saolei(a,c,b,d,e,g,line,arrange,thunder1,thunder2,space);M:{if(lei==3)cout<<"*******游戏结束,欢迎下次使用********"<<endl;}if(lei==4){system("cls");gotoL;}for(i=0;i<=line-1;i++)delete[]a[i],delete[]b[i],delete[]c[i],delete[]d[i],delete[]e[i],delete[]g[i];delete[]a, delete[]b,delete[]c,delete[]d,delete[]e,delete[]g;a=NULL,b=NULL,c=NULL,d=NULL,e=NULL,g=NULL;}}。

C语言扫雷游戏代码及详细注释

C语言扫雷游戏代码及详细注释

C语言扫雷游戏代码及详细注释扫雷游戏代码及详细注释扫雷游戏做为c语言初学者的编程练习还是很不错的,那么本篇文章详细注释了扫雷代码中的各行代码的含义以及作用,对初学者来说还是很有帮助的。

一、game.h头文件代码1.#ifndef __GAME_H__2.#define __GAME_H__3.4.#define COLS 11 //列数5.#define ROWS 11 //行数6.#define COL (COLS-2)7.#define ROW (ROWS-2)8.#define MAX 10 //雷的个数9.10.#include<stdio.h>11.#include<stdlib.h>12.#include<time.h>13.#include<string.h>//memset头文件14.15.void init_board(char mine[ROWS][COLS], int row, int col);//声明初始化棋盘函数,在game.c实现16.void set_mine(char mine[ROWS][COLS]); //声明布雷函数17.void display(char mine[ROWS][COLS], int row, int col);//声明打印棋盘函数18.int get_mine_count(char mine[ROWS][COLS], int x, int y);//声明统计坐标周围雷的数目函数19.20.#endif//__GAME_H__二、game.c代码1.#define _CRT_SECURE_NO_WARNINGS 12.3.#include'game.h'4.5.void init_board(char mine[ROWS][COLS], char set, int row, int col)//初始化棋盘函数为set6.{7.memset(mine, set, row*col*sizeof(mine[0][0])); //memset 本身就有初始化功能括号内容依次为:8.} //数组名,初始化目标,总共初始化字节9.10.void set_mine(char mine[ROWS][COLS])//随机布雷函数11.{12.int count = MAX;//设置计数器,记录要布入雷的个数13.while(count>0)14.{15.int x = rand()%9+1;//产生1到9的随机数字16.int y = rand()%9+1;17.if((mine[x][y]) == '0') //判断同一位置是否重复布雷18.{19.mine[x][y] = '1';20.count--; //每成功布雷一颗,计数器就减一21.}22.}23.}24.25.void display(char mine[ROWS][COLS], int row, int col)//打印棋盘函数26.{27.int i = 0;28.int j = 0;29.printf(' ');//两个空格,为了使对齐,一个留给列号,一个留给棋盘30.for(i=1; i<=row-2; i++)//打印列号31.{32.printf('%d ', i);33.}34.printf('\n');35.for(i = 1; i<=row-2; i++)//输出的是棋盘信息36.{37.printf('%d ', i);38.for(j=1; j<=col-2; j++)39.{40.printf('%c ', mine[i][j]);//41.}42.printf('\n');43.}44.}45.46.int get_mine_count(char mine[ROWS][COLS], int x, int y)//统计坐标周围雷数目的函数47.{48.return mine[x-1][y-1]49.+mine[x][y-1]+mine[x+1][y-1]50.+mine[x+1][y]+mine[x+1][y+1]51.+mine[x][y+1]+mine[x-1][y+1]52.+mine[x-1][y]-8*'0'; //由于数组中存放的是字符'0',周围是8个数字,所以-8*'0'才能返回雷数的int值53.}三、test.c代码1.#define _CRT_SECURE_NO_WARNINGS 12.#include'game.h'3.4.void menu()5.{6.printf('*********************************\n');7.printf('*********1.play 0.exit***********\n');8.printf('*********************************\n');9.}10.11.enum Option //枚举,增加代码可读性12.{13.EXIT, //默认为014.PLAY //为115.};16.17.void game()18.{19.char mine[ROWS][COLS] = {0};//非雷的位置可初始化为零20.char show[ROWS][COLS] = {0};//创建一个和mine一样大的数组来存放,坐标周围雷的数目21.int x = 0;22.int y = 0;23.int win = 0;24.init_board(mine, '0', ROWS, COLS);//初始化数组,各位置放零25.init_board(show, '*', ROWS, COLS);//初始化数组,各位置放*26.27.set_mine(mine);//布雷28.display(show, ROWS, COLS);//打印棋盘29.while(win<(ROW*COL-MAX)) //非雷的个数30.{31.printf('请输入排查坐标:>');32.scanf('%d%d', &x, &y);33.if(((x>=1)&&(x<=ROWS-2))&&((y>=1)&&(y<=COLS-2)))//判断输入坐标是否在棋盘内34.{35.if(mine[x][y] == '1')36.{37.printf('很不幸,踩到地雷\n');38.break;39.}40.else //每进入一次意味着排完一颗雷41.{42.43.int count = get_mine_count(mine, x, y);//通过此函数统计指定坐标周围有几个雷44.show[x][y] = count+'0';//为了打印*,所以把display函数打印的类型改为%c打印出来的是字符。

turbo_c下的扫雷游戏源代码

turbo_c下的扫雷游戏源代码

#include<stdio.h>#include<graphics.h>#include<conio.h>#include<bios.h>#include<stdlib.h>#include<time.h>#define ESC 283#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ENTER 7181#define SPACE 14624#define WIDTH 15#define AO 0#define TU 1#define MINE -1int m_maxx,m_maxy;int m_row,m_col;int m_x0,m_y0;int *m_mine;int *m_flag;int m_num;int a[8]={-1,-1,-1,0,1,1,1,0};int b[8]={-1,0,1,1,1,0,-1,-1};int m_count;int m_countdown;int first;void init_system();void close_system();void draw_box(int,int,int,int,int,int);void draw_mine_region();void count_x0y0();void draw_title();void draw_smile(int);void draw_mine(int,int);void clean_mine();int shift_k_by_ij(int,int);void shift_ij_by_k(int,int *,int *);void shift_xy_by_ij(int i,int j,int *x,int *y);void set_mines();void disp_all_mines();void find_mine(int,int);void count_around_mines();int dig_mine1(int,int);void dig_mine2(int,int);void dig_mine3(int,int);void gameover();void disp_number(int,int);void all_number();void gamewin();void make_flag(int,int);void draw_redflag(int,int);void draw_questionmark(int,int);void disp_rest_mine();void disp_time(time_t);void judge_error_flag();void disp_all_redflags();void draw_cursor(int,int);main(){init_system();set_mines();draw_mine_region();clean_mine();getch();close_system();}void disp_time(time_t t){int x,y;char str[4];if(t>999)return;x=m_x0+m_col*(WIDTH+1)+1-40;y=m_y0-30;draw_box(x,y,x+30,y+20,AO,LIGHTGRAY);settextstyle(DEFAULT_FONT,HORIZ_DIR,1);sprintf(str,"%3ld",t);setcolor(RED);outtextxy(x+3,y+6,str);}void disp_rest_mine(){int x,y;char str[4];x=m_x0+10;y=m_y0-30;draw_box(x,y,x+30,y+20,AO,LIGHTGRAY);settextstyle(DEFAULT_FONT,HORIZ_DIR,1);sprintf(str,"%3d",m_countdown);x=x+(WIDTH-textwidth(str))/2;y=y+3+(WIDTH-textheight(str))/2;setcolor(RED);outtextxy(x,y,str);}void draw_questionmark(int i,int j){int x,y;char str[]="?";m_countdown++;disp_rest_mine();shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,TU,LIGHTGRAY);setcolor(BLACK);settextstyle(DEFAULT_FONT,HORIZ_DIR,1);x=x+(WIDTH-textwidth(str))/2;y=y+(WIDTH-textheight(str))/2;outtextxy(x,y,str);}void draw_redflag(int i,int j){int x,y;m_countdown--;disp_rest_mine();shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,TU,LIGHTGRAY);setcolor(RED);line(x+WIDTH/4,y+WIDTH/4,x+3*WIDTH/4,y+WIDTH/2+1);line(x+WIDTH/4,y+WIDTH/2+1,x+3*WIDTH/4,y+WIDTH/2+1);setfillstyle(SOLID_FILL,RED);/* floodfill(x+WIDTH/2,y+WIDTH/2,RED); */setcolor(BLACK);line(x+WIDTH/4,y+WIDTH/4,x+WIDTH/4,y+3*WIDTH/4+1);setlinestyle(SOLID_LINE,0,3);line(x+WIDTH/4-2,y+3*WIDTH/4+1,x+WIDTH/4+2,y+3*WIDTH/4+1);setlinestyle(SOLID_LINE,0,1);}void make_flag(int i,int j){int k,x,y;k=shift_k_by_ij(i,j);shift_xy_by_ij(i,j,&x,&y);if(m_flag[k]==0){draw_redflag(i,j);draw_cursor(i,j);m_flag[k]=-1;}else if(m_flag[k]==-1){draw_questionmark(i,j);m_flag[k]=-2;draw_cursor(i,j);}else if(m_flag[k]==-2){draw_box(x,y,x+WIDTH,y+WIDTH,TU,LIGHTGRAY);m_flag[k]=0;draw_cursor(i,j);}}void count_around_mines(){int i,j,k,m,n,x,y;for(k=0;k<m_row*m_col;k++){shift_ij_by_k(k,&i,&j);for(m=0;m<8;m++){x=i+a[m];y=j+b[m];if(x>=1&&x<=m_row&&y>=1&&y<=m_col&&m_mine[k]!=MINE){n=shift_k_by_ij(x,y);if(m_mine[n]==MINE)m_mine[k]++;}}}}void gamewin(){int x,y;char str[]="WIN ! ! !";setcolor(RED);settextstyle(DEFAULT_FONT,HORIZ_DIR,4);x=m_x0+(m_col*(WIDTH+1)-1-textwidth(str))/2;y=m_y0+(m_row*(WIDTH+1)-1-textheight(str))/2;outtextxy(x,y,str);}void gameover(){int x,y;char str[]="GAME OVER !";setcolor(RED);settextstyle(DEFAULT_FONT,HORIZ_DIR,4);x=m_x0+(m_col*(WIDTH+1)-1-textwidth(str))/2;y=m_y0+(m_row*(WIDTH+1)-1-textheight(str))/2;outtextxy(x,y,str);}void dig_mine3(int i,int j){int k,x,y,m,c,d;k=shift_k_by_ij(i,j);if(m_mine[k]<0||m_mine[k]>8||m_flag[k]==-1)return;else if(m_mine[k]>0&&m_mine[k]<9){disp_number(i,j);m_count++;m_mine[k]+=20;return;}else if(m_mine[k]==0){shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,AO,LIGHTGRAY);m_count++;m_mine[k]+=20;}for(m=0;m<8;m++){c=i+a[m];d=j+b[m];if(c>=1&&c<=m_row&&d>=1&&d<=m_col)dig_mine3(c,d);}}void all_number(){int i,j;for(i=1;i<m_row;i++)for(j=1;j<m_col;j++)disp_number(i,j);}void disp_number(int i,int j){int k,x,y;char str[2];k=shift_k_by_ij(i,j);if(m_mine[k]==MINE)return;shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,AO,LIGHTGRAY);if(m_mine[k]==0)return;settextstyle(DEFAULT_FONT,HORIZ_DIR,1);sprintf(str,"%d",m_mine[k]);x=x+(WIDTH-textwidth(str))/2;y=y+(WIDTH-textheight(str))/2;switch(m_mine[k]){case 1: setcolor(BLUE);break;case 2: setcolor(GREEN);break;case 3: setcolor(RED);break;case 4: setcolor(CYAN);break;case 5: setcolor(YELLOW);break;case 6: setcolor(BLACK);break;case 7: setcolor(BROWN);break;case 8: setcolor(MAGENTA);break;}outtextxy(x,y,str);}void dig_mine2(int i,int j){int k,x,y;k=shift_k_by_ij(i,j);if(m_flag[k]==-1)return;shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,AO,LIGHTGRAY);disp_number(i,j);m_count++;m_mine[k]+=20;}void judge_error_flag(){int k,m,i,j,x,y;m=m_row*m_col;for(k=0;k<m;k++){if(m_flag[k]==MINE&&m_mine[k]!=MINE){shift_ij_by_k(k,&i,&j);shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,AO,LIGHTGRAY);draw_mine(i,j);setcolor(RED);setlinestyle(SOLID_LINE,0,3);line(x+2,y+2,x+WIDTH-2,y+WIDTH-2);line(x+WIDTH-2,y+2,x+2,y+WIDTH-2);setlinestyle(SOLID_LINE,0,1);}}}int dig_mine1(int i,int j){int x,y;shift_xy_by_ij(i,j,&x,&y);disp_all_mines();judge_error_flag();draw_box(x,y,x+WIDTH,y+WIDTH,AO,RED);draw_mine(i,j);draw_smile(0);return 1;}int dig_mine(int i,int j){int x,y,k;shift_xy_by_ij(i,j,&x,&y);k=shift_k_by_ij(i,j);if(m_flag[k]==-1||m_mine[k]>10)return 0;if(m_mine[k]==MINE){dig_mine1(i,j);m_mine[k]+=20;return 1;}else if(m_mine[k]>0&&m_mine[k]<9){dig_mine2(i,j);m_mine[k]+=20;draw_cursor(i,j);}else if(m_mine[k]==0){dig_mine3(i,j);draw_cursor(i,j);}else disp_all_mines();return 0;}void disp_all_mines(){int k,i,j,x,y;for(k=0;k<m_row*m_col;k++){shift_ij_by_k(k,&i,&j);if(m_mine[k]==MINE){shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,AO,LIGHTGRAY);draw_mine(i,j);}}}void draw_mine(int i,int j){int x,y;shift_xy_by_ij(i,j,&x,&y);setcolor(BLACK);circle(x+WIDTH/2,y+WIDTH/2,WIDTH/4);setfillstyle(SOLID_FILL,BLACK);floodfill(x+WIDTH/2,y+WIDTH/2,BLACK);}void set_mines(){int i,k,mcount=0,mnum;mnum=m_col*m_row;for(i=0;i<mnum;i++){m_mine[i]=0;m_flag[i]=0;}randomize();while(mcount<m_num){k=random(mnum);if(m_mine[k]==0){m_mine[k]=MINE;mcount++;}}count_around_mines();}int shift_k_by_ij(int i,int j){return (i-1)*m_col+j-1;}void shift_ij_by_k(int k,int *i,int *j){*i=k/m_col+1;*j=k%m_col+1;}void shift_xy_by_ij(int i,int j,int *x,int *y) {*x=m_x0+(j-1)*(WIDTH+1);*y=m_y0+(i-1)*(WIDTH+1);}void disp_all_redflags(){int k,m,i,j,x,y;m=m_row*m_col;for(k=0;k<m;k++){shift_ij_by_k(k,&i,&j);if(m_mine[k]==MINE&&m_flag[k]!=MINE){if(m_flag[k]==-2){shift_xy_by_ij(i,j,&x,&y);draw_box(x,y,x+WIDTH,y+WIDTH,TU,LIGHTGRAY);}draw_redflag(i,j);}}}void draw_cursor(int i,int j){static int x,y;setcolor(LIGHTGRAY);setlinestyle(SOLID_LINE,0,1);setwritemode(1);x=m_x0+(j-1)*(WIDTH+1);y=m_y0+(i-1)*(WIDTH+1);rectangle(x+4,y+4,x+WIDTH-4,y+WIDTH-4);setwritemode(0);}void clean_mine(){int key=0,a,mark=0,pi=0,pj=0;time_t mt,ntl=0;int i=1,j=1,k,x,y;draw_cursor(i,j);mt=time(NULL);while(key!=ESC){if(mt!=time(NULL)&&mark!=0){mt=time(NULL);disp_time(++ntl);}key=bioskey(1);if(key==0)continue;key=bioskey(0);pi=i;pj=j;switch(key){case UP: if(i==1)i=m_row;elsei=i-1;break;case DOWN:if(i==m_row)i=1;elsei=i+1;break;case LEFT:if(j==1)j=m_col;elsej=j-1;break;case RIGHT:if(j==m_col)j=1;elsej=j+1;break;case ENTER:mark=1;a=dig_mine(i,j);if(a==1)gameover();else if(m_count==m_row*m_col-m_num){a=1;draw_smile(1);disp_all_redflags();gamewin();}break;case SPACE:k=shift_k_by_ij(i,j);if(m_mine[k]<10)make_flag(i,j);break;}if(a==1)break;if(pi==i&&pj==j&&(key==ENTER||key==SPACE))continue;else if(pi!=i||pj!=j)draw_cursor(pi,pj);draw_cursor(i,j);}}void draw_smile(int type){int m=m_x0+m_col*(WIDTH+1)/2,n=m_y0-25;setcolor(YELLOW);circle(m,n,15);setfillstyle(SOLID_FILL,YELLOW);floodfill(m,n,YELLOW);setcolor(BLACK);arc(m-6,n,60,120,6);arc(m-6,n,60,120,5);arc(m+6,n,60,120,6);arc(m+6,n,60,120,5);setcolor(RED);if(type==0){setlinestyle(SOLID_LINE,0,3);line(m-6,n+9,m+6,n+9);setlinestyle(SOLID_LINE,0,1);}if(type==1){arc(m,n,220,320,10);arc(m,n,220,320,9);}}void draw_title(){int mwidth,mhight;mwidth=m_col*(WIDTH+1)+1;mhight=m_row*(WIDTH+1)+1;draw_box(m_x0-4,m_y0-50,m_x0+mwidth+2,m_y0+mhight+2,TU,LIGHTGRAY);draw_box(m_x0-2,m_y0-50,m_x0+mwidth+2,m_y0-2,TU,LIGHTGRAY);disp_rest_mine();disp_time(0);}void draw_mine_region(){int i,j,x,y;draw_title();y=m_y0;for(i=0;i<m_row;i++){x=m_x0;for(j=0;j<m_col;j++){draw_box(x,y,x+WIDTH,y+WIDTH,TU,LIGHTGRAY);x+=WIDTH+1;}y+=WIDTH+1;}}void count_x0y0(){m_x0=(m_maxx-m_col*(WIDTH+1)+1)/2;m_y0=(m_maxy-m_row*(WIDTH+1)+1)/2;}void draw_box(int x1,int y1,int x2,int y2,int type,int color){setcolor(WHITE);rectangle(x1,y1,x2,y2);setfillstyle(SOLID_FILL,color);floodfill((x1+x2)/2,(y1+y2)/2,WHITE);if(type==TU)setcolor(WHITE);elsesetcolor(DARKGRAY);line(x1,y1,x2,y1);line(x1,y1,x1,y2);line(x1+1,y1+1,x2-1,y1+1);line(x1+1,y1+1,x1+1,y2-1);if(type==TU)setcolor(DARKGRAY);elsesetcolor(WHITE);line(x1,y2,x2,y2);line(x2,y1,x2,y2);line(x1-1,y2-1,x2-1,y2-1);line(x2-1,y1+1,x2-1,y2-1);}void init_system(){int gdriver=DETECT,gmode;initgraph(&gdriver,&gmode,"BGI\\");m_maxx=getmaxx();m_maxy=getmaxy();m_count=0;m_row=20;m_col=10;m_num=10;m_countdown=m_num;count_x0y0();m_mine=(int *)malloc(sizeof(int)*m_row*m_col);m_flag=(int *)malloc(sizeof(int)*m_row*m_col);first=1;}void close_system(){free(m_mine);free(m_flag);closegraph();}。

扫雷c语言代码

扫雷c语言代码

扫雷c语言代码该程序是一个经典的扫雷游戏,使用C语言编写。

该游戏的规则是将地图上的所有地雷挖出来而不触发任何地雷。

以下是该程序的代码:#include <stdio.h>#include <stdlib.h>#include <time.h>// 定义常量#define MAX_ROW 9#define MAX_COL 9#define MINES 10char mineField[MAX_ROW][MAX_COL]; // 地图char gameField[MAX_ROW][MAX_COL]; // 游戏中的视图// 初始化地图void initMineField() {int i, j, count;srand(time(NULL));// 随机放置地雷for (i=0; i<MAX_ROW; i++) {for (j=0; j<MAX_COL; j++) {mineField[i][j] = '0';}}count = 0;while (count < MINES) {i = rand() % MAX_ROW;j = rand() % MAX_COL;if (mineField[i][j] == '0') {mineField[i][j] = '*';count++;}}// 计算周围的地雷数for (i=0; i<MAX_ROW; i++) {for (j=0; j<MAX_COL; j++) {if (mineField[i][j] == '*') {continue;}if (i > 0 && j > 0 && mineField[i-1][j-1] == '*') {mineField[i][j]++;}if (i > 0 && mineField[i-1][j] == '*') {mineField[i][j]++;}if (i > 0 && j < MAX_COL-1 && mineField[i-1][j+1] == '*') {mineField[i][j]++;}if (j > 0 && mineField[i][j-1] == '*') {mineField[i][j]++;}if (j < MAX_COL-1 && mineField[i][j+1] == '*') { mineField[i][j]++;}if (i < MAX_ROW-1 && j > 0 && mineField[i+1][j-1] == '*') {mineField[i][j]++;}if (i < MAX_ROW-1 && mineField[i+1][j] == '*') { mineField[i][j]++;}if (i < MAX_ROW-1 && j < MAX_COL-1 &&mineField[i+1][j+1] == '*') {mineField[i][j]++;}}}}// 显示游戏视图void displayGameField() {int i, j;// 清屏system("cls");printf(" ");for (j=0; j<MAX_COL; j++) {printf(" %d", j+1);}printf(" \n");printf(" +");for (j=0; j<MAX_COL; j++) {printf("--");}printf("-+\n");for (i=0; i<MAX_ROW; i++) {printf("%c|", i+'A');for (j=0; j<MAX_COL; j++) {printf(" %c", gameField[i][j]);}printf(" |\n");}printf(" +");for (j=0; j<MAX_COL; j++) {printf("--");}printf("-+\n");}// 打开格子void open(int row, int col) {if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) {// 超出范围return;}if (gameField[row][col] != '-') {// 已经打开return;}gameField[row][col] = mineField[row][col];if (mineField[row][col] == '*') {// 触雷displayGameField();printf("Game over!\n");exit(0);}if (mineField[row][col] != '0') {// 周围有地雷return;}// 递归打开周围的格子open(row-1, col-1);open(row-1, col);open(row-1, col+1);open(row, col-1);open(row, col+1);open(row+1, col-1);open(row+1, col);open(row+1, col+1);}// 主函数int main() {int i, j, row, col, remain = MAX_ROW * MAX_COL - MINES; char command;initMineField();for (i=0; i<MAX_ROW; i++) {for (j=0; j<MAX_COL; j++) {gameField[i][j] = '-';}}displayGameField();while (remain > 0) {printf("Please enter your command (open/o, flag/f, unflag/u):");scanf("%c %d %d", &command, &row, &col);getchar(); // 读取回车符row--;col--;switch (command) {case 'o':open(row, col);remain--;break;case 'f':gameField[row][col] = 'F'; break;case 'u':gameField[row][col] = '-'; break;}displayGameField();}printf("You win!\n");return 0;}。

c语言编写扫雷代码

c语言编写扫雷代码

c语言编写扫雷代码示例编写扫雷游戏的代码涉及到图形界面、事件处理等,因此需要使用相应的库来简化这些操作。

下面是一个使用C语言和Simple DirectMedia Layer (SDL)库编写的简单扫雷游戏的代码示例。

请注意,这只是一个基本的示例,实际的扫雷游戏可能需要更多功能和复杂性。

首先,确保你已经安装了SDL库。

接下来,你可以使用以下代码作为一个简单的扫雷游戏的起点。

```c#include <SDL.h>#include <stdio.h>#include <stdlib.h>#include <time.h>// 游戏常量#define SCREEN_WIDTH 640#define SCREEN_HEIGHT 480#define CELL_SIZE 20#define ROWS 15#define COLS 20#define MINES 40// 游戏状态typedef struct {int revealed; // 是否被揭示int mine; // 是否是地雷int adjacent; // 相邻地雷数量} Cell;// 游戏数据Cell board[ROWS][COLS];// SDL 相关变量SDL_Window* window = NULL;SDL_Renderer* renderer = NULL;// 初始化游戏板void initializeBoard() {// 初始化每个单元格for (int i = 0; i < ROWS; ++i) {for (int j = 0; j < COLS; ++j) {board[i][j].revealed = 0;board[i][j].mine = 0;board[i][j].adjacent = 0;}}// 随机生成地雷位置srand(time(NULL));for (int k = 0; k < MINES; ++k) {int i = rand() % ROWS;int j = rand() % COLS;if (!board[i][j].mine) {board[i][j].mine = 1;// 增加相邻地雷数量for (int ni = i - 1; ni <= i + 1; ++ni) {for (int nj = j - 1; nj <= j + 1; ++nj) {if (ni >= 0 && ni < ROWS && nj >= 0 && nj < COLS && !(ni == i && nj == j)) {board[ni][nj].adjacent++;}}}} else {// 如果已经有地雷,重新生成k--;}}}// 渲染游戏板void renderBoard() {// 清空屏幕SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);SDL_RenderClear(renderer);// 绘制每个单元格for (int i = 0; i < ROWS; ++i) {for (int j = 0; j < COLS; ++j) {if (board[i][j].revealed) {// 已揭示的单元格if (board[i][j].mine) {SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // 地雷} else {SDL_SetRenderDrawColor(renderer, 192, 192, 192, 255); // 其他}} else {// 未揭示的单元格SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);}// 绘制单元格SDL_Rect cellRect = {j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE};SDL_RenderFillRect(renderer, &cellRect);// 绘制地雷数量(已揭示的单元格)if (board[i][j].revealed && !board[i][j].mine && board[i][j].adjacent > 0) {char text[2];snprintf(text, sizeof(text), "%d", board[i][j].adjacent);SDL_Color textColor = {0, 0, 0, 255};SDL_Surface* surface = TTF_RenderText_Solid(font, text, textColor);SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);SDL_Rect textRect = {j * CELL_SIZE + CELL_SIZE / 3, i * CELL_SIZE + CELL_SIZE / 3, CELL_SIZE / 2, CELL_SIZE / 2};SDL_RenderCopy(renderer, texture, NULL, &textRect);SDL_DestroyTexture(texture);SDL_FreeSurface(surface);}}}// 刷新屏幕SDL_RenderPresent(renderer);}// 处理鼠标点击事件void handleMouseClick(int x, int y) {int i = y / CELL_SIZE;int j = x / CELL_SIZE;if (!board[i][j].revealed) {board[i][j].revealed = 1;if (board[i][j].mine) {// 点击到地雷,游戏结束printf("Game Over!\n");SDL_Quit();exit(1);} else {// 递归揭示相邻单元格if (board[i][j].adjacent == 0) {for (int ni = i - 1; ni <= i + 1; ++ni) {for (int nj = j - 1; nj <= j + 1; ++nj) {if (ni >= 0 && ni < ROWS && nj >= 0 && nj < COLS && !(ni == i && nj == j)) {handleMouseClick(nj * CELL_SIZE, ni * CELL_SIZE);}}}}}}}int main() {// 初始化SDLSDL_Init(SDL_INIT_VIDEO);// 创建窗口和渲染器window = SDL_CreateWindow("Minesweeper", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);// 初始化游戏板initializeBoard();// 游戏循环int quit = 0;SDL_Event e;while (!quit) {// 处理事件while (SDL_PollEvent(&e) != 0) {if (e.type == SDL_QUIT) {quit = 1;} else if (e.type == SDL_MOUSEBUTTONDOWN) {if (e.button.button == SDL_BUTTON_LEFT) {handleMouseClick(e.button.x, e.button.y);}}}//渲染游戏板renderBoard();}// 清理资源SDL_DestroyWindow(window);SDL_DestroyRenderer(renderer);SDL_Quit();return 0;}```这是一个简单的扫雷游戏的C语言代码,使用SDL库来创建窗口、处理事件和渲染图形。

c语言扫雷程序代码

c语言扫雷程序代码

c语言扫雷程序代码以下是一份基于C语言编写的扫雷程序代码,供参考:#include <stdio.h>#include <stdlib.h>#include <time.h>#define ROW 10 // 扫雷区域行数#define COL 10 // 扫雷区域列数#define MINE_NUM 10 // 地雷数量int mine_map[ROW][COL] = {0}; // 扫雷地图数组 int show_map[ROW][COL] = {0}; // 显示地图数组 // 随机生成地雷void create_mine() {int i, j, k;for (k = 0; k < MINE_NUM; k++) {i = rand() % ROW;j = rand() % COL;if (mine_map[i][j] == 9) { // 该位置已有地雷 k--;continue;}mine_map[i][j] = 9; // 设定该位置为地雷 }}// 统计周围的地雷数量int count_mine(int x, int y) {int count = 0, i, j;for (i = x - 1; i <= x + 1; i++) {for (j = y - 1; j <= y + 1; j++) {if (i < 0 || i >= ROW || j < 0 || j >= COL) { // 超出地图边界continue;}if (mine_map[i][j] == 9) { // 周围有地雷count++;}}}return count;}// 初始化显示地图void init_show_map() {int i, j;for (i = 0; i < ROW; i++) {for (j = 0; j < COL; j++) {show_map[i][j] = 10; // 10代表未翻开 }}}// 显示地图void show() {int i, j;printf(' ');for (i = 0; i < COL; i++) {printf('%d ', i);}printf('');for (i = 0; i < ROW; i++) {printf('%d ', i);for (j = 0; j < COL; j++) {if (show_map[i][j] == 10) {printf('* ');} else if (show_map[i][j] == 9) {printf('P ');} else if (show_map[i][j] == 0) {printf(' ');} else {printf('%d ', show_map[i][j]);}}printf('');}}// 翻开指定位置void open(int x, int y) {if (mine_map[x][y] == 9) { // 踩到地雷 printf('Game Over!');show_map[x][y] = 9;show();exit(0);}if (show_map[x][y] == 10) { // 未翻开int count = count_mine(x, y);show_map[x][y] = count;if (count == 0) { // 周围没有地雷 int i, j;for (i = x - 1; i <= x + 1; i++) {for (j = y - 1; j <= y + 1; j++) {if (i < 0 || i >= ROW || j < 0 || j >= COL) { // 超出地图边界continue;}open(i, j);}}}}}int main() {int i, j, x, y;srand(time(NULL));create_mine();init_show_map();show();while (1) {printf('Please input x and y: ');scanf('%d%d', &x, &y);open(x, y);show(); }return 0; }。

c语言扫雷代码

c语言扫雷代码

c语言扫雷代码以下是一份简单的C语言扫雷代码,用于实现经典扫雷游戏的基本功能:#include <stdio.h>#include <stdlib.h>#include <time.h>#define ROWS 10#define COLS 10#define MINES 10int main(void){int field[ROWS][COLS] = {0}; // 初始化游戏区域int mines[MINES][2] = {0}; // 初始化地雷坐标数组int i, j, k, x, y, count, gameover = 0;srand(time(NULL)); // 初始化随机数生成器// 随机布置地雷for (k = 0; k < MINES; k++) {do {x = rand() % ROWS;y = rand() % COLS;} while (field[x][y] == -1);mines[k][0] = x;mines[k][1] = y;field[x][y] = -1;}// 计算每个非地雷格子周围地雷数for (i = 0; i < ROWS; i++) {for (j = 0; j < COLS; j++) {if (field[i][j] != -1) {count = 0;if (i > 0 && j > 0 && field[i-1][j-1] == -1) count++;if (i > 0 && field[i-1][j] == -1) count++;if (i > 0 && j < COLS-1 && field[i-1][j+1] == -1) count++; if (j > 0 && field[i][j-1] == -1) count++;if (j < COLS-1 && field[i][j+1] == -1) count++;if (i < ROWS-1 && j > 0 && field[i+1][j-1] == -1) count++; if (i < ROWS-1 && field[i+1][j] == -1) count++;if (i < ROWS-1 && j < COLS-1 && field[i+1][j+1] == -1) count++;field[i][j] = count;}}}// 游戏开始,循环猜测地雷位置并扫开周围格子while (!gameover) {// 打印游戏区域printf(' ');for (j = 0; j < COLS; j++) {printf('%d ', j);}printf('');for (i = 0; i < ROWS; i++) {printf('%d ', i);for (j = 0; j < COLS; j++) {if (field[i][j] < 0) {printf('* ');} else {printf('%d ', field[i][j]);}}printf('');}// 用户输入坐标猜测地雷位置printf('Enter row and column: ');scanf('%d %d', &x, &y);// 判断用户猜测是否正确for (k = 0; k < MINES; k++) {if (x == mines[k][0] && y == mines[k][1]) {printf('BOOM! You lose.');gameover = 1;break;}}// 扫开周围格子if (!gameover) {if (field[x][y] == 0) {if (x > 0 && field[x-1][y] >= 0) field[x-1][y] = -2;if (x > 0 && y > 0 && field[x-1][y-1] >= 0) field[x-1][y-1] = -2;if (x > 0 && y < COLS-1 && field[x-1][y+1] >= 0)field[x-1][y+1] = -2;if (y > 0 && field[x][y-1] >= 0) field[x][y-1] = -2;if (y < COLS-1 && field[x][y+1] >= 0) field[x][y+1] = -2; if (x < ROWS-1 && field[x+1][y] >= 0) field[x+1][y] = -2; if (x < ROWS-1 && y > 0 && field[x+1][y-1] >= 0)if (x < ROWS-1 && y < COLS-1 && field[x+1][y+1] >= 0) field[x+1][y+1] = -2;for (i = 0; i < ROWS; i++) {for (j = 0; j < COLS; j++) {if (field[i][j] == -2) {field[i][j] = -1;count = 0;if (i > 0 && j > 0 && field[i-1][j-1] == -1) count++;if (i > 0 && field[i-1][j] == -1) count++;if (i > 0 && j < COLS-1 && field[i-1][j+1] == -1) count++; if (j > 0 && field[i][j-1] == -1) count++;if (j < COLS-1 && field[i][j+1] == -1) count++;if (i < ROWS-1 && j > 0 && field[i+1][j-1] == -1) count++; if (i < ROWS-1 && field[i+1][j] == -1) count++;if (i < ROWS-1 && j < COLS-1 && field[i+1][j+1] == -1) count++;field[i][j] = count;}}}} else {count = 0;if (x > 0 && j > 0 && field[x-1][y-1] == -1) count++;if (x > 0 && field[x-1][y] == -1) count++;if (x > 0 && y < COLS-1 && field[x-1][y+1] == -1) count++; if (y > 0 && field[x][y-1] == -1) count++;if (y < COLS-1 && field[x][y+1] == -1) count++;if (x < ROWS-1 && y > 0 && field[x+1][y-1] == -1) count++; if (x < ROWS-1 && field[x+1][y] == -1) count++;if (x < ROWS-1 && y < COLS-1 && field[x+1][y+1] == -1) count++;field[x][y] = count;}}// 判断游戏是否胜利count = 0;for (i = 0; i < ROWS; i++) {for (j = 0; j < COLS; j++) {if (field[i][j] >= 0) count++;}}if (count == ROWS * COLS - MINES) {printf('Congratulations! You win. ');gameover = 1;}}return 0;}。

c语言扫雷程序代码

c语言扫雷程序代码

c语言扫雷程序代码很高兴能够回答这个问题,关于c语言扫雷程序代码,以下是一个简单的实现示例。

#include<stdio.h>#include<stdlib.h>#include<time.h>#define ROW 9#define COL 9#define COUNT 10void menu()//游戏主菜单{printf("*****************\n");printf("*****扫雷游戏*****\n");printf("*****************\n");printf("*** 1.开始游戏 ***\n");printf("*** 0.退出游戏 ***\n");printf("*****************\n");}void init_board(char board[ROW + 2][COL + 2], int row, int col, char ch)//初始化扫雷棋盘{int i = 0;int j = 0;for (i = 1; i <= row; i++){for (j = 1; j <= col; j++){board[i][j] = ch;}}}void show_board_with_numbers(char mine_board[ROW +2][COL + 2], char show_board[ROW + 2][COL + 2], int row, int col)//棋盘及数字{printf(" ");for (int i = 1; i <= row; i++){printf("%d ", i);}printf("\n");printf(" ");for (int i = 1; i <= row; i++){printf("- ");}printf("\n");for (int i = 1; i <= row; i++){printf("%2d|", i);for (int j = 1; j <= col; j++){if (show_board[i][j] == 'o'){printf("%c ", mine_board[i][j]);}else{printf("%c ", show_board[i][j]); }}putchar('\n');}}int get_all_count(char mine_board[ROW + 1][COL + 1], int x, int y)//计算当前位置周围地雷数量{int count = 0;for (int i = x - 1; i <= x + 1; i++){for (int j = y - 1; j <= y + 1; j++){if (i >= 1 && i <= ROW && j >= 1 && j <= COL && mine_board[i][j] == '*'){count++;}}}return count;}void find_mine(int x, int y, char mine_board[ROW + 2][COL + 2],char show_board[ROW + 2][COL + 2])//查找地雷并标记{if (mine_board[x][y] == '*'){return;}else{int count = get_all_count(mine_board, x, y);show_board[x][y] = count + '0';if (count == 0){for (int i = x - 1; i <= x + 1; i++){for (int j = y - 1; j <= y + 1; j++){if (i >= 1 && i <= ROW && j >= 1 && j <= COL && show_board[i][j] == 'o'){find_mine(i, j, mine_board, show_board);}}}}}}void set_mine(char mine_board[ROW + 2][COL + 2], int count)//随机放置地雷{int x = 0;int y = 0;int i = 0;int j = 0;for (i = 0; i <= ROW; i++){mine_board[i][0] = '1';mine_board[i][COL + 1] = '1';}for (j = 0; j <= COL; j++) {mine_board[0][j] = '1';mine_board[ROW + 1][j] = '1';}srand((unsigned)time(NULL));for (i = 0; i < count; i++){x = rand() % ROW + 1;y = rand() % COL + 1;if (mine_board[x][y] == '*'){i--;}else{mine_board[x][y] = '*';}}}void game()//游戏主程序{char mine_board[ROW + 2][COL + 2]; char show_board[ROW + 2][COL + 2]; int x = 0;int y = 0;int win = 0;init_board(mine_board, ROW, COL, '0'); init_board(show_board, ROW, COL, 'o');set_mine(mine_board, COUNT);show_board_with_numbers(mine_board, show_board, ROW, COL);while (1){printf("请输入你要排查的方格坐标(x,y):");scanf("%d,%d", &x, &y);if (x >= 1 && x <= ROW && y >= 1 && y <= COL){if (mine_board[x][y] == '*'){printf("不好意思,你踩到地雷了!\n");break;}else{find_mine(x, y, mine_board, show_board);printf("当前棋盘状态如下:\n");show_board_with_numbers(mine_board, show_board, ROW, COL);win = win + ROW * COL - COUNT;for (int i = 1; i <= ROW; i++){for (int j = 1; j <= COL; j++){if (show_board[i][j] != 'o'){win--;}}}if (win == 0){printf("你赢了!\n");break;}}}else{printf("输入坐标有误,请重新输入!\n"); }}}int main()//主程序{int choice = 0;do{menu();printf("请输入你的选择:");scanf("%d", &choice);switch (choice){case 1:game();break;case 0:printf("退出游戏!\n");break;default:printf("输入有误,请重新输入!\n"); break;}} while (choice);return 0;}以上就是一个简单的c语言扫雷程序的实现,希望对你有所帮助。

c语言扫雷源代码

c语言扫雷源代码

c语言扫雷源代码扫雷游戏是一种非常经典的单人益智游戏,玩家需要根据数字提示找出地雷的位置。

下面给出一个简单的C语言扫雷游戏的源代码示例:```c#include <stdio.h>#include <stdlib.h>#include <time.h>#define SIZE 10 // 扫雷游戏的大小#define MINE_COUNT 10 // 地雷的数量typedef struct {int x;int y;} Position;typedef struct {int isMine; // 是否是地雷int isFlagged; // 是否被标记为地雷int isRevealed; // 是否已被翻开int adjacentMines; // 相邻地雷的数量} Cell;Cell board[SIZE][SIZE];void initializeBoard() {int i, j;// 初始化游戏面板for (i = 0; i < SIZE; i++) {for (j = 0; j < SIZE; j++) {board[i][j].isMine = 0;board[i][j].isFlagged = 0;board[i][j].isRevealed = 0;board[i][j].adjacentMines = 0;}}}void generateMines() {int i, j;int count = 0;srand(time(NULL)); // 以当前时间作为随机数种子 while (count < MINE_COUNT) {i = rand() % SIZE;j = rand() % SIZE;// 如果该格子已经是地雷,则重新生成随机位置 if (!board[i][j].isMine) {board[i][j].isMine = 1;count++;}}}void calculateAdjacentMines() {int i, j, k, l;int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};for (i = 0; i < SIZE; i++) {for (j = 0; j < SIZE; j++) {if (!board[i][j].isMine) {for (k = 0; k < 8; k++) {int ni = i + dx[k];int nj = j + dy[k];if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj].isMine) {board[i][j].adjacentMines++;}}}}}}void displayBoard() {int i, j;printf(" ");for (i = 0; i < SIZE; i++) {printf("%d ", i);}printf("\n");for (i = 0; i < SIZE; i++) {printf("%d ", i);for (j = 0; j < SIZE; j++) {if (board[i][j].isRevealed) {if (board[i][j].isMine) {printf("M ");} else {printf("%d ", board[i][j].adjacentMines);}} else if (board[i][j].isFlagged) {printf("F ");} else {printf(". ");}}printf("\n");}}void revealAdjacentEmptyCells(Position pos) {int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};int i;for (i = 0; i < 8; i++) {int ni = pos.x + dx[i];int nj = pos.y + dy[i];if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && !board[ni][nj].isRevealed && !board[ni][nj].isMine) { board[ni][nj].isRevealed = 1;if (board[ni][nj].adjacentMines == 0) {Position newPos;newPos.x = ni;newPos.y = nj;revealAdjacentEmptyCells(newPos);}}}}int main() {Position pos;char cmd;int isSuccess = 0;initializeBoard();generateMines();calculateAdjacentMines();displayBoard();while (!isSuccess) {printf("Enter command (R: reveal, F: flag): ");fflush(stdout);scanf(" %c", &cmd);printf("Enter position (x, y): ");fflush(stdout);scanf("%d %d", &(pos.x), &(pos.y));if (cmd == 'R') {board[pos.x][pos.y].isRevealed = 1;if (board[pos.x][pos.y].isMine) {printf("Game Over!\n");isSuccess = 1;} else if (board[pos.x][pos.y].adjacentMines == 0) {revealAdjacentEmptyCells(pos);}} else if (cmd == 'F') {board[pos.x][pos.y].isFlagged = 1;}displayBoard();}return 0;}```上述代码实现了一个简单的C语言扫雷游戏。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*5.3.4源程序*/ #include<>um=random(8);/*如果随机数的结果是1表示这个格子有地雷*/if(Mine[i][j].num==1)mineNUM++;/*现有雷数加1*/elseMine[i][j].num=2;Mine[i][j].flag=0;/*表示没红旗标志*/}sprintf(randmineNUM,"%d",mineNUM);/*显示这次总共有多少雷数*/setcolor(1);settextstyle(0,0,2);outtextxy(210,70,randmineNUM);mineNUM=100-mineNUM;/*变量取空白格数量*/MouseOn();}voidGameOver(void)/*游戏结束画面*/{inti,j;setcolor(0);for(i=0;i<10;i++)for(j=0;j<10;j++)if(Mine[i][j].num==1)/*显示所有的地雷*/{DrawEmpty(i,j,0,RED);setfillstyle(SOLID_FILL,BLACK);fillellipse(200+j*20,100+i*20,7,7);}}voidGameWin(void)/*显示胜利*/{setcolor(11);settextstyle(0,0,2);outtextxy(230,30,"YOUWIN!");}intMineStatistics(inti,intj)/*统计每个格子周围的雷数*/{ intnNUM=0;if(i==0&&j==0)/*左上角格子的统计*/{if(Mine[0][1].num==1)nNUM++;if(Mine[1][0].num==1)nNUM++;if(Mine[1][1].num==1)nNUM++;}elseif(i==0&&j==9)/*右上角格子的统计*/{if(Mine[0][8].num==1)nNUM++;if(Mine[1][9].num==1)nNUM++;if(Mine[1][8].num==1)nNUM++;}elseif(i==9&&j==0)/*左下角格子的统计*/{if(Mine[8][0].num==1)nNUM++;if(Mine[9][1].num==1)nNUM++;if(Mine[8][1].num==1)nNUM++;}elseif(i==9&&j==9)/*右下角格子的统计*/{if(Mine[9][8].num==1)nNUM++;if(Mine[8][9].num==1)nNUM++;if(Mine[8][8].num==1)nNUM++;}elseif(j==0)/*左边第一列格子的统计*/{ if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++; if(Mine[i+1][j+1].num==1)nNUM++;}elseif(j==9)/*右边第一列格子的统计*/{if(Mine[i][j-1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;}elseif(i==0)/*第一行格子的统计*/{if(Mine[i+1][j].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;}elseif(i==9)/*最后一行格子的统计*/{ if(Mine[i-1][j].num==1)nNUM++; if(Mine[i][j-1].num==1)nNUM++; if(Mine[i][j+1].num==1)nNUM++; if(Mine[i-1][j-1].num==1)nNUM++; if(Mine[i-1][j+1].num==1)nNUM++;}else/*普通格子的统计*/{if(Mine[i-1][j].num==1)nNUM++;if(Mine[i-1][j+1].num==1)nNUM++;if(Mine[i][j+1].num==1)nNUM++;if(Mine[i+1][j+1].num==1)nNUM++;if(Mine[i+1][j].num==1)nNUM++;if(Mine[i+1][j-1].num==1)nNUM++;if(Mine[i][j-1].num==1)nNUM++;if(Mine[i-1][j-1].num==1)nNUM++;}return(nNUM);/*把格子周围一共有多少雷数的统计结果返回*/}intShowWhite(inti,intj)/*显示无雷区的空白部分*/{if(Mine[i][j].flag==1||Mine[i][j].num==0)/*如果有红旗或该格处理过就不对该格进行任何判断*/return;mineNUM--;/*显示过数字或者空格的格子就表示多处理了一个格子,当所有格子都处理过了表示*/if(Mine[i][j].roundnum==0&&Mine[i][j].num!=1)/*显示空格*/{DrawEmpty(i,j,1,7);Mine[i][j].num=0;}elseif(Mine[i][j].roundnum!=0)/*输出雷数*/{DrawEmpty(i,j,0,8);sprintf(randmineNUM,"%d",Mine[i][j].roundnum);setcolor(RED);outtextxy(195+j*20,95+i*20,randmineNUM);Mine[i][j].num=0;/*已经输出雷数的格子用0表示已经用过这个格子*/ return}/*8个方向递归显示所有的空白格子*/if(i!=0&&Mine[i-1][j].num!=1)ShowWhite(i-1,j);if(i!=0&&j!=9&&Mine[i-1][j+1].num!=1)ShowWhite(i-1,j+1);if(j!=9&&Mine[i][j+1].num!=1)ShowWhite(i,j+1);if(j!=9&&i!=9&&Mine[i+1][j+1].num!=1)ShowWhite(i+1,j+1);if(i!=9&&Mine[i+1][j].num!=1)ShowWhite(i+1,j);if(i!=9&&j!=0&&Mine[i+1][j-1].num!=1)ShowWhite(i+1,j-1);if(j!=0&&Mine[i][j-1].num!=1)ShowWhite(i,j-1);if(i!=0&&j!=0&&Mine[i-1][j-1].num!=1)ShowWhite(i-1,j-1);}voidGamePlay(void)/*游戏过程*/{inti,j,Num;/*Num用来接收统计函数返回一个格子周围有多少地雷*/ for(i=0;i<10;i++)for(j=0;j<10;j++)Mine[i][j].roundnum=MineStatistics(i,j);/*统计每个格子周围有多少地雷*/while(!kbhit()){if(LeftPress())/*鼠标左键盘按下*/{MouseGetXY();if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)/*重新来*/{MouseOff();gameAGAIN=1;break;}if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置格子范围内*/ j=(MouseX-190)/20;/*x坐标*/i=(MouseY-90)/20;/*y坐标*/if(Mine[i][j].flag==1)/*如果格子有红旗则左键无效*/continue;if(Mine[i][j].num!=0)/*如果格子没有处理过*/{if(Mine[i][j].num==1)/*鼠标按下的格子是地雷*/{MouseOff();GameOver();/*游戏失败*/break;}else/*鼠标按下的格子不是地雷*/{MouseOff();Num=MineStatistics(i,j);if(Num==0)/*周围没地雷就用递归算法来显示空白格子*/ShowWhite(i,j);else/*按下格子周围有地雷*/{sprintf(randmineNUM,"%d",Num);/*输出当前格子周围的雷数*/setcolor(RED);outtextxy(195+j*20,95+i*20,randmineNUM);mineNUM--;}MouseOn();Mine[i][j].num=0;/*点过的格子周围雷数的数字变为0表示这个格子已经用过*/ if(mineNUM<1)/*胜利了*/{GameWin();break;}}}}}if(RightPress())/*鼠标右键键盘按下*/{MouseGetXY();if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置在格范围内*/{ j=(MouseX-190)/20;/*x坐标*/i=(MouseY-90)/20;/*y坐标*/MouseOff();if(Mine[i][j].flag==0&&Mine[i][j].num!=0)/*本来没红旗现在显示红旗*/{ DrawRedflag(i,j);Mine[i][j].flag=1;}elseif(Mine[i][j].flag==1)/*有红旗标志再按右键就红旗消失*/{DrawEmpty(i,j,0,8);Mine[i][j].flag=0;}}MouseOn();sleep(1);}}}。

相关文档
最新文档