当前位置:网站首页>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
边栏推荐
- 表单文本框的使用(一) 选择文本
- Mysql多表查询 #子查询
- Stop asking yourself if you are suitable for software testing
- 556. The next larger element III
- Common shortcut keys in PCB
- tonybot 人形机器人 查看端口并对应端口 0701
- Tonybot humanoid robot infrared remote control play 0630
- 7-19 check denomination (solve binary linear equation)
- 7-16 find the set of integers that meet the given conditions
- Tonybot humanoid robot checks the port and corresponds to port 0701
猜你喜欢
Adc128s022 ADC Verilog design and Implementation
Paper sharing: generating playful palettes from images
How to query the baby category of tmall on Taobao
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
tonybot 人形機器人 紅外遙控玩法 0630
Puzzle (016.4) domino effect
洛谷P4047 [JSOI2010]部落划分 题解
MySQL multi table query subquery
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
随机推荐
关于敏捷的一些概念
Dllexport et dllimport
NPM install is stuck with various strange errors of node NPY
Pyqt interface production (login + jump page)
【7.3】146. LRU缓存机制
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
7-3 count the number of words in a line of text
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
Sendmail无法发送邮件及发送过慢解决
Detailed explanation of four modes of distributed transaction (Seata)
Niuke: crossing the river
String reverse order
Optical cat super account password and broadband account password acquisition
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
Jiuyi cloud black free encryption free version source code
Detailed explanation of four modes of distributed transaction (Seata)
Zzuli:1041 sum of sequence 2
To improve efficiency or increase costs, how should developers understand pair programming?
论文分享:Generating Playful Palettes from Images