当前位置:网站首页>C language to achieve minesweeping games
C language to achieve minesweeping games
2022-06-11 07:31:00 【∞ big understand】
Minesweeping games
Through a period of C Language learning , Presumably, the little friends are also eager to write some small programs , This simple minesweeping game , Very suitable C Language beginners to practice .
Achieve mine clearance , First there must be two chessboards , A chessboard holds the information of thunder , The other one is for display on the screen ; Then the player enters the coordinates to clear the mine , If the coordinates entered are thunder , The game is over , If there is no thunder, the corresponding position of the chessboard will be displayed , Show the number of surrounding mines ; Finally, check all the coordinates and avoid being killed by mines , Is victory .
The above is the basic logic of mine clearance , now , Step by step .
1. Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
mine It's the chess board that releases thunder ,show Is to show the chessboard . We put mine Are initialized to ‘0’, Lei Wei ‘1’,show
All initialized to ‘*’.
2. Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;//10 A thunder
while (count)
{
//1. Generate random subscripts
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
3. Check the thunder
Checking thunder is the key to this game , It's also a little difficult , First , You need to pass both chessboards , Every time I input the coordinates, I will look in the thunder pan , If it's ray, the game is over , It is not ray that will make the position of the corresponding display panel Show the number of thunder around .
void FindMine(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're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinates , Re input \n");
}
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}
see (x,y) A function of the surrounding ray number :
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
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');
}
The above steps make the minesweeping game complete step by step , Next is the running code of the game :
1. The header file
#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
// Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
// Show chessboard
void DisplayBoard(char board[ROWS][COLS], int row, int col);
// Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col);
// Check Ray's
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
2. Game code
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("------------------------\n");
for (i = 0; i <= 9; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("------------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
//1. Generate random subscripts
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
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 FindMine(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're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinates , Re input \n");
}
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}
3. General procedure
#include "game.h"
void menu()
{
printf("********************************\n");
printf("********* 1. play ********\n");
printf("********* 0. exit ********\n");
printf("********************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };// Store Ray's information
char show[ROWS][COLS] = {
0 };// Store the information of the detected mine
// Initialize the chessboard
InitBoard(mine, ROWS, COLS, '0');//'0'
InitBoard(show, ROWS, COLS, '*');//'*'
// Arrange thunder
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
// Check the thunder
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , To choose !\n");
break;
}
} while (input);
return 0;
}
That's what this issue is about
边栏推荐
猜你喜欢

2022低压电工考题及在线模拟考试

Import on CSDN MD file

RTMP protocol

JVM学习记录(七)——类加载过程与双亲委派模型

如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!

QT interface nested movement based on qscrollarea

【软件测试】这样的简历已经刷掉了90%的面试者

Mobile console Gobang (first draft of detailed design)
![[Oracle database] mammy tutorial day03 Sorting Query](/img/ea/24c9495a2ef4f1786f7b7852bde321.png)
[Oracle database] mammy tutorial day03 Sorting Query

Implementation of stack (C language)
随机推荐
What is the lifecycle of automated testing?
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
10 advanced concepts that must be understood in learning SQL
gaussDB for redis和 redis的区别?
【CF】 A. New Year Candles
JVM learning record (VII) -- class loading process and parental delegation model
C language volatile
Implementation of stack (C language)
【CF#697 (Div. 3)】 A - Odd Divisor
QT table display data
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
JVM tuning
2022 melting welding and thermal cutting exam exercises and answers
C language inherits memory management mechanism (unfinished)
Sdl-4 PCM playback
【软件测试】这样的简历已经刷掉了90%的面试者
自动化测试的生命周期是什么?
模线性方程组(中国剩余定理+通用解法)
2022 low voltage electrician test questions and online simulation test
Gobang interface of mobile console (C language)