当前位置:网站首页>[realize the simple version of minesweeping games]
[realize the simple version of minesweeping games]
2022-07-28 06:46:00 【JuLiJuLi.】
What needs to be done to realize the minesweeping game ?
1. First, we will create two two-dimensional arrays , The first one is used to show players for minesweeping , The second array is used to arrange Lei's , This array needs to be expanded by one circle , It is used to prevent several mines near the back calculation from crossing the boundary of the edge position .
2. First initialize the first two-dimensional array to all 0; Then lay thunder on some positions in the second chessboard .
3. How to generate random mines , Here we use srand Function and time Function to complete random values , In order to arrange random thunder .
4. Judge whether the coordinates entered by the player are thunder .
5. How to generate a prompt when the player sweeps to a place that is not a mine? How many mines are nearby .
How to realize , The code is as follows .
(1).Li.h The header file is used to declare the function .
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9 // That's ok
#define COL 9 // Column
#define ROWS ROW+2 // Prevent extra rows from crossing the boundary
#define COLS COL+2 // Prevent extra columns from crossing the boundary
#define EASY_COUNT 10 // The number of ray
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);(2)game.c The source file is used to realize the logic of the game .
#include "Li.h"
void menu()
{
printf("******************************\n");
printf("********* 1. play ********\n");
printf("********* 0. exit ********\n");
printf("******************************\n");
}
void game()
{
// The realization of mine sweeping game
char mine[ROWS][COLS] = { 0 };//'0' // It is used to store the information of the arranged mine
char show[ROWS][COLS] = { 0 };//'*' // Used to store the information of the detected mines
init_board(mine, ROWS, COLS, '0'); // Initialize the chessboard to '0'
init_board(show, ROWS, COLS, '*'); // Initialize the chessboard to '*'
// Arrange thunder
set_mine(mine, ROW, COL);
show_board(show, ROW, COL);
find_mine(mine, show, ROW, COL); // Check the thunder
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL)); // utilize srand Function and time Function to generate random numbers
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 reselect !\n");
break;
}
} while (input);
return 0;
}(3).Li.c The source file is used to realize the minesweeping game .
#include "Li.h"
void init_board(char arr[ROWS][COLS], int rows, int cols, char set) // Initialize chessboard
{
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) // Print chessboard
{
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) // Arrange thunder
{ // Arrange thunder "1" For ray
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1; // Control the generation range of random numbers
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y) // Check the thunder number
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) // Investigation results
{
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're killed in the blast !\n");
show_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
show_board(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinates , Re input \n");
}
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Demining succeeded !\n");
show_board(mine, ROW, COL);
}
}边栏推荐
- [the beginning of self redemption]
- Water rendering example
- Leetcode brush question diary sword finger offer II 048. serialization and deserialization binary tree
- [c language] - step by step to achieve minesweeping games
- ZOJ Problem 1005 jugs
- SSAO by computer shader (I)
- 【无标题】
- [c语言]--一步一步实现扫雷小游戏
- Dynamic planning -- multi-step stair climbing (advanced version of stair climbing)
- Personal understanding of Chinese remainder theorem
猜你喜欢

explain详解

Graphic pipeline foundation (part outside)

2021-11-10

NFT data storage blind box + mode system development

MySQL index optimization

AQS之ReentrantLock源码解析

Redis implementation of distributed lock and analysis of the main process of redismission distributed lock

水瓶效果制作

mongo ssl 配置实战

Source code analysis of countdownlatch of AQS
随机推荐
图形管线基础(番外篇)
[C language] string library function introduction and simulation
OJ 1242 freshman's debut
redis实现分布式锁思路及redission分布式锁主流程分析
prometheus监控nacos
Mongodb quick start
Question brushing record ---- reverse the linked list (reverse the whole linked list)
[pta---- output full arrangement]
Current learning progress
Two dimensional array practice: spiral matrix
mongoDB快速入门
OJ 1507 deletion problem
Graphic pipeline foundation (II)
图形管线基础(二)
Rain Scene Effect (I)
Feignclient @requestmapping parameter setting and simple method setting of request header
Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
feignclient @RequestMapping参数设置及请求头简易方式设置
OJ 1505 保险丝
订单交易简析