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语言编写的扫雷游戏源代码

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语言扫雷程序大作业代码

原创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.}。

C编写的扫雷源程序

C编写的扫雷源程序

C编写的扫雷源程序集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-//h e a d e r f i l e #include<iostream>#include<cstdlib>#include<ctime>#include<windows.h>#include<conio.h>//defines#defineKEY_UP0xE048#defineKEY_DOWN0xE050#defineKEY_LEFT0xE04B#defineKEY_RIGHT0xE04D#defineKEY_ESC0x001B#defineKEY_1'1'#defineKEY_2'2'#defineKEY_3'3'#defineGAME_MAX_WIDTH100#defineGAME_MAX_HEIGHT100//StringsResource#defineSTR_GAMETITLE"ArrowKey:MoveCursorKey1:Open\Key2:MarkKey3:OpenNeighbors"#defineSTR_GAMEWIN"Congratulations!YouWin!Thankyouforplaying!\n "#defineSTR_GAMEOVER"GameOver,thankyouforplaying!\n"#defineSTR_GAMEEND"Presentedbyyzfy.PressESCtoexit\n"//-------------------------------------------------------------//BaseclassclassCConsoleWnd{public:staticintTextOut(constchar*);staticintGotoXY(int,int);staticintCharOut(int,int,constint);staticintTextOut(int,int,constchar*);staticintGetKey();public:};//{{//classCConsoleWnd////intCConsoleWnd::GetKey()//WaitforstandardinputandreturntheKeyCode//intCConsoleWnd::GetKey(){intnkey=getch(),nk=0;if(nkey>=128||nkey==0)nk=getch();returnnk>0nkey*256+nk:nkey;}////intCConsoleWnd::GotoXY(intx,inty)//Movecursorto(x,y)//OnlyConsoleApplication//intCConsoleWnd::GotoXY(intx,inty){COORDcd;cd.X=x;cd.Y=y;returnSetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);}////intCConsoleWnd::TextOut(constchar*pstr)//Outputastringatcurrentposition//intCConsoleWnd::TextOut(constchar*pstr){for(;*pstr;++pstr)putchar(*pstr);return0;}////intCConsoleWnd::CharOut(intx,inty,constintpstr) //Outputacharat(x,y)//intCConsoleWnd::CharOut(intx,inty,constintpstr) {GotoXY(x,y);returnputchar(pstr);}////intCConsoleWnd::TextOut(constchar*pstr)//Outputastringat(x,y)//intCConsoleWnd::TextOut(intx,inty,constchar*pstr) {GotoXY(x,y);returnTextOut(pstr);}//}}//-------------------------------------------------------------//ApplicationclassclassCSLGame:publicCConsoleWnd{private:private:intcurX,curY;intpoolWidth,poolHeight;intbm_gamepool[GAME_MAX_HEIGHT+2][GAME_MAX_WIDTH+2];public:CSLGame():curX(0),curY(0){poolWidth=poolHeight=0;}intInitPool(int,int,int);intMoveCursor(){returnCConsoleWnd::GotoXY(curX,curY);} intDrawPool(int);intWaitMessage();intGetShowNum(int,int);intTryOpen(int,int);private:intDFSShowNum(int,int);private:conststaticintGMARK_BOOM;conststaticintGMARK_EMPTY;conststaticintGMARK_MARK;};constintCSLGame::GMARK_BOOM=0x10; constintCSLGame::GMARK_EMPTY=0x100; constintCSLGame::GMARK_MARK=0x200;//{{//classCSLGame:publicCConsoleWnd////intCSLGame::InitPool(intWidth,intHeight,intnBoom) //Initializethegamepool.//IfWidth*Height<=nBoom,ornBoom<=0,//orWidthandHeightexceedlimit,thenreturn1//otherwisereturn0//intCSLGame::InitPool(intWidth,intHeight,intnBoom) {poolWidth=Width;poolHeight=Height;if(nBoom<=0||nBoom>=Width*Height||Width<=0||Width>GAME_MAX_WIDTH||Height<=0||Height>GAME_MAX_HEIGHT){return1;}//zeromemoryfor(inty=0;y<=Height+1;++y){for(intx=0;x<=Width+1;++x){bm_gamepool[y][x]=0;}}//initseedsrand(time(NULL));//initBoomswhile(nBoom){intx=rand()%Width+1,y=rand()%Height+1; if(bm_gamepool[y][x]==0){bm_gamepool[y][x]=GMARK_BOOM;--nBoom;}}//initcursorpositioncurX=curY=1;MoveCursor();return0;}////intCSLGame::DrawPool(intbDrawBoom=0) //DrawgamepooltoConsolewindow//intCSLGame::DrawPool(intbDrawBoom=0) {for(inty=1;y<=poolHeight;++y){CConsoleWnd::GotoXY(1,y);for(intx=1;x<=poolWidth;++x){if(bm_gamepool[y][x]==0){putchar('.');}elseif(bm_gamepool[y][x]==GMARK_EMPTY) {putchar('');}elseif(bm_gamepool[y][x]>0&&bm_gamepool[y][x]<=8){putchar('0'+bm_gamepool[y][x]);}elseif(bDrawBoom==0&&(bm_gamepool[y][x]&GMARK_MARK)) {putchar('#');}elseif(bm_gamepool[y][x]&GMARK_BOOM){if(bDrawBoom)putchar('*');elseputchar('.');}}}return0;}////intCSLGame::GetShowNum(intx,inty)//returnShowNumat(x,y)//intCSLGame::GetShowNum(intx,inty){intnCount=0;for(intY=-1;Y<=1;++Y)for(intX=-1;X<=1;++X){if(bm_gamepool[y+Y][x+X]&GMARK_BOOM)++nCount; }returnnCount;}////intCSLGame::TryOpen(intx,inty)//Tryopen(x,y)andshowthenumber//Ifthereisaboom,thenreturnEOF//intCSLGame::TryOpen(intx,inty){intnRT=0;if(bm_gamepool[y][x]&GMARK_BOOM){nRT=EOF;}else{intnCount=GetShowNum(x,y);if(nCount==0){DFSShowNum(x,y);}elsebm_gamepool[y][x]=nCount;}returnnRT;}////intCSLGame::DFSShowNum(intx,inty) //Privatefunction,nocomment//intCSLGame::DFSShowNum(intx,inty) {if((0<x&&x<=poolWidth)&&(0<y&&y<=poolHeight)&&(bm_gamepool[y][x]==0)){intnCount=GetShowNum(x,y);if(nCount==0){bm_gamepool[y][x]=GMARK_EMPTY;for(intY=-1;Y<=1;++Y)for(intX=-1;X<=1;++X){DFSShowNum(x+X,y+Y);}}elsebm_gamepool[y][x]=nCount;}return0;}////intCSLGame::WaitMessage()//Gameloop,waitandprocessaninputmessage //return:0:notend;1:Win;otherwise:Lose //intCSLGame::WaitMessage(){intnKey=CConsoleWnd::GetKey(); intnRT=0,nArrow=0;switch(nKey){caseKEY_UP:{if(curY>1)--curY;nArrow=1;}break;caseKEY_DOWN:{if(curY<poolHeight)++curY; nArrow=1;}break;caseKEY_LEFT:{if(curX>1)--curX;nArrow=1;}break;caseKEY_RIGHT:{if(curX<poolWidth)++curX;nArrow=1;}break;caseKEY_1:{nRT=TryOpen(curX,curY);}break;caseKEY_2:{if((bm_gamepool[curY][curX]&~(GMARK_MARK|GMARK_BOOM))==0){bm_gamepool[curY][curX]^=GMARK_MARK; }}break;caseKEY_3:{if(bm_gamepool[curY][curX]&0xF){intnb=bm_gamepool[curY][curX]&0xF; for(inty=-1;y<=1;++y)for(intx=-1;x<=1;++x){if(bm_gamepool[curY+y][curX+x]&GMARK_MARK) --nb;}if(nb==0){for(inty=-1;y<=1;++y)for(intx=-1;x<=1;++x){if((bm_gamepool[curY+y][curX+x]&(0xF|GMARK_MARK))==0){nRT|=TryOpen(curX+x,curY+y);}}}}}break;caseKEY_ESC:{nRT=EOF;}break;}if(nKey==KEY_1||nKey==KEY_3) {inty=1;for(;y<=poolHeight;++y){intx=1;for(;x<=poolWidth;++x){if(bm_gamepool[y][x]==0)break; }if(x<=poolWidth)break;}if(!(y<=poolHeight)){nRT=1;}}if(nArrow==0){DrawPool();}MoveCursor();returnnRT;}//}}//-------------------------------------------------------------//{{////mainfunction//intmain(void){intx=50,y=20,b=100,n;//definewidth&height&n_booms CSLGameslGame;//InitGame{CConsoleWnd::GotoXY(0,0);CConsoleWnd::TextOut(STR_GAMETITLE);slGame.InitPool(x,y,b);slGame.DrawPool();slGame.MoveCursor();}while((n=slGame.WaitMessage())==0)//GameMessageLoop;//EndoftheGame{slGame.DrawPool(1); CConsoleWnd::TextOut("\n");if(n==1){CConsoleWnd::TextOut(STR_GAMEWIN);}else{CConsoleWnd::TextOut(STR_GAMEOVER); }CConsoleWnd::TextOut(STR_GAMEEND);}while(CConsoleWnd::GetKey()!=KEY_ESC) ;return0;}//}}。

扫雷游戏代码

扫雷游戏代码

扫雷游戏代码standalone; self-contained; independent; self-governed;autocephalous; indie; absolute; unattached; substantive/**/#ifndef BLOCK_H_#define BLOCK_H_#include<QLabel>class QWidget;class Block:public QLabel{Q_OBJECTpublic:explicit Block(bool mine_flag,QWidget*parent=0);void set_number(int number);void turn_over();bool is_mine()const;bool is_turn_over()const;signals:void turn_over(bool is_mine);protected:void mousePressEvent(QMouseEvent*event); private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;};#endif#include""#include<QLabel>#include<QMouseEvent>#include<QPixmap>#include<QWidget>Block::Block(bool mine_flag,QWidget*parent) :QLabel(parent){mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(QPixmap(":/images/"));}void Block::set_number(int number){number_=number;}void Block::turn_over(){if(!turn_over_flag_){turn_over_flag_=true;if(mine_flag_)setPixmap(QPixmap(":/images/"));elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();}}bool Block::is_mine()const{return mine_flag_;}bool Block::is_turn_over()const{return turn_over_flag_;}/*鼠标事件的实现*/void Block::mousePressEvent(QMouseEvent*event){if(event->button()==Qt::LeftButton){if(!turn_over_flag_&&!mark_flag_){turn_over_flag_=true;if(mine_flag_==true){setPixmap(QPixmap(":/images/"));update();emit turn_over(true);}else{setPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();emit turn_over(false);}}}else if(event->button()==Qt::RightButton){if(!turn_over_flag_){if(!mark_flag_){mark_flag_=true;setPixmap(QPixmap(":/images/"));}else{mark_flag_=false;setPixmap(QPixmap(":/images/"));}update();}}QLabel::mousePressEvent(event);}#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#include""#include<QWidget>class QEvent;class QGridLayout;class QObject;class BlockArea:public QWidget{Q_OBJECTpublic:BlockArea(int row,int column,int mine_number,QWidget* parent=0);void set_block_area(int row,int column,intmine_number,int init_flag=false);signals:void game_over(bool is_win);protected:bool eventFilter(QObject*watched,QEvent*event); private slots:void slot_turn_over(bool is_mine);private:int calculate_mines(int x,int y)const;rg(easy_record_time_)),1,1);up_layout->addWidget(new QLabel(easy_record_name_),1,2);up_layout->addWidget(new QLabel(tr("Middle")),2,0);up_layout->addWidget(newQLabel(QString("%1").arg(middle_record_time_)),2,1);up_layout->addWidget(newQLabel(middle_record_name_),2,2);up_layout->addWidget(new QLabel(tr("Hard")),3,0);up_layout->addWidget(newQLabel(QString("%1").arg(hard_record_time_)),3,1);up_layout->addWidget(new QLabel(hard_record_name_),3,2);QPushButton*recount_button=newQPushButton(tr("recount"));QPushButton*close_button=new QPushButton(tr("close"));close_button->setDefault(true);connect(recount_button,SIGNAL(clicked()),&dialog,SLOT(ac cept()));connect(close_button,SIGNAL(clicked()),&dialog,SLOT(reje ct()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(recount_button);bottom_layout->addWidget(close_button);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted){easy_record_time_=middle_record_time_=hard_record_time_= g_no_record_time;easy_record_name_=middle_record_name_=hard_record_name_= g_no_record_name;}}void MainWindow::slot_show_game_toolBar(bool show){if(show)game_toolBar->show();elsegame_toolBar->hide();}void MainWindow::slot_show_statusBar(bool show){if(show)statusBar()->show();elsestatusBar()->hide();}/*游戏的设置容易、中等、困难及自定义*/void MainWindow::slot_standard(QAction*standard_action) {if(standard_action==easy_standard_action){current_standard_=0;row_=9;column_=9;mine_number_=10;}else if(standard_action==middle_standard_action){ current_standard_=1;row_=16;column_=16;mine_number_=40;}else if(standard_action==hard_standard_action){current_standard_=2;row_=16;column_=30;mine_number_=99;}else if(standard_action==custom_standard_action){ QDialog dialog;(tr("set standard"));QSpinBox*row_spinBox=new QSpinBox;row_spinBox->setRange(5,50);row_spinBox->setValue(row_);QSpinBox*column_spinBox=new QSpinBox;column_spinBox->setRange(5,50);column_spinBox->setValue(column_);QSpinBox*mine_spinBox=new QSpinBox;mine_spinBox->setValue(mine_number_);QHBoxLayout*up_layout=new QHBoxLayout;up_layout->addWidget(row_spinBox);up_layout->addWidget(column_spinBox);up_layout->addWidget(mine_spinBox);QDialogButtonBox*dialog_buttonBox=new QDialogButtonBox;dialog_buttonBox->addButton(QDialogButtonBox::Ok);dialog_buttonBox->addButton(QDialogButtonBox::Cancel);connect(dialog_buttonBox,SIGNAL(accepted()),&dialog,SLOT (accept()));connect(dialog_buttonBox,SIGNAL(rejected()),&dialog,SLOT (reject()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(dialog_buttonBox);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted)if(row_spinBox->value()*column_spinBox->value()>mine_spinBox->value()){current_standard_=3;row_=row_spinBox->value();column_=column_spinBox->value();mine_number_=mine_spinBox->value();}}slot_new_game();}/*实现帮助菜单中的关于游戏,及功能*/void MainWindow::slot_about_game(){QString introduction("<h2>"+tr("About Mine Sweepr")+"</h2>"+"<p>"+tr("This game is played by revealing squares of the grid,typically by clicking them with a mouse.If a square containing a mine is revealed,the player loses the game.Otherwise,a digit is revealed in the square,indicating the number of adjacent squares(out of the possible eight)that contain this number is zero then the square appears blank,and the surrounding squares are automatically also revealed.By using logic,the player can in many instances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed,or mine-filled,in which they can be marked as such(which is effected by right-clicking the square and indicated by a flag graphic).")+"</p>"+"<p>"+tr("This program is free software;you can redistribute it and/or under the terms of the GNU General Public License as published by the Software Foundation;either version3of the License,or(at your option)any later version.")+"</p>"+"<p>"+tr("Please see")+"<a href="+tr("for an overview of GPLv3licensing")+"</p>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author:")+g_software_author+"</br>");QMessageBoxmessageBox(QMessageBox::Information,tr("About Mine Sweeper"),introduction,QMessageBox::Ok);();}/*游戏的判断,及所给出的提示做出判断*/void MainWindow::slot_game_over(bool is_win){();QString name;if(is_win){switch(current_standard_){case0:if(time_label->text().toInt()<easy_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){easy_record_time_=time_label->text().toInt();easy_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case1:if(time_label->text().toInt()<middle_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){middle_record_time_=time_label->text().toInt();middle_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case2:if(time_label->text().toInt()<hard_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){hard_record_time_=time_label->text().toInt();hard_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;default:QMessageBox::information(this,tr("Result"),tr("Y ou win"));}}else{QMessageBox::information(this,tr("Result"),tr("You lose"));}}/*定时器的设置*/void MainWindow::slot_timer(){time_label->setText(QString("%1").arg()/1000));}/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/void MainWindow::read_settings(){QSettings settings;("MainWindow");resize("size").toSize());move("pos").toPoint());bool show_game_toolBar=("showGameToolBar").toBool();show_game_toolBar_action->setChecked(show_game_toolBar);slot_show_game_toolBar(show_game_toolBar);bool show_statusBar=("showStatusBar").toBool();show_statusBar_action->setChecked(show_statusBar);slot_show_statusBar(show_statusBar);();("GameSetting");current_standard_=("current_standard").toInt();switch(current_standard_){case0:easy_standard_action->setChecked(true);break;case1:middle_standard_action->setChecked(true);break;case2:hard_standard_action->setChecked(true);break;case3:custom_standard_action->setChecked(true);break;default:;}row_=("row").toInt()==09:("row").toInt();column_=("column").toInt()==09:("column").toInt();mine_number_=("mine_number").toInt()==010:("mine_number" ).toInt();();("Rank");easy_record_time_=("easy_record_time").toInt()==0g_no_re cord_time:("easy_record_time").toInt();middle_record_time_=("middle_record_time").toInt()==0g_n o_record_time:("middle_record_time").toInt();hard_record_time_=("hard_record_time").toInt()==0g_no_re cord_time:("hard_record_time").toInt();easy_record_name_=("easy_record_name").toString()==""g_n o_record_name:("easy_record_name").toString();middle_record_name_=("middle_record_name").toString()==" "g_no_record_name:("middle_record_name").toString();hard_record_name_=("hard_record_name").toString()==""g_n o_record_name:("hard_record_name").toString();();}void MainWindow::write_settings(){QSettings settings;("MainWindow");("size",size());("pos",pos());("showGameToolBar",show_game_toolBar_action->isChecked());("showStatusBar",show_statusBar_action->isChecked());();("GameSetting");("current_standard",current_standard_);("row",row_);("column",column_);("mine_number",mine_number_);();("Rank");("easy_record_time",easy_record_time_);("middle_record_time",middle_record_time_);("hard_record_time",hard_record_time_);("easy_record_name",easy_record_name_);("middle_record_name",middle_record_name_);("hard_record_name",hard_record_name_);();}/*菜单栏里图片的显示*/void MainWindow::create_actions(){new_game_action=new QAction(QIcon(":/images/"),tr("New Game"),this);new_game_action->setShortcut(QKeySequence::New);connect(new_game_action,SIGNAL(triggered()),this,SLOT(sl ot_new_game()));rank_action=newQAction(QIcon(":/images/"),tr("Rank"),this);connect(rank_action,SIGNAL(triggered()),this,SLOT(slot_r ank()));exit_action=newQAction(QIcon(":/images/"),tr("Exit"),this);exit_action->setShortcut(QKeySequence::Quit);connect(exit_action,SIGNAL(triggered()),this,SLOT(close( )));show_game_toolBar_action=new QAction(tr("Show Game Tool Bar"),this);show_game_toolBar_action->setCheckable(true);connect(show_game_toolBar_action,SIGNAL(toggled(bool)),t his,SLOT(slot_show_game_toolBar(bool)));show_statusBar_action=new QAction(tr("Show Status Bar"),this);show_statusBar_action->setCheckable(true);connect(show_statusBar_action,SIGNAL(toggled(bool)),this ,SLOT(slot_show_statusBar(bool)));easy_standard_action=newQAction(QIcon(":/images/"),tr("Easy"),this);easy_standard_action->setCheckable(true);middle_standard_action=newQAction(QIcon(":/images/"),tr("Middle"),this);middle_standard_action->setCheckable(true);hard_standard_action=newQAction(QIcon(":/images/"),tr("Hard"),this);hard_standard_action->setCheckable(true);custom_standard_action=newQAction(QIcon(":/images/"),tr("Custom"),this);custom_standard_action->setCheckable(true);standard_actionGroup=new QActionGroup(this);standard_actionGroup->addAction(easy_standard_action);standard_actionGroup->addAction(middle_standard_action);standard_actionGroup->addAction(hard_standard_action);standard_actionGroup->addAction(custom_standard_action);connect(standard_actionGroup,SIGNAL(triggered(QAction*)) ,this,SLOT(slot_standard(QAction*)));about_game_action=newQAction(QIcon(":/images/"),tr("About Game"),this);connect(about_game_action,SIGNAL(triggered()),this,SLOT( slot_about_game()));about_qt_action=new QAction(QIcon(":/images/"),tr("About Qt"),this);connect(about_qt_action,SIGNAL(triggered()),qApp,SLOT(ab outQt()));}/*菜单栏的创建*/void MainWindow::create_menus(){game_menu=menuBar()->addMenu(tr("Game"));game_menu->addAction(new_game_action);game_menu->addSeparator();game_menu->addAction(rank_action);game_menu->addSeparator();game_menu->addAction(exit_action);setting_menu=menuBar()->addMenu(tr("Setting"));setting_menu->addAction(show_game_toolBar_action);setting_menu->addAction(show_statusBar_action);setting_menu->addSeparator();setting_menu->addAction(easy_standard_action);setting_menu->addAction(middle_standard_action);setting_menu->addAction(hard_standard_action);setting_menu->addAction(custom_standard_action);help_menu=menuBar()->addMenu(tr("Help"));help_menu->addAction(about_game_action);help_menu->addAction(about_qt_action);}void MainWindow::create_game_toolBar(){game_toolBar=addToolBar(tr("Game Tool Bar"));game_toolBar->setFloatable(false);game_toolBar->setMovable(false);game_toolBar->addAction(new_game_action);game_toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); }void MainWindow::create_statusBar(){time_label=new QLabel;statusBar()->addPermanentWidget(time_label);statusBar()->addPermanentWidget(newQLabel(tr("second")));}#include""#include<QApplication>#include<QTranslator>int main(int argc,char*argv[]){QApplication app(argc,argv);QTranslator translator;(":/");(&translator);MainWindow window;();return();}。

扫雷算法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。

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

C语言扫雷源代码.txt如果背叛是一种勇气,那么接受背叛则需要更大的勇气。

爱情是块砖,婚姻是座山。

砖不在多,有一块就灵;山不在高,守一生就行。

#include <graphics.h> #include <stdlib.h>#include <dos.h>#include <conio.h>/*鼠标信息宏定义*/#define WAITING 0xff00#define LEFTPRESS 0xff01#define LEFTCLICK 0xff10#define LEFTDRAG 0xff19#define RIGHTPRESS 0xff02#define RIGHTCLICK 0xff20#define RIGHTDRAG 0xff2a#define MIDDLEPRESS 0xff04#define MIDDLECLICK 0xff40#define MIDDLEDRAG 0xff4c#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;int up[16][16],down[16][16],mouse_draw[16][16],pixel_save[16][16];void MouseMath()/*计算鼠标的样子*/{int i,j,jj,k;long UpNum[16]={0x3fff,0x1fff,0x0fff,0x07ff,0x03ff,0x01ff,0x00ff,0x007f,0x003f,0x00ff,0x01ff,0x10ff,0x30ff,0xf87f,0xf87f,0xfc3f };long DownNum[16]={0x0000,0x7c00,0x6000,0x7000, 0x7800,0x7c00,0x7e00,0x7f00, 0x7f80,0x7e00,0x7c00,0x4600, 0x0600,0x0300,0x0300,0x0180 };for(i=0;i<16;i++){j=jj=15;while(UpNum[i]!=0){up[i][j]=UpNum[i]%2;j--;UpNum[i]/=2;}while(DownNum[i]!=0){down[i][jj--]=DownNum[i]%2;DownNum[i]/=2;}for(k=j;k>=0;k--)up[i][k]=0;for(k=jj;k>=0;k--)down[i][k]=0;for(k=0;k<16;k++)/*四种组合方式*/{if(up[i][k]==0&&down[i][k]==0)mouse_draw[i][k]=1;else if(up[i][k]==0&&down[i][k]==1) mouse_draw[i][k]=2;else if(up[i][k]==1&&down[i][k]==0) mouse_draw[i][k]=3;elsemouse_draw[i][k]=4;}}mouse_draw[1][2]=4;/*特殊点*/}void Init(void);/*图形驱动*/void MouseOn(int,int);/*鼠标光标显示*/void MouseOff(void);/*鼠标光标隐藏*/void MouseSetXY(int,int);/*设置当前位置*/int LeftPress(void);/*左键按下*/int RightPress(void);/*鼠标右键按下*/int MiddlePress();void MouseGetXY(void);/*得到当前位置*/int MouseStatus();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();MouseMath();//MouseOn(MouseX,MouseY);Control();Close();}void Init(void)/*图形开始*/{int gd=DETECT,gm;registerbgidriver(EGAVGA_driver);initgraph(&gd,&gm,"");}void Close(void)/*图形关闭*/{closegraph();}/*鼠标光标显示*/void MouseOn(int x,int y){int i,j;int color;for(i=0;i<16;i++)/*画鼠标*/{for(j=0;j<16;j++){pixel_save[i][j]=getpixel(x+j,y+i);/*保存原来的颜色*/ if(mouse_draw[i][j]==1)putpixel(x+j,y+i,0);else if(mouse_draw[i][j]==2)putpixel(x+j,y+i,15);}}}/*隐藏鼠标*/void MouseOff(){int i,j,x,y,color;x=MouseX;y=MouseY;for(i=0;i<16;i++)/*原位置异或消去*/for(j=0;j<16;j++){if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)continue;color=getpixel(x+j,y+i);putpixel(x+j,y+i,color^color);putpixel(x+j,y+i,pixel_save[i][j]);}}void MouseSetXY(int x,int y)/*设置当前位置*/{_CX=x;_DX=y;_AX=0x04;geninterrupt(0x33);}int LeftPress(void)/*鼠标左键按下*/{_AX=0x03;geninterrupt(0x33);return(_BX&1);}int RightPress(void)/*鼠标右键按下*/{_AX=0x03;geninterrupt(0x33);return(_BX&2);}/*是否按下中键返回值同上 */int MiddlePress(){_AX=0x03;geninterrupt(0x33);return(_BX&4);}void MouseGetXY(void)/*得到当前位置*/{_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;}/*鼠标按键情况,返回0表示只移动,返回1表示左右键同时按下,2表示只按了左键,3表示只按了右键*/int MouseStatus(){int x,y;int status;int press=0;int i,j,color;status=0;/*默认鼠标没有移动*/x=MouseX;y=MouseY;while(x==MouseX&&y==MouseY&&status==0&&press==0){if(LeftPress()&&RightPress())press=1;else if(LeftPress())press=2;else if(RightPress())press=3;MouseGetXY();if(MouseX!=x||MouseY!=y)status=1;}if(status)/*移动情况才重新显示鼠标*/{for(i=0;i<16;i++)/*原位置异或消去*/for(j=0;j<16;j++){if(mouse_draw[i][j]==3||mouse_draw[i][j]==4)continue;color=getpixel(x+j,y+i);putpixel(x+j,y+i,color^color);putpixel(x+j,y+i,pixel_save[i][j]);}MouseOn(MouseX,MouseY);/*新位置显示*/}if(press!=0)/*有按键的情况*/return press;return 0;/*只移动的情况*/}void Control(void)/*游戏开始,重新,关闭*/{int gameFLAG=1;/*游戏失败后判断是否重新开始的标志*/while(1){MouseStatus();if(gameFLAG)/*游戏失败后没判断出重新开始或者退出游戏的话就继续判断*/ {GameBegain(); /*游戏初始画面*/GamePlay();/*具体游戏*/if(gameAGAIN==1)/*游戏中重新开始*/{gameAGAIN=0;continue;}}gameFLAG=0;if(LeftPress())/*判断是否重新开始*/{if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85){gameFLAG=1;continue;}}if(kbhit())/*判断是否按键退出*/break;}}void DrawSmile(void)/*画笑脸*/{MouseOff();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);MouseGetXY();MouseOn(MouseX,MouseY);}void DrawRedflag(int i,int j)/*显示红旗*/{MouseOff();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);MouseGetXY();MouseOn(MouseX,MouseY);}void DrawEmpty(int i,int j,int mode,int color)/*两种空格子的显示*/ {MouseOff();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);MouseGetXY();MouseOn(MouseX,MouseY);}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;/*变量取空白格数量*/}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);MouseOff();fillellipse(200+j*20,100+i*20,7,7);MouseGetXY();MouseOn(MouseX,MouseY);}}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)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++;}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++;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);/*把格子周围一共有多少雷数的统计结果返回*/ }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);MouseOff();outtextxy(195+j*20,95+i*20,randmineNUM);MouseGetXY();MouseOn(MouseX,MouseY);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);}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()){ MouseStatus();if(LeftPress())/*鼠标左键盘按下*/{if(MouseX>280&&MouseX<300&&MouseY>65&&MouseY<85)/*重新来*/{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)/*鼠标按下的格子是地雷*/{GameOver();/*游戏失败*/break;}else/*鼠标按下的格子不是地雷*/{Num=MineStatistics(i,j);if(Num==0)/*周围没地雷就用递归算法来显示空白格子*/ShowWhite(i,j);else/*按下格子周围有地雷*/{MouseOff();sprintf(randmineNUM,"%d",Num);/*输出当前格子周围的雷数*/setcolor(RED);outtextxy(195+j*20,95+i*20,randmineNUM);mineNUM--;MouseGetXY();MouseOn(MouseX,MouseY);}Mine[i][j].num=0;/*点过的格子周围雷数的数字变为0表示这个格子已经用过*/if(mineNUM<1)/*胜利了*/{GameWin();break;}}}}}if(RightPress())/*鼠标右键键盘按下*/{if(MouseX>190&&MouseX<390&&MouseY>90&&MouseY<290)/*当前鼠标位置在格子范围内*/{j=(MouseX-190)/20;/*x坐标*/i=(MouseY-90)/20;/*y坐标*/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;}}delay(1000000);delay(1000000);delay(1000000);delay(1000000);delay(1000000);}} }。

相关文档
最新文档