当前位置:网站首页>C language to realize mine sweeping
C language to realize mine sweeping
2022-07-03 14:36:00 【roseisbule】
Minesweeping is a very classic game . Today I use c Language to implement it .
First, clarify the idea of mine sweeping :
1. Use a two-dimensional array to store the location of Mines (arr2), Use another two-dimensional array to store the chessboard (arr1).
2. I typed arr1 Coordinates of a certain position in , The computer calculates the number of mines around this position , And print this point into thunder number . If it is 0, Print blank , And take the surrounding points as a new location point , To calculate , This can realize the function of expanding blank .
Then I have to finish so much , You need to implement these functions :1. Print function , Print chessboard .2. Buried lightning function , Computers bury mines randomly .3. Receive instruction function , Let the computer know that I want to clear mines , Or mark ray , Or unmark ray .4. Minesweeping function , Give the computer a coordinate , To determine whether they were killed , How many mines are there in this place without being killed , If it is 0, Then expand outward .5. Mark ray function , Give the coordinates , The computer marks this thunder , This point can no longer be scanned .6. Unmark ray function , Is to unmark .7. Extension function , The expansion function expands the blank , Because you need to scan around , So in leipan (arr1) On , The best we can do is scan the corner , Then it will scan how many mines there are around , This exceeds the size of the thunder disk , So our arr2 Need to be compared with arr1 A big circle , Then use function recursion to expand .
First , Write the menu function
void menu()
{
system("title Mine clearance ");
system("color f0");
system("date /T");
system("TIME /T");
printf("-----------------------------\n");
printf("****** 1. Start the game *******\n");
printf("****** 0. Quit the game *******\n");
printf("-----------------------------\n");
int input = 0;
char arr1[HANG][LIE] = { 0 };
char arr2[HANG + 2][LIE + 2] = { 0 };
printf(" Please select :");
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game(arr1,arr2,HANG,LIE);
break;
case 0:
printf(" Quit the game ");
return;
default:
printf(" error , Re input :");
}
}while (input);
}Then write two initialization functions , Yes arr1 and arr2 To initialize
void chushihua2(char arr[HANG+2][LIE+2],int hang,int lie,char a)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
for (j = 0; j < lie; j++)
arr[i][j] = a;
}
void chushihua(char arr[HANG][LIE], int hang, int lie,char a)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
for (j = 0; j < lie; j++)
arr[i][j] = a;
}Then write the print function
void display(char arr[HANG][LIE], int hang, int lie)
{
//system("cls");
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
printf(" %d ", i + 1);
printf("\n");
for (i = 0; i < hang; i++)
{
printf("%d", i + 1);
for (j = 0; j < lie - 1; j++)
{
printf(" %c |", arr[i][j]);
}
printf(" %c\n ", arr[i][j]);
if (i < hang - 1)
{
for (j = 0; j < lie - 1; j++)
printf("---|");
printf("---\n");
}
}
}Then write the theme of the game ,game function
void game(char arr1[HANG][LIE],char arr2[HANG+2][LIE+2],int hang,int lie)
{
flag = 0;
// arr1 The board arr2 answer
chushihua(arr1, HANG, LIE,'#');
chushihua2(arr2, HANG + 2, LIE + 2,'0');
put_lei(arr2, HANG + 2, LIE + 2);
while (1)
{
display(arr1,hang,lie);
jieguo(arr1,arr2,hang,lie);
put_order(arr1,arr2,hang,lie);
}
}Then there is the filling function
void put_lei(char arr[HANG + 2][LIE + 2], int hang, int lie)
{
int count = 0;
printf(" The computer is burying mines \n");
while (count < NUM)
{
int i = rand() % (hang - 2) + 1;
srand((unsigned int)(time(NULL)));
int j = rand() % (lie - 2) + 1;
if (arr[i][j] == '0')
{
arr[i][j] = '1';
count++;
}
}
}Then it gives the instruction function For convenience of measurement bug Add an additional debugging mode
void put_order(char arr1[HANG][LIE],char arr2[HANG+2][LIE+2],int hang,int lie)
{
if (max == 1)
{
int i = 1;
while (i)
{
int x = 0;
int y = 0;
printf(" Print chessboard (1) or Print mine disk (2) or Print the coordinates of all mines (3) or Exit debugging (4):");
scanf("%d", &i);
switch (i)
{
case 1:
for (x = 0; x < HANG; x++)
{
for (y = 0; y < LIE; y++)
printf("%c ", arr1[x][y]);
printf("\n");
}
break;
case 2:
for (x = 0; x < HANG+2; x++)
{
for (y = 0; y < LIE+2; y++)
printf("%c ", arr2[x][y]);
printf("\n");
}
break;
case 3:
for (x = 0; x < HANG + 2; x++)
{
for (y = 0; y < LIE + 2; y++)
{
if (arr2[x][y] == '1')
printf("%d ,%d \n", x, y);
}
}
break;
case 4:
max = 0;
return;
default:
printf(" error , Re input :");
}
}
}
if (max == 0)
{
printf(" Mine clearance (1) or Mark ray (2) or Unmark ray (3) or Tips (4) or Debug mode (5):");
int input = 0;
while (1)
{
scanf("%d", &input);
switch (input)
{
case 1:
saolei(arr1, arr2, hang, lie);
return;
case 2:
biaojilei(arr1, hang, lie);
return;
case 3:
quxiaobiaojilei(arr1, hang, lie);
return;
case 4:
tishi(arr1, arr2, hang, lie);
return;
case 5:
max = 1;
return;
default:
printf(" error , Re input :");
}
}
}
}Then write the mark Cancel mark Prompt function . The tag function needs to create a global variable flag( Marked quantity ), If flag=0, Then you can't unmark , Because it has not been marked . If flag = NUM( The number of ray ), Then you can't mark any more , Because if you mark the whole chessboard , That way, we can win directly .
void biaojilei(char arr[HANG][LIE],int hang,int lie)
{
if (flag == NUM)
{
printf(" The number of marked mines has reached the online \n");
return;
}
int i = 0;
int j = 0;
printf(" Enter the marking coordinates :");
while (1)
{
scanf("%d %d", &i, &j);
if (i<1 || i>hang || j<1 || j> lie)
{
printf(" error , Re input :");
continue;
}
if (arr[i - 1][j - 1] =='@')
{
printf(" This coordinate has been marked , Re input :");
continue;
}
if (arr[i - 1][j - 1] != '#')
{
printf(" This coordinate has been scanned , Re input :");
continue;
}
arr[i - 1][j - 1] = '@';
flag++;
break;
}
}
void quxiaobiaojilei(char arr[HANG][LIE],int hang,int lie)
{
if (flag == 0)
{
printf(" Not marked ray , Cannot unmark \n");
return;
}
int i = 0;
int j = 0;
printf(" Enter the coordinates of the mark :");
while (1)
{
scanf("%d %d", &i, &j);
if (i<1 || i>hang || j<1 || j> lie)
{
printf(" error , Re input :");
continue;
}
if(arr[i-1][j-1] != '@')
{
printf(" This position is not marked , Cannot unmark , Re input :");
continue;
}
else
{
arr[i - 1][j - 1] = '#';
flag--;
break;
}
}
}
void tishi(char arr1[HANG][LIE], char arr2[HANG + 2][LIE + 2], int hang, int lie)
{
int i = 0;
int j = 0;
for (i = 0; i < hang; i++)
for (j = 0; j < lie; j++)
{
if (arr2[i+1][j+1] == '1' && arr1[i][j] == '#')
{
arr1[i][j] = '@';
flag++;
return;
}
}
}Next is minesweeping and expanding the blank function
int jisuanleishu(char arr[HANG+2][LIE+2],int hang,int lie,int i,int j)
{
int sz= arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1] + arr[i][j - 1] + arr[i][j + 1] + arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1] - 8 * '0';
return sz;
}
void panduan(char arr1[HANG][LIE], char arr2[HANG + 2][LIE + 2], int hang, int lie,int i ,int j)
{
int sz = jisuanleishu(arr2, hang, lie, i, j);
if (sz == 0)
{
arr1[i - 1][j - 1] = ' ';
if (i - 1 >= 1 && i - 1 <= hang && j - 1 >= 1 && j - 1 <= lie && arr1[i - 2][j - 2] == '#')
panduan(arr1, arr2, hang, lie, i - 1, j - 1);
if (i >= 1 && i <= hang && j - 1 >= 1 && j - 1 <= lie && arr1[i - 1][j - 2] == '#')
panduan(arr1, arr2, hang, lie, i , j - 1);
if (i + 1 >= 1 && i + 1 <= hang && j - 1 >= 1 && j - 1 <= lie && arr1[i][j - 2] == '#')
panduan(arr1, arr2, hang, lie, i + 1, j - 1);
if (i - 1 >= 1 && i - 1 <= hang && j >= 1 && j <= lie && arr1[i - 2][j - 1] == '#')
panduan(arr1, arr2, hang, lie, i - 1, j );
if (i + 1 >= 1 && i + 1 <= hang && j >= 1 && j <= lie && arr1[i][j - 1] == '#')
panduan(arr1, arr2, hang, lie, i + 1, j);
if (i - 1 >= 1 && i - 1 <= hang && j + 1 >= 1 && j + 1 <= lie && arr1[i - 2][j] == '#')
panduan(arr1, arr2, hang, lie, i - 1, j + 1);
if (i >= 1 && i <= hang && j + 1 >= 1 && j + 1 <= lie && arr1[i - 1][j] == '#')
panduan(arr1, arr2, hang, lie, i , j + 1);
if (i + 1 >= 1 && i + 1 <= hang && j + 1 >= 1 && j + 1 <= lie && arr1[i][j] == '#')
panduan(arr1, arr2, hang, lie, i + 1, j + 1);
}
else
arr1[i - 1][j - 1] = '0' + sz;
}
void saolei(char arr1[HANG][LIE],char arr2[HANG+2][LIE+2],int hang,int lie)
{
int i = 0;
int j = 0;
printf(" Enter the coordinates of the scan :");
while (1)
{
scanf("%d %d", &i, &j);
if (i<1 || i>hang || j<1 || j> lie)
{
printf(" error , Re input :");
continue;
}
if (arr1[i - 1][j - 1] != '#')
{
printf(" This coordinate cannot be scanned , Because it's not an unknown point \n");
printf(" Re input :");
continue;
}
if (arr2[i][j] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
printf(" Return in three seconds \n");
Sleep(3000);
system("cls");
menu();
}
else
{
panduan(arr1, arr2, hang, lie, i, j);
return;
}
}
}Finally, we need a function to judge whether the minesweeping is successful
void jieguo(char arr1[HANG][LIE], char arr2[HANG+2][HANG+2],int hang,int lie)
{
int i = 0;
int j = 0;
int x = 0;
int y = 0;
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
if (arr1[i][j] == '#' && arr2[i + 1][j + 1] != '1')
{
y = 0;
break;
}
else
y++;
}
if (y == 0)
break;
}
for (i = 0; i < hang; i++)
{
for (j = 0; j < lie; j++)
{
if (arr1[i][j] == '@' && arr2[i + 1][j + 1] == '1')
x++;
if (x + y == NUM)
{
printf(" Mine clearance succeeded !\n");
printf(" Return in three seconds \n");
Sleep(3000);
system("cls");
menu();
return;
}
}
}
}thus , Most of the demining has been completed
The test diagram is as follows


gitee link :RoseIsBlue/learning_c_language - Gitee.com
边栏推荐
- Paper sharing: generating playful palettes from images
- [qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
- 动态获取权限
- 7-6 mixed type data format input
- The mail function of LNMP environment cannot send mail
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- tonybot 人形机器人 查看端口并对应端口 0701
- Mysql多表查询 #子查询
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- String reverse order
猜你喜欢

To improve efficiency or increase costs, how should developers understand pair programming?

tonybot 人形机器人 红外遥控玩法 0630

Understand the application scenario and implementation mechanism of differential segment

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules

一文了解微分段应用场景与实现机制

Tonybot humanoid robot infrared remote control play 0630
![洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解](/img/89/da1a3a38e02671628f385de0f30369.png)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解

论文分享:Generating Playful Palettes from Images

Detailed explanation of four modes of distributed transaction (Seata)
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution
随机推荐
7-28 monkeys choose King (Joseph problem)
7-20 print 99 formula table (format output)
使用并行可微模拟加速策略学习
Zzuli:1040 sum of sequence 1
Zzuli:1055 rabbit reproduction
Zzuli:1052 sum of sequence 4
Puzzle (016.3) is inextricably linked
一文了解微分段应用场景与实现机制
超简单手机地图开发
Pyqt interface production (login + jump page)
Find the sum of the elements of each row of the matrix
Why is this error reported when modifying records in the database
Find specified characters
Recent learning summary
7-14 sum integer segments (C language)
Zzuli:1042 sum of sequence 3
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
7-6 mixed type data format input
7-24 reduction of the simplest fraction (rolling Division)
Time conversion ()