原创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语⾔实现扫雷游戏详细代码实例扫雷游戏思路:先制作⼀个菜单让玩家选择是玩游戏还是退出游戏,菜单做好了,接着我们开始制作扫雷的棋盘并初始化,初始化弄完了我们下⼀步开始埋雷,雷埋好了就开始扫雷。
⼤概思路就是这样具体实现看下⾯:菜单的实现代码:int main(){int input = 0;srand((unsigned int)time(NULL));do{printf("**************************\n");printf("*** 1. play 0. exit ***\n");printf("*** 2. clear ***\n");printf("**************************\n");printf("请选择:>");scanf("%d", &input);switch (input){case 1:game();//游戏实现break;case 2://清屏选项system("cls");break;case 0:printf("退出程序!\n");break;default:printf("输⼊错误,请重新输⼊!\n");Sleep(1000);system("cls");break;}} while (input);return 0;}这⾥我们⽤了Windows库函数清屏,如果屏幕上显⽰的东西太多了,我们可以选择2来清屏,还有⼀个睡眠函数,如果输出错误会短暂的提⽰你⼀秒,告诉你选择错误了。
效果展⽰图:制作好菜单那我们开始实现整个游戏的逻辑框架了,定义两个⼆维数组,⼀个⽤于显⽰,⼀个⽤于存放地雷。
如果这两个东西都只⽤⼀个⼆维数组的话后⾯的实现逻辑会⽐较⿇烦所以我选择使⽤两个⼆维数组。
C语言实现扫雷附完整代码
C语⾔实现扫雷附完整代码⽬录⼀、理清逻辑⼆、创建⽂件三、具体步骤1.打印菜单2.创建⼆维数组3.初始化⼆维数组并打印棋盘4.布置雷5.排查雷(内含判断胜负)四、完整代码五、待改进⼀、理清逻辑我们先来看⼀下实现扫雷的基本逻辑1.打印游戏菜单2.创建并初始化⼆维数组3.布置雷4.进⾏排雷⼆、创建⽂件我创建了三个⽂件,分别为test.c、game.h和game.ctest.c⽂件⽤于实现进⼊游戏、退出游戏、判断输赢、打印菜单等逻辑game.c⽤于编写游戏的主要实现⽅法game.h存放头⽂件和函数的声明三、具体步骤1.打印菜单void menu(){printf("**********************************\n");printf("*********** 1.start **********\n");printf("*********** 0. exit **********\n");printf("**********************************\n");}void test(){int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请输⼊:>");scanf("%d", &input);switch (input){case 1:printf("游戏开始\n");game();break;case 0:printf("游戏结束\n");break;default :printf("⾮法输⼊,请重新输⼊\n");break;}} while (input);}2.创建⼆维数组因为我们想实现⼀个9*9的扫雷,所以考虑到数组越界的问题,我们要创建⼀个11*11的⼆维数组,需要同时去创建两个⼆维数组,mine数组⽤来查看雷的位置和show数组来展⽰排雷过程。
C语言编写的扫雷游戏源代码
C语言编写的扫雷嬉戏源代码/* 源程序*/#include <graphics.h>#include <stdlib.h>#include <dos.h>#define LE 0xff01#define LEFTCLICK 0xff10#define LEFTDRAG 0xff19#define MOUSEMOVE 0xff08struct{int num;/*格子当前处于什么状态,1有雷,0已经显示过数字或者空白格子*/ int roundnum;/*统计格子四周有多少雷*/int flag;/*右键按下显示红旗的标记,0没有红旗标记,1有红旗标记*/}Mine[10][10];int gameAGAIN=0;/*是否重来的变量*/int gamePLAY=0;/*是否是第一次玩嬉戏的标记*/int mineNUM;/*统计处理过的格子数*/char randmineNUM[3];/*显示数字的字符串*/int Keystate;int MouseExist;int MouseButton;int MouseX;int MouseY;void Init(void);/*图形驱动*/void MouseOn(void);/*鼠标光标显示*/void MouseOff(void);/*鼠标光标隐藏*/void MouseSetXY(int,int);/*设置当前位置*/int Le(void);/*左键按下*/int RightPress(void);/*鼠标右键按下*/void MouseGetXY(void);/*得到当前位置*/void Control(void);/*嬉戏开场,重新,关闭*/void GameBegain(void);/*嬉戏开场画面*/void DrawSmile(void);/*画笑脸*/void DrawRedflag(int,int);/*显示红旗*/void DrawEmpty(int,int,int,int);/*两种空格子的显示*/void GameOver(void);/*嬉戏完毕*/void GameWin(void);/*显示成功*/int MineStatistics(int,int);/*统计每个格子四周的雷数*/int ShowWhite(int,int);/*显示无雷区的空白局部*/void GamePlay(void);/*嬉戏过程*/void Close(void);/*图形关闭*/void main(void)Init();Control();Close();}void Init(void)/*图形开场*/{int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");}void Close(void)/*图形关闭*/{closegraph();}void MouseOn(void)/*鼠标光标显示*/{_AX=0x01;geninterrupt(0x33);}void MouseOff(void)/*鼠标光标隐藏*/{_AX=0x02;geninterrupt(0x33);}void MouseSetXY(int x,int y)/*设置当前位置*/ {_CX=x;_DX=y;_AX=0x04;geninterrupt(0x33);}int Le(void)/*鼠标左键按下*/{_AX=0x03;geninterrupt(0x33);return(_BX&1);}int RightPress(void)/*鼠标右键按下*/{_AX=0x03;geninterrupt(0x33);return(_BX&2);}void MouseGetXY(void)/*得到当前位置*/_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;}void Control(void)/*嬉戏开场,重新,关闭*/{int gameFLAG=1;/*嬉戏失败后推断是否重新开场的标记*/while(1){if(gameFLAG)/*嬉戏失败后没推断出重新开场或者退出嬉戏的话就接着推断*/ {GameBegain(); /*嬉戏初始画面*/GamePlay();/*详细嬉戏*/if(gameAGAIN==1)/*嬉戏中重新开场*/{gameAGAIN=0;continue;}}MouseOn();gameFLAG=0;if(Le())/*推断是否重新开场*/{MouseGetXY();if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85){gameFLAG=1;continue;}}if(kbhit())/*推断是否按键退出*/break;}MouseOff();}void DrawSmile(void)/*画笑脸*/{setfillstyle(SOLID_FILL,YELLOW);fillellipse(290,75,10,10);setcolor(YELLOW);setfillstyle(SOLID_FILL,BLACK);/*眼睛*/fillellipse(285,75,2,2);setcolor(BLACK);/*嘴巴*/bar(287,80,293,81);}void DrawRedflag(int i,int j)/*显示红旗*/{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);}void DrawEmpty(int i,int j,int mode,int color)/*两种空格子的显示*/ {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);}void GameBegain(void)/*嬉戏开场画面*/{int i,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);/*假如随机数的结果是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();}void GameOver(void)/*嬉戏完毕画面*/{int i,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);}}void GameWin(void)/*显示成功*/{setcolor(11);settextstyle(0,0,2);outtextxy(230,30,"YOU WIN!");}int MineStatistics(int i,int j)/*统计每个格子四周的雷数*/{int nNUM=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)}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++;}else if(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++;else if(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++;}else if(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++;}else if(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++;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);/*把格子四周一共有多少雷数的统计结果返回*/}int ShowWhite(int i,int j)/*显示无雷区的空白局部*/{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);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);}void GamePlay(void)/*嬉戏过程*/{int i,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(Le())/*鼠标左键盘按下*/{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();/*嬉戏失败*/}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语言扫雷程序大作业代码
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语言扫雷程序代码
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复制代码#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.}。
用C语言完成:扫雷。
⽤C语⾔完成:扫雷。
上⼀篇我们介绍了三⼦棋游戏⼩程序,那这⼀篇我来介绍另⼀个简单的游戏⼩程序——扫雷。
相信⼤家都玩过windows下的扫雷游戏,对其的规则也⽐较了解,这⾥我们就不对规则做过多赘述了。
⾸先菜单和主函数的编写和上⼀篇三⼦棋中的内容⼀致,这⾥我们直接给出代码:void menu(){printf("********* Welcom to Mines *********\n");printf("***************************************\n");printf("* 1.play 0.exit *\n");printf("***************************************\n");}int main(){int choice;srand((unsigned int)time(NULL));do{menu();printf("Input your choice:");scanf("%d", &choice);switch (choice){case1:game();break;case0:break;default:printf(" Input error!Please try again.\n");break;}} while (choice);return0;}下⾯展⽰我的代码写出的菜单和主函数:⼀样,我们先定义⼀个game()函数,⽽后慢慢填充其中的内容。
和三⼦棋不同的是,这⾥需要创建两个数组,其中⼀个是雷区数组,另⼀个是需要输出在屏幕上的显⽰数组。
为了便于区分两个数组,雷区数组我⽤mines表⽰,显⽰数组我⽤show_area来表⽰。
但这⼀步我们先跳过,先来完成初始化数组的功能函数。
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版
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语言初学者的编程练习还是很不错的,那么本篇文章详细注释了扫雷代码中的各行代码的含义以及作用,对初学者来说还是很有帮助的。
一、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打印出来的是字符。
C语言游戏代码(里面揽括扫雷_俄罗斯方块_推箱子_五子棋_贪吃蛇)
五子棋#include <stdio.h>#include <bios.h>#include <ctype.h>#include <conio.h>#include <dos.h>#define CROSSRU 0xbf /*右上角点*/#define CROSSLU 0xda /*左上角点*/#define CROSSLD 0xc0 /*左下角点*/#define CROSSRD 0xd9 /*右下角点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*十字交叉点*//*定义棋盘左上角点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落子--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--方向键up*/#define PLAY2DOWN 0x5000/*下移--方向键down*/ #define PLAY2LEFT 0x4b00/*左移--方向键left*/#define PLAY2RIGHT 0x4d00/*右移--方向键right*/ #define PLAY2DO 0x1c0d/*落子--回车键Enter*//*若想在游戏中途退出, 可按Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有无棋子*//*若有棋子, 还应能指出是哪个玩家的棋子*/#define CHESSNULL 0 /*没有棋子*/#define CHESS1 'O'/*一号玩家的棋子*/#define CHESS2 'X'/*二号玩家的棋子*//*定义按键类别*/#define KEYEXIT 0/*退出键*/#define KEYFALLCHESS 1/*落子键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINV ALID 3/*无效键*//*定义符号常量: 真, 假--- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************/ /* 定义数据结构*//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};/**********************************************************/ /*自定义函数原型说明*/void Init(void);int GetKey(void);int CheckKey(int press);int ChangeOrder(void);int ChessGo(int Order,struct point Cursor);void DoError(void);void DoOK(void);void DoWin(int Order);void MoveCursor(int Order,int press);void DrawCross(int x,int y);void DrawMap(void);int JudgeWin(int Order,struct point Cursor);int JudgeWinLine(int Order,struct point Cursor,int direction);void ShowOrderMsg(int Order);void EndGame(void);/**********************************************************//**********************************************************/ /* 定义全局变量*/int gPlayOrder; /*指示当前行棋方*/struct point gCursor; /*光标在棋盘上的位置*/char gChessBoard[19][19];/*用于记录棋盘上各点的状态*//**********************************************************//**********************************************************/ /*主函数*/void main(){int press;int bOutWhile=FALSE;/*退出循环标志*/printf("Welcome ");Init();/*初始化图象,数据*/while(1){press=GetKey();/*获取用户的按键值*/switch(CheckKey(press))/*判断按键类别*/{/*是退出键*/case KEYEXIT:clrscr();/*清屏*/bOutWhile = TRUE;break;/*是落子键*/case KEYFALLCHESS:if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/DoError();/*落子错误*/else{DoOK();/*落子正确*//*如果当前行棋方赢棋*/if(JudgeWin(gPlayOrder,gCursor)==TRUE){DoWin(gPlayOrder);bOutWhile = TRUE;/*退出循环标志置为真*/}/*否则*/else/*交换行棋方*/ChangeOrder();ShowOrderMsg(gPlayOrder);}break;/*是光标移动键*/case KEYMOVECURSOR:MoveCursor(gPlayOrder,press);break;/*是无效键*/case KEYINV ALID:break;}if(bOutWhile==TRUE)break;}/*游戏结束*/EndGame();}/**********************************************************//*界面初始化,数据初始化*/void Init(void){int i,j;char *Msg[]={"Player1 key:"," UP----w"," DOWN--s"," LEFT--a"," RIGHT-d"," DO----space","","Player2 key:"," UP----up"," DOWN--down"," LEFT--left"," RIGHT-right"," DO----ENTER","","exit game:"," ESC",NULL,/* 先手方为1号玩家*/gPlayOrder = CHESS1;/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋子*/ for(i=0;i<19;i++)for(j=0;j<19;j++)gChessBoard[i][j]=CHESSNULL;/*光标初始位置*/gCursor.x=gCursor.y=0;/*画棋盘*/textmode(C40);DrawMap();/*显示操作键说明*/i=0;textcolor(BROWN);while(Msg[i]!=NULL){gotoxy(25,3+i);cputs(Msg[i]);i++;}/*显示当前行棋方*/ShowOrderMsg(gPlayOrder);/*光标移至棋盘的左上角点处*/gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);}/*画棋盘*/void DrawMap(void){int i,j;clrscr();for(i=0;i<19;i++)for(j=0;j<19;j++)DrawCross(i,j);}/*画棋盘上的交叉点*/void DrawCross(int x,int y){gotoxy(x+MAPXOFT,y+MAPYOFT); /*交叉点上是一号玩家的棋子*/if(gChessBoard[x][y]==CHESS1) {textcolor(LIGHTBLUE);putch(CHESS1);return;}/*交叉点上是二号玩家的棋子*/if(gChessBoard[x][y]==CHESS2) {textcolor(LIGHTBLUE);putch(CHESS2);return;}textcolor(GREEN);/*左上角交叉点*/if(x==0&&y==0){putch(CROSSLU);return;}/*左下角交叉点*/if(x==0&&y==18){putch(CROSSLD);return;}/*右上角交叉点*/if(x==18&&y==0){putch(CROSSRU);return;}/*右下角交叉点*/if(x==18&&y==18){putch(CROSSRD); return;}/*左边界交叉点*/if(x==0){putch(CROSSL); return;}/*右边界交叉点*/if(x==18){putch(CROSSR); return;}/*上边界交叉点*/if(y==0){putch(CROSSU); return;}/*下边界交叉点*/if(y==18){putch(CROSSD); return;}/*棋盘中间的交叉点*/ putch(CROSS);}/*交换行棋方*/int ChangeOrder(void) {if(gPlayOrder==CHESS1) gPlayOrder=CHESS2; elsegPlayOrder=CHESS1;return(gPlayOrder);}/*获取按键值*/int GetKey(void){char lowbyte;int press;while (bioskey(1) == 0);/*如果用户没有按键,空循环*/press=bioskey(0);lowbyte=press&0xff;press=press&0xff00 + toupper(lowbyte); return(press);}/*落子错误处理*/void DoError(void){sound(1200);delay(50);nosound();}/*赢棋处理*/void DoWin(int Order){sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);nosound();textcolor(RED+BLINK);gotoxy(25,20);if(Order==CHESS1)cputs("PLAYER1 WIN!");elsecputs("PLAYER2 WIN!");gotoxy(25,21);cputs(" \\<^+^>/");getch();}/*走棋*/int ChessGo(int Order,struct point Cursor){/*判断交叉点上有无棋子*/if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL){/*若没有棋子, 则可以落子*/gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); textcolor(LIGHTBLUE);putch(Order);gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); gChessBoard[Cursor.x][Cursor.y]=Order;return TRUE;}elsereturn FALSE;}/*判断当前行棋方落子后是否赢棋*/int JudgeWin(int Order,struct point Cursor){int i;for(i=0;i<4;i++)/*判断在指定方向上是否有连续5个行棋方的棋子*/if(JudgeWinLine(Order,Cursor,i))return TRUE;return FALSE;}/*判断在指定方向上是否有连续5个行棋方的棋子*/int JudgeWinLine(int Order,struct point Cursor,int direction) {int i;struct point pos,dpos;const int testnum = 5;int count;switch(direction){case 0:/*在水平方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y;dpos.x=1;dpos.y=0;break;case 1:/*在垂直方向*/pos.x=Cursor.x;pos.y=Cursor.y-(testnum-1);dpos.x=0;dpos.y=1;break;case 2:/*在左下至右上的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y+(testnum-1);dpos.x=1;dpos.y=-1;break;case 3:/*在左上至右下的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y-(testnum-1);dpos.x=1;dpos.y=1;break;}count=0;for(i=0;i<testnum*2+1;i++)/*????????i<testnum*2-1*/ {if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18) {if(gChessBoard[pos.x][pos.y]==Order){count++;if(count>=testnum)return TRUE;}elsecount=0;}pos.x+=dpos.x;pos.y+=dpos.y;}return FALSE;}/*移动光标*/void MoveCursor(int Order,int press) {switch(press){case PLAY1UP:if(Order==CHESS1&&gCursor.y>0) gCursor.y--;break;case PLAY1DOWN:if(Order==CHESS1&&gCursor.y<18) gCursor.y++;break;case PLAY1LEFT:if(Order==CHESS1&&gCursor.x>0) gCursor.x--;break;case PLAY1RIGHT:if(Order==CHESS1&&gCursor.x<18) gCursor.x++;break;case PLAY2UP:if(Order==CHESS2&&gCursor.y>0) gCursor.y--;break;case PLAY2DOWN:if(Order==CHESS2&&gCursor.y<18) gCursor.y++;break;case PLAY2LEFT:if(Order==CHESS2&&gCursor.x>0) gCursor.x--;break;case PLAY2RIGHT:if(Order==CHESS2&&gCursor.x<18) gCursor.x++;break;}gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*游戏结束处理*/void EndGame(void){textmode(C80);}/*显示当前行棋方*/void ShowOrderMsg(int Order){gotoxy(6,MAPYOFT+20);textcolor(LIGHTRED);if(Order==CHESS1)cputs("Player1 go!");elsecputs("Player2 go!");gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*落子正确处理*/void DoOK(void){sound(500);delay(70);sound(600);delay(50);sound(1000);delay(100);nosound();}/*检查用户的按键类别*/int CheckKey(int press){if(press==ESCAPE)return KEYEXIT;/*是退出键*/elseif( ( press==PLAY1DO && gPlayOrder==CHESS1) || ( press==PLAY2DO && gPlayOrder==CHESS2))return KEYFALLCHESS;/*是落子键*/elseif( press==PLAY1UP || press==PLAY1DOWN || press==PLAY1LEFT || press==PLAY1RIGHT || press==PLAY2UP || press==PLAY2DOWN ||press==PLAY2LEFT || press==PLAY2RIGHT)return KEYMOVECURSOR;/*是光标移动键*/elsereturn KEYINV ALID;/*按键无效*/}贪吃蛇#define N 200#include <graphics.h>#include <stdlib.h>#include <dos.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score=0;/*得分*/int gamespeed=50000;/*游戏速度自己调整*/ struct Food{int x;/*食物的横坐标*/int y;/*食物的纵坐标*/int yes;/*判断是否要出现食物的变量*/ }food;/*食物的结构体*/struct Snake{int x[N];int y[N];int node;/*蛇的节数*/int direction;/*蛇移动方向*/int life;/* 蛇的生命,0活着,1死亡*/}snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/void Init(void){int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/ void DrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49); /*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10); /*左边*/rectangle(601,i,610,i+10);/*右边*/}}/*玩游戏具体过程*/void GamePlay(void){randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=100;snake.y[0]=100;/*蛇头*/snake.x[1]=110;snake.y[1]=100;snake.node=2;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*/{food.x=rand()%400+60;food.y=rand()%350+60;while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/ food.x++;while(food.y%10!=0)food.y++;food.yes=0;/*画面上有食物了*/}if(food.yes==0)/*画面上有食物了就要显示*/{setcolor(GREEN);rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/{snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/switch(snake.direction){case 1:snake.x[0]+=10;break;case 2: snake.x[0]-=10;break;case 3: snake.y[0]-=10;break;case 4: snake.y[0]+=10;break;}for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/{if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){GameOver();/*显示失败*/snake.life=1;break;}}if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)/*蛇是否撞到墙壁*/{GameOver();/*本次游戏结束*/snake.life=1; /*蛇死*/}if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/ break;if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/{setcolor(0);/*把画面上的食物东西去掉*/rectangle(food.x,food.y,food.x+10,food.y-10);snake.x[snake.node]=-20;snake.y[snake.node]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/snake.node++;/*蛇的身体长一节*/food.yes=1;/*画面上需要出现新的食物*/score+=10;PrScore();/*输出新得分*/}setcolor(4);/*画出蛇*/for(i=0;i<snake.node;i++)rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);delay(gamespeed);setcolor(0);/*用黑色去除蛇的的最后一节*/rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);} /*endwhile(!kbhit)*/if(snake.life==1)/*如果蛇死就跳出循环*/break;key=bioskey(0);/*接收按键*/if(key==ESC)/*按ESC键退出*/break;elseif(key==UP&&snake.direction!=4)/*判断是否往相反的方向移动*/snake.direction=3;elseif(key==RIGHT&&snake.direction!=2)snake.direction=1;elseif(key==LEFT&&snake.direction!=1)snake.direction=2;elseif(key==DOWN&&snake.direction!=3)snake.direction=4;}/*endwhile(1)*/}/*游戏结束*/void GameOver(void){cleardevice();PrScore();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAME OVER");getch();}/*输出成绩*/void PrScore(void){char str[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"score:%d",score);outtextxy(55,20,str);}/*图形结束*/void Close(void){getch();closegraph();}扫雷游戏/*模拟扫雷游戏*/#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(EGA VGA_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语言代码该程序是一个经典的扫雷游戏,使用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语言和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语言编写的扫雷程序代码,供参考:#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语言扫雷代码,用于实现经典扫雷游戏的基本功能:#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语言扫雷程序代码,以下是一个简单的实现示例。
#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#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、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int e,f;
for(e=1;e<11;e++)
for(f=1;f<11;f++)
{
if(bomb[e][f]==1)
tru[e-1][f-1]='*';
else
tru[e-1][f-1]='_';
}
return 0;
}
int updt_tbl()//---------------------------------游戏中棋盘的更新
}
int welcome()
{
int i,j,m,a,num;
printf("Hello,welcome to Bomb Hunter!\nFirst of all,please input the number of bombs(eg:30):\n");
scanf("%d",&num);
printf("Now let's begin,good luck!\n");
{5,'|','_','_','_','_','_','_','_','_','_','_'},
{6,'|','_','_','_','_','_','_','_','_','_','_'},
{7,'|','_','_','_','_','_','_','_','_','_','_'},
{8,'|','_','_','_','_','_','_','_','_','_','_'},
{
int i,j;
for (i=1;i<11;i++)
for (j=1;j<11;j++)
{
if(tru[i-1][j-1]=='/'||tru[i-1][j-1]=='*')
tbl[i+1][j+1]=tru[i-1][j-1];
}//---------------------------------tbl的最终刷新
{
int i,j;
for (i=1;i<11;i++)
for (j=1;j<11;j++)
{
if(tru[i-1][j-1]!='/'&&tru[i-1][j-1]!='*')
tbl[i+1][j+1]=tru[i-1][j-1];
}
return 0;
}
int fnl_tbl()//-----------------------------结局棋盘的更新[final_table]
srand((int)time(0));
for (m=0;m<num;)
{
a=random(100);//----------------------0到99生成随机数,取个位和十位
j=a%10+1;
i=(a-j)/10+1;
if(bomb[i][j]!=1)
{
bomb[i][j]=1;
m++;
}
}//------------------------------------地雷数组生成
return 0;
}
int dspl()//----------------------------棋盘打印函数[display]
{
int s,t;
updt_tbl();
printf("\n");
for(s=0;s<12;s++)
{
for(t=0;t<12;t++)
if(tbl[s][t]=='_'||tbl[s][t]=='*'||tbl[s][t]=='X'||tbl[s][t]=='P'||tbl[s][t]=='/'||tbl[s][t]=='O'||tbl[s][t]=='+'||tbl[s][t]=='-'||tbl[s][t]=='|'||tbl[s][t]=='_'||tbl[s][t]==' '||tbl[s][t]=='Y')
printf("%3c",tbl[s][t]);//---------------------------打印字符
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)
int n=0;//-------------------------------------------完成度判断
int bomb[12][12]={{},{},{},{},{},{},{},{},{},{},{},{},};//地雷分布(地雷1、无n");
updt_tru();//---------------tru生成
dspl();//-------------------打印棋盘
return 0;
}
int updt_tru()//---------------------------真相数组的生成[update_truth]
{1,'|','_','_','_','_','_','_','_','_','_','_'},
{2,'|','_','_','_','_','_','_','_','_','_','_'},
{3,'|','_','_','_','_','_','_','_','_','_','_'},
{4,'|','_','_','_','_','_','_','_','_','_','_'},
int tru[10][10]={{},{},{},{},{},{},{},{},{},{}};//真相数组[truth](地雷数、地雷*、误判/、空白O)
int tbl[12][12]={{' ','X',1,2,3,4,5,6,7,8,9,10},
{'Y','+','-','-','-','-','-','-','-','-','-','-'},
{9,'|','_','_','_','_','_','_','_','_','_','_'},
{10,'|','_','_','_','_','_','_','_','_','_','_'}}; //棋盘数组[table](坐标系|-12、地雷数、红旗P、地雷*、爆炸X、误判/)
int main()//---------------------主函数
{
welcome();
do
{
game();
}
while (n<=9);//未胜利,继续进行游戏
if(n==10)
printf("Mission succeed!\n");//全部地雷标记完成,胜利
else
printf("Mission failed!\n");//n=11,引爆地雷,失败
return 0;