当前位置:网站首页>[C language] simple implementation of mine sweeping game
[C language] simple implementation of mine sweeping game
2022-07-02 05:55:00 【SouLinya】
When implementing this little game , Every time we implement a function, we have to check whether there is bug, Function by function .
List of articles
1. Realize the idea
1.1 menu
void menu()
{
printf("********************\n");
printf("**1.play 0.exit**\n");
printf("********************\n");
}
int main()
{
srand((unsigned int)time(NULL));// The computer randomly arranges the location of thunder
int input = 0;
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();// Enter the game function
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}
1.2 Initialize chessboard
Ideas :
(1) Because the number and use of prompts to prevent thunder around the chessboard 1 It means thunder confusion , So we need to prepare two chessboards . A checkerboard randomly arranges thunder , Another chessboard is used to check the number of Mines . character 1 It means ray , character 0 It doesn't mean ray .
(2) Considering that users settle on the edge of the chessboard , We also need to check the number of thunder , If it is 99 Array of , Count the surrounding 8 Location (s) , It will cause cross-border , So what we need to prepare is 1111 Two chessboards of ( Auxiliary statistics ), But what we show users is 99 The chessboard of .
(3) Prepare two arrays .
A chessboard is initialized to characters 0( Ray hasn't been arranged yet );
A chessboard is initialized to characters ( Ray hasn't been checked yet , The game hasn't started yet )
// This is the function called
// Initialize two arrays
// One is initialized to '0'( It means that thunder has not been arranged )
//InitBoard(mine, ROWS, COLS,'0');
// One is initialized to '*'( It means that the investigation has not started yet )
//InitBoard(show, ROWS, COLS,'*');
// Implement the function of initializing the chessboard
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;
}
}
}
1.3 Print chessboard
Note that we need to print ( Exhibition ) The chessboard of is 9x9 Of , A print is all 0( It hasn't been laid yet ) The chessboard of , A print is all * The chessboard of . After initializing the chessboard , Print it out first to see if it can be achieved .
// Print chessboard
//Display(mine, ROW, COL);
//Display(show, ROW, COL);
// Print chessboard
void Display(char board[ROWS][COLS], int row, int col)
{
// What we want to print is 9*9 The chessboard of
int i = 0;
int j = 0;
printf("---------- The Minesweeper game ---------\n");
// Optimize the chessboard -- Print the number of columns
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// Print lines
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game ---------\n");
}
Print result display :
1.4 Random arrangement of thunder
Pay attention to it 9*9 Lay thunder on the chessboard of . After the arrangement, you can put mine Print out the array to see if it is randomly arranged each time
// stay 9*9 Lay thunder on the chessboard of
void SetMine(char mine[ROWS][COLS], int row, int col)
{
// Arrangement 10 A thunder
int count = boom;
while (count)
{
int x = rand() % 9 + 1;//1-9
int y = rand() % 9 + 1;//1-9
if (mine[x][y] == '0')// If there is no thunder in this position , Only with this coordinate can we lay thunder
{
mine[x][y] = '1';
count--;
}
}
}
1.5 Check the thunder
To check thunder, we need to consider whether the coordinates entered by players are legal , Whether it has been settled , And we should start from mine Get the number of mines around the user input coordinates in the array , That is to obtain the number of mines , And then on show In the corresponding position of the array .( Notice that we get numbers , And the characters we need )
1.5.1 Get the number of surrounding mines
int get_Mine(char mine[ROWS][COLS], int x, int y)
{
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 - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}
1.5.2 Convert numbers and characters
from ASCII It can be found in the code table ’0’ The value of is 48,’1’ The value of is 49, And our numbers 0 Namely 0,1 Namely 1, That is, the difference between numbers and characters 48, namely ’0’ Value . So if you want to convert numbers into characters , Just add the number ’0’, How to print in character form , To convert characters into numbers , Then subtract ’0’.
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;// The winning situation - Check out ROW*COL-boom
while (win<ROW*COL-boom)
{
printf(" Please enter the coordinates you want to check :");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);//1-9
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (show[x][y] != '*')
{
printf(" This coordinate has been checked \n");
}
else if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
Display(mine, ROW, COL);
break;
}
else
{
// Count how many mines are around this coordinate
// Notice that we get numbers 3, What we want is characters '3', Can be saved to the character array show in
int count= get_Mine(mine, x, y);
show[x][y] = count + '0';
Display(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinate input , Please re-enter \n");
}
}
if (win == ROW * COL - boom)
{
printf(" congratulations ! Mine clearance is successful !\n");
Display(mine, ROW, COL);
}
}
Judge the user's successful demining
Namely discharge win==ROW*COL- The number of ray
Two 、 Code implementation
The header file game.h
// The header file
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// Display chessboard
#define ROW 9
#define COL 9
// Auxiliary chessboard
#define ROWS ROW+1
#define COLS COL+2
// If you want to set up a few mines, set up a few mines
#define boom 70
// Function declaration
// Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
// Print chessboard
void Display(char board[ROWS][COLS], int row, int col);
// Arrange thunder Lay out Ray's array
void SetMine(char mine[ROWS][COLS], int row, int col);
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
The realization of game logic game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
// Implement the function of initializing the chessboard
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;
}
}
}
// Print chessboard
void Display(char board[ROWS][COLS], int row, int col)
{
// What we want to print is 9*9 The chessboard of
int i = 0;
int j = 0;
printf("---------- The Minesweeper game ---------\n");
// Optimize the chessboard -- Print the number of columns
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// Print lines
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game ---------\n");
}
// stay 9*9 Lay thunder on the chessboard of
void SetMine(char mine[ROWS][COLS], int row, int col)
{
// Arrangement 10 A thunder
int count = boom;
while (count)
{
int x = rand() % 9 + 1;//1-9
int y = rand() % 9 + 1;//1-9
if (mine[x][y] == '0')// If there is no thunder in this position , Only with this coordinate can we lay thunder
{
mine[x][y] = '1';
count--;
}
}
}
int get_Mine(char mine[ROWS][COLS], int x, int y)
{
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 - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;// The winning situation - Check out ROW*COL-boom
while (win<ROW*COL-boom)
{
printf(" Please enter the coordinates you want to check :");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);//1-9
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (show[x][y] != '*')
{
printf(" This coordinate has been checked \n");
}
else if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
Display(mine, ROW, COL);
break;
}
else
{
// Count how many mines are around this coordinate
// Notice that we get numbers 3, What we want is characters '3', Can be saved to the character array show in
int count= get_Mine(mine, x, y);
show[x][y] = count + '0';
Display(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinate input , Please re-enter \n");
}
}
if (win == ROW * COL - boom)
{
printf(" congratulations ! Mine clearance is successful !\n");
Display(mine, ROW, COL);
}
}
Functions to implement the game test.c
#include"game.h"
void menu()
{
printf("********************\n");
printf("**1.play 0.exit**\n");
printf("********************\n");
}
void game()
{
// Lay out Ray's array
char mine[ROWS][COLS] = {
0 };
// Check Ray's array
char show[ROWS][COLS] = {
0 };
// Initialize two arrays
// One is initialized to '0'( It means that thunder has not been arranged )
InitBoard(mine, ROWS, COLS,'0');
// One is initialized to '*'( It means that the investigation has not started yet )
InitBoard(show, ROWS, COLS,'*');
// Print chessboard
Display(mine, ROW, COL);
Display(show, ROW, COL);
// Arrange thunder
SetMine(mine, ROW, COL);
Display(mine, ROW, COL);
// Check the thunder
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));// The computer randomly arranges the location of thunder
int input = 0;
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();// Enter the game function
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
- Common websites for Postgraduates in data mining
- Small and medium-sized projects to achieve certification and authorization of hand filter
- Redis Key-Value数据库【初级】
- 如何写出好代码 — 防御式编程指南
- mock-用mockjs模拟后台返回数据
- Software testing Q & A
- Reading notes of cgnf: conditional graph neural fields
- 测试 - 用例篇
- Grbl software: basic knowledge of simple explanation
猜你喜欢

all3dp. All Arduino projects in com website (2022.7.1)

mysql的约束总结
![[golang syntax] be careful with the copy of slices](/img/5e/1c82c58940939b94d03377ebdc03e3.jpg)
[golang syntax] be careful with the copy of slices

Vscode paste image plugin saves image path settings

Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders

GRBL 软件:简单解释的基础知识

2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions

Technologists talk about open source: This is not just using love to generate electricity

OLED12864 液晶屏

数理统计与机器学习
随机推荐
Cube magique infini "simple"
JS determines whether the mobile terminal or the PC terminal
Regular expression summary
servlet的web.xml配置详解(3.0)
Zzuli:1064 encrypted characters
PHP array to XML
RGB infinite cube (advanced version)
JWT工具类
Some descriptions of Mipi protocol of LCD
Technologists talk about open source: This is not just using love to generate electricity
Can't the dist packaged by vite be opened directly in the browser
TI毫米波雷达学习(一)
软件测试基础篇
all3dp.com网站中全部Arduino项目(2022.7.1)
2022-2-14 learning xiangniuke project - Section 6 displays login information
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
DRM display framework as I understand it
Zzuli:1067 faulty odometer
测试 - 用例篇