In view of the greedy snake program I wrote last time, some people may not see that it is two files after writing the header file and the source file separately , Write all the code in the same file this time . Here's the code . This program did not go online to find some good-looking material , All the designs are made in Chinese EasyX Graphics library function drawing . If you have better looking material, you can modify it on this program . I left notes where I could put pictures .
==================================================================================================
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <graphics.h>
#define ROW 9// Number of rows
#define COL 9// Number of columns
#define NUM 10// Thunder number
#define SIZE 50// Picture size
int count=0;// Define a global variable to record the number of open cells , If this value is equal to ROW*COL-NUM, It means all the mines have been found , Every time 0-8 Hour meter plus 1
int map[ROW][COL];
//IMAGE img[12];// Save pictures , No suitable material has been found. This method is not used for the time being . use EasyX Draw mines and numbers .
// Initialize game
void init_game()
{
int i=0;// For circulation
int j=0;// For circulation
int n=0,m=0;// Used to traverse a nine palace lattice
int r=0;// that 's ok
int c=0;// column
// Initialize map
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
map[i][j]=0;
}
}
// Mine burying
for(i=0;i<NUM;)
{
// To avoid repetition , Please check if there is any thunder in this position
r=rand()%ROW;
c=rand()%COL;
if(map[r][c]==0)
{
map[r][c]=-1;
i++;
}
}
// Determine the values of other elements , Traverse all not for -1 Value of
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if(map[i][j]!=-1)// Not for -1 Value of , Ergodic nine palace lattice
{
for(n=(i-1<0?0:i-1);n<=(i+1>ROW-1?ROW-1:i+1);n++)
{
for(m=(j-1<0?0:j-1);m<=(j+1>COL-1?COL-1:j+1);m++)
{
if(map[n][m]==-1)
{
map[i][j]++;
}
}
}
}
}
}
// Encrypt so that you don't find out at the beginning of the game that those are numbers and mines , Decrypt when mouse clicks
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
map[i][j]+=20;// Encrypted digital range 19--28
}
}
// Load the material
//loadimage(&img[0],L"res/0.jpg",SIZE,SIZE);
setbkcolor(WHITE);// Set window background color
cleardevice();// Refresh Window
}
// Draw numbers
void draw_num(int r,int c)
{
count++;// Dot open lattice counting plus 1
char a=map[r][c]+48;
setfillcolor(RGB(0,0,255));
settextcolor(RGB(255,0,0));
settextstyle(30,0,L" Chinese Amber ");
solidrectangle(r*SIZE+1,c*SIZE+1,(r+1)*SIZE-1,(c+1)*SIZE-1);
outtextxy(r*SIZE+17,c*SIZE+10,a);
}
// Draw zero
void draw_zero(int r,int c)
{
count++;
setfillcolor(RGB(0,0,255));
settextstyle(30,0,L" Chinese Amber ");
solidrectangle(r*SIZE+1,c*SIZE+1,(r+1)*SIZE-1,(c+1)*SIZE-1);
}
// Mine painting
void draw_dilei(int r,int c)
{
setfillcolor(RGB(255,0,0));
solidcircle(r*SIZE+SIZE/2,c*SIZE+SIZE/2,SIZE/3);
outtextxy((r+1)*SIZE,(c+1)*SIZE,L" Step on the thunder , The game failed !");
}
// Draw a triangle
void draw_three(int r,int c)
{
setfillcolor(RGB(255,0,0));
POINT pts[] = { {r*SIZE+SIZE/2, c*SIZE+SIZE/4}, {r*SIZE+SIZE/4,
c*SIZE+SIZE/4*3}, {r*SIZE+SIZE/4*3, c*SIZE+SIZE/4*3} };
solidpolygon(pts, 3);
}
// Delete triangle
void delete_three(int r,int c)
{
POINT pts[] = { {r*SIZE+SIZE/2, c*SIZE+SIZE/4}, {r*SIZE+SIZE/4,
c*SIZE+SIZE/4*3}, {r*SIZE+SIZE/4*3, c*SIZE+SIZE/4*3} };
setfillcolor(RGB(0,255,0));
solidpolygon(pts, 3);
setfillcolor(RGB(255,0,0));
}
// Draw a map
void draw_map()
{
int i=0;// For circulation
int j=0;// For circulation
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if(map[i][j]>=19 && map[i][j]<=28)// Encrypted numbers
{
setfillcolor(RGB(0,255,0));
solidrectangle(i*SIZE+1,j*SIZE+1,(i+1)*SIZE-1,(j+1)*SIZE-1);
}
}
}
}
// Turn on the numbers 0 Grid around , because 0 There may be numbers around 0, So we use recursive functions
void open_zero(int i,int j)
{
int n=0,m=0;// Used to traverse a nine palace lattice
map[i][j]-=20;
draw_zero(i,j);
for(n=(i-1<0?0:i-1);n<=(i+1>ROW-1?ROW-1:i+1);n++)
{
for(m=(j-1<0?0:j-1);m<=(j+1>COL-1?COL-1:j+1);m++)
{
if(map[n][m]>=21 && map[n][m]<=28)// Encrypted numbers 1-8
{
map[n][m]-=20;
draw_num(n,m);
}
else if(map[n][m]==20)
{
open_zero(n,m);
}
}
}
}
// play a game
int play_game()
{
MOUSEMSG msg={0};
msg=GetMouseMsg();// Get mouse message
switch(msg.uMsg)
{
case WM_LBUTTONDOWN:// Left key
{
int r,c;// Selected subscript
r=msg.x/SIZE;
c=msg.y/SIZE;
if(map[r][c]>=21 && map[r][c]<=28)// number 1-8 Corresponding password
{
map[r][c]-=20;// decrypt
draw_num(r,c);
return 0;
}
else if(map[r][c]==20)// number 0 Corresponding password
{
open_zero(r,c);// number 0 It's special , Because if the number is 0 It means that there is no ray in all the little squares around Taitai , You can use the function to 0 All nearby grids are open
return 0;
}
else if(map[r][c]==19)// Mine code
{
draw_dilei(r,c);
return -1;
}
}
case WM_RBUTTONDOWN:// Right click
{
int r,c;// Selected subscript
r=msg.x/SIZE;
c=msg.y/SIZE;
if(map[r][c]>=19 && map[r][c]<=28)
{
map[r][c]+=20;// Secondary encryption
draw_three(r,c);
}
else if(map[r][c]>=39 && map[r][c]<=48)
{
map[r][c]-=20;// Solving quadratic encryption
delete_three(r,c);
}
}
default:return 0;
}
return 0;
}
int main()
{
srand((unsigned int)time(NULL));// Random number seed
initgraph(ROW*SIZE,COL*SIZE);// Initialize a window
init_game();// Initialize game
draw_map();// Draw a map
while(1)// Cycle play
{
if(play_game()==-1)// If you step on thunder, it's over
break;
if(count==ROW*COL-NUM)// All mines cleared. It's over
{
outtextxy(0,0,L" Congratulations on clearing all mines !");
break;
}
}
system("pause");// Program pause
return 0;
}
==================================================================================
If you don't understand, please leave a message below the comment .
Technology