当前位置:网站首页>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
边栏推荐
- Understand the application scenario and implementation mechanism of differential segment
- Why is this error reported when modifying records in the database
- 7-10 stack of hats (25 points) (C language solution)
- Mysql多表查询 #子查询
- Use of constraintlayout
- 7-2 and then what time (15 minutes)
- 【7.3】146. LRU caching mechanism
- retrofit
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Zzuli:1041 sum of sequence 2
猜你喜欢

Sword finger offer 28 Symmetric binary tree

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

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

retrofit

Adc128s022 ADC Verilog design and Implementation

Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them

Mysql多表查询 #子查询

NPM install is stuck with various strange errors of node NPY

Jiuyi cloud black free encryption free version source code

Code writing and playing method of tonybot humanoid robot at fixed distance
随机推荐
puzzle(016.3)千丝万缕
MySQL multi table query subquery
Tonybot humanoid robot checks the port and corresponds to port 0701
Get permissions dynamically
Solr series of full-text search engines - basic principles of full-text search
China PETG market forecast and Strategic Research Report (2022 Edition)
Add ZABBIX calculation type itemcalculated items
LNMP环境mail函数不能发送邮件解决
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
7-22 tortoise and rabbit race (result oriented)
Jiuyi cloud black free encryption free version source code
7-2 and then what time (15 minutes)
Strategy, tactics (and OKR)
Ultra simple mobile map development
Mysql多表查询 #子查询
Zzuli:1047 logarithmic table
Thread.sleep和TimeUnit.SECONDS.sleep的区别
X86 assembly language - Notes from real mode to protected mode
Zzuli:1057 prime number determination
提高效率 Or 增加成本,开发人员应如何理解结对编程?