当前位置:网站首页>[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;
}
边栏推荐
- OLED12864 液晶屏
- Generics and generic constraints of typescript
- A collection of commonly used plug-ins for idea development tools
- 使用sha256文件验证下载的文件
- php按照指定字符,获取字符串中的部分值,并重组剩余字符串为新的数组
- [whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
- Matplotlib double Y axis + adjust legend position
- Fundamentals of software testing
- [paper translation] gcnet: non local networks meet squeeze exception networks and beyond
- 2022-2-14 learning xiangniuke project - Section 7 account setting
猜你喜欢

How vite is compatible with lower version browsers

RGB 无限立方体(高级版)

深度学习分类网络 -- AlexNet
![[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法](/img/25/73f11ab2711ed2cc9f20bc7f9116b6.png)
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法

How to write good code - Defensive Programming Guide

51单片机——ADC讲解(A/D转换、D/A转换)

VSCode paste image插件保存图片路径设置
![[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP](/img/25/73f11ab2711ed2cc9f20bc7f9116b6.png)
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP

我所理解的DRM显示框架

all3dp. All Arduino projects in com website (2022.7.1)
随机推荐
Zzuli:1068 binary number
【C语言】筛选法求素数
"Simple" infinite magic cube
Zzuli:1062 greatest common divisor
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
Opencv LBP features
Grbl software: basic knowledge of simple explanation
Spark概述
PHP inner class name is the same as the inner class method name
1036 Boys vs Girls
Typora installation (no need to enter serial number)
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
500. 键盘行
[personal test] copy and paste code between VirtualBox virtual machine and local
Several keywords in C language
Basic use of form
Software testing - concept
深度学习分类网络--Network in Network
JWT工具类