当前位置:网站首页>C language - minesweeping
C language - minesweeping
2022-06-25 05:18:00 【Summer orange TV】
Mine clearance
Introduction to the game
Minesweeping is a popular game . The goal of the Chinese version of minesweeping is to find all non minesweeping grids in the shortest time according to the numbers that appear in the click grid , At the same time, avoid stepping on thunder .
Game design
Module processing :
game.c: To implement a function
game.h: Place function header file
test.c: menu , Reference function
Initialize chessboard
void init_board(char arr[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++)
{
arr[i][j] = set;
}
}
}
Print chessboard
void show_board(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- Mine clearance -----------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("---------- Mine clearance ------------\n");
}
Arrange thunder
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] ='1';
count--;
}
}
}
Check the thunder
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf(" Please enter the coordinates to be checked :>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf(" unfortunately , You were blown up \n");
show_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine(mine, x, y);
show[x][y] = count + '0';
show_board(show, ROW, COL);
win++;
}
}
else
{
printf(" illegal input , Re input \n");
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
show_board(mine, ROW, COL);
}
}
}
Determine the number of surrounding mines during demining
int get_mine(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x-1][y + 1] +
mine[x ][y - 1]+
mine[x ][y+1] +
mine[x + 1][y - 1] +
mine[x+1][y ] +
mine[x +1][y + 1] - 8 * '0';
}
Master code
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void init_board(char arr[ROWS][COLS], int rows, int cols, char set);
void show_board(char arr[ROWS][COLS], int row, int col);
void set_mine(char mine[ROWS][COLS], int row, int col);
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c
#include "game.h"
void init_board(char arr[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++)
{
arr[i][j] = set;
}
}
}
void show_board(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- Mine clearance -----------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("---------- Mine clearance ------------\n");
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] ='1';
count--;
}
}
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf(" Please enter the coordinates to be checked :>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf(" unfortunately , You were blown up \n");
show_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine(mine, x, y);
show[x][y] = count + '0';
show_board(show, ROW, COL);
win++;
}
}
else
{
printf(" illegal input , Re input \n");
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
show_board(mine, ROW, COL);
}
}
}
int get_mine(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x-1][y + 1] +
mine[x ][y - 1]+
mine[x ][y+1] +
mine[x + 1][y - 1] +
mine[x+1][y ] +
mine[x +1][y + 1] - 8 * '0';
}
test.c
#include"game.h"
void menu()
{
printf("*********************\n");
printf("****** 1.play ****\n");
printf("****** 0.exit ****\n");
printf("*********************\n");
}
void game()
{
// Mine clearance
//mine The array is used to store the information of the deployed mine
char mine[ROWS][COLS] = {
0 };//'0'
// Check out the information of mine
char show[ROWS][COLS] = {
0 };//'*'
// Initialize chessboard
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
// Print chessboard
/*show_board(mine, ROW, COL); show_board(show, ROW, COL);*/
// Arrange thunder
set_mine(mine, ROW, COL);
show_board(show, ROW, COL);
// Check the thunder
show_board(mine, ROW, COL);
find_mine(mine, show, ROW, COL);
show_board(mine, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please enter :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , Please re-enter \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- Apache+php uploading large files
- Could not find “store“ in the context of “Connect(homePage)
- Page electronic clock (use js to dynamically obtain time display)
- Eyeshot Ultimate 2022 Crack By Xacker
- DOM document object model (I)
- Summary of SQL injection (I)
- How to choose the years of investment in financial products?
- Virtual honeypot Honeyd installation and deployment
- Read the general components of antd source code
- Five simple data types of JS
猜你喜欢

Personalized Federated Learning with Moreau Envelopes

Baidu ueeditor set toolbar initial value

Uva1103 ancient pictograph recognition

Penetration test - right raising topic

TeeChart Pro ActiveX 2022.1

Ranorex Studio 10.1 Crack

Flex flexible layout for mobile terminal page production
![Bind simulation, key points of interpreting bind handwritten code [details]](/img/03/6aa300bb8b8342199aed5a819f3634.jpg)
Bind simulation, key points of interpreting bind handwritten code [details]
![[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear](/img/12/d41f8d5abcb61d2632a8b117bf1604.jpg)
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear

CTFHub-rce
随机推荐
How to make colleagues under the same LAN connect to their own MySQL database
SSRF-lab
How micro engine uploads remote attachments
Fundamentals of C language
Click to jump out and drag the pop-up window
Five simple data types of JS
Introduction to the hardest core PWN in the whole network_ Graphic analysis
Enhanced paste quill editor
Virtual honeypot Honeyd installation and deployment
A summary of the experiment of continue and break in C language
Go Concurrency
buuctf(re)
电子协会 C语言 1级 28 、字符菱形
Laravel Vonage SMS sending
How to open the DWG file of the computer
Customize the console plot result style
hr竟主动给这位测试小姐姐涨工资,她是怎么做到的?
Dynamic programming example 1 leetcode 322 coin change
Mysql interactive_ Timeout and wait_ Timeout differences
Google Earth engine (GEE) - Global jrc/gsw1_ 1 / batch download of yearlyhistory dataset (China region)