当前位置:网站首页>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
边栏推荐
- 7-28 monkeys choose King (Joseph problem)
- 添加Zabbix计算类型项目Calculated items
- Zzuli: sum of 1051 square roots
- 7-20 print 99 formula table (format output)
- Tonybot Humanoïde Robot Infrared Remote play 0630
- 如何查询淘宝天猫的宝贝类目
- J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
- 【7.3】146. LRU caching mechanism
- Luogu p5194 [usaco05dec]scales s solution
- 【7.3】146. LRU缓存机制
猜你喜欢

Dllexport and dllimport

retrofit

Use of constraintlayout

Why is this error reported when modifying records in the database

分布式事务(Seata) 四大模式详解

Pyqt interface production (login + jump page)

Accelerating strategy learning using parallel differentiable simulation

Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

Tonybot humanoid robot starts for the first time 0630
随机推荐
Tonybot humanoid robot checks the port and corresponds to port 0701
Selective sorting
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Zzuli:1047 logarithmic table
7-14 sum integer segments (C language)
洛谷P5536 【XR-3】核心城市 题解
7-10 stack of hats (25 points) (C language solution)
China PETG market forecast and Strategic Research Report (2022 Edition)
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Raft agreement
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
Why is this error reported when modifying records in the database
etcd集群权限管理和账号密码使用
【7.3】146. LRU缓存机制
别再问自己适不适合做软件测试了
Accelerating strategy learning using parallel differentiable simulation
7-15 calculation of PI
556. The next larger element III