当前位置:网站首页>Minesweeping simple version
Minesweeping simple version
2022-07-29 03:14:00 【Wind rises and falls】
Preface
Code interpretation is in code
1.test.c
#include"game.h"
void menu()
{
printf("***********************\n");
printf("*******1.play 0.exit***\n");
printf("***********************\n");
}
void game()
{
char mine[rows][cols] = {
0 };
char slow[rows][cols] = {
0 };// initialization Two more lines Just to not cross the line
initboard(mine, row+2, col+2,'0');// Assuming that 9*9 My thunder disk become 11*11 To prevent the coordinates on the extreme edge from looking around 8 When there are two coordinates Transboundary
initboard(slow, row+2, col+2, '*');// initialization The thunder plate we want to see
displayboard(slow, row, col);// Print thunder disk
setmine(mine, col, row);// Arrange thunder
findmine(mine,slow, row, col);// Demining
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf(" Input number :>");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Input error \n");
break;
}
} while (input);
return 0;
}
2.game.h
#define _CRT_SECURE_NO_WARNINGS
#define row 9
#define col 9
#define rows row+2
#define cols col+2
#define easycount 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void initboard(char board[rows][cols], int Rows, int Cols);
void displayboard(char board[row][col], int Row, int Col);
void setmine(char mine[row][col], int Row, int Col);
void findmine(char mine[row][col],char slow[row][col], int Row, int Col);
3.game.c
#include"game.h"
void initboard(char board[rows][cols], int Rows, int Cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < Rows; i++)
{
for (j = 0; j < Cols; j++)
{
board[i][j] = set;// mine Thunder plate is used to bury thunder Pass it on '0'
} //slow The thunder disk is used to check thunder Pass it on '*'
}
}
void displayboard(char board[row][col], int Row, int Col)// Print thunder disk
{
int i = 0;
int j = 0;// Actually see 9*9 My thunder disk So it's coming That's ok 9 Column 9
for (i = 1; i <= Row; i++)
{
for (j = 1; j <= Col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void setmine(char mine[row][col], int Row, int Col)// Arrange thunder
{
int count = 10;// Set the number of mines to 10
while (count)
{
int x = rand() % row + 1;// take x y The coordinates of are set to random numbers
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int nofind(char mine[row][col], int x, int y)// Look around the coordinates 8 Whether there is thunder in the coordinates
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + + mine[x][y - 1] + mine[x][y + 1]
+ mine[x + 1][y] + mine[x + 1][y + 1] + mine[x + 1][y - 1]) - 8 * '0';
}// Because it's a character number So you need to subtract characters 0 Into a digital
void findmine(char mine[row][col], char slow[row][col],int Row, int Col)// Check the thunder
{
int x = 0;
int y = 0;
int count = 0;
int win = 0;
while (win<row*col-easycount)// Lei Panwei 9*9 -10 A thunder If not all of Ray's have been found And then jump out of the loop
{
printf(" Enter the coordinates of the mine to be checked :>");
scanf("%d%d", &x, &y);
int s1 = x;
int s2 = y;
if(x>=1&&y<=Row&&y>=1&&y<=Col)// The scope of thunder
{
if (mine[x][y] == '1')
{
printf(" Was blown up \n");
displayboard(mine, row, col);
break;
}
count = nofind(mine, x, y);
slow[x][y] = count+'0';// Send the number of whether there is thunder around to slow In the thunder disk
displayboard(slow, row, col); // Because it stores characters So convert to characters
win++;
}
else
{
printf(" Illegal coordinates \n");
}
}
if (win == row * col - easycount)//easycount To set the number of Mines
{
printf(" Mine clearance is successful \n");
displayboard(mine, row, col);// Print the distribution of thunder
}
}
边栏推荐
- Wechat's crazy use of glide - life cycle learning
- 2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
- 服务器运行管理制度
- MYSQL入门与进阶(十二)
- 万字详解 Google Play 上架应用标准包格式 AAB
- 生产部署zabbix5.0笔记
- Digital image processing Chapter 10 - image segmentation
- 力扣刷题之数组序号计算(每日一题7/28)
- 01-sdram: Code of initialization module
- Principle knowledge is useful
猜你喜欢

Verilog: blocking assignment and non blocking assignment

"PHP Basics" output approximate value of PI

爆肝整理JVM十大模块知识点总结,不信你还不懂

Alibaba Sentinel - workflow and principle analysis

从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析

Verilog的时间系统任务----$time、$stime、$realtime

Detailed steps for installing MySQL 8.0 under Linux

Navicat new database

Wechat's crazy use of glide - life cycle learning

HTB-Blue
随机推荐
JVM基础入门篇一(内存结构)
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
生产部署zabbix5.0笔记
带你来浅聊一下,单商户功能模块汇总
服务器运行管理制度
Calculation of array serial number of force deduction questions (daily question 7/28)
The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled
Principle knowledge is useful
Unity game special effects
简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
西瓜书学习第六章---SVM
MySQL installation and configuration super detailed tutorial and simple database and table building method
mysql大表联合查询优化,大事务优化,规避事务超时,锁等待超时与锁表
What is SOA (Service Oriented Architecture)?
C traps and defects Chapter 3 semantic "traps" 3.6 Boundary Calculation and asymmetric boundary
增量实时灾备笔记
爆肝整理JVM十大模块知识点总结,不信你还不懂
C traps and defects Chapter 3 semantic "traps" 3.8 operators &, |, and!
kubernetes-1.24.x 特性
C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters