当前位置:网站首页>C language beginner level - realize the minesweeping game
C language beginner level - realize the minesweeping game
2022-07-01 05:46:00 【Xiao LV LC】
List of articles
C Language to achieve mine sweeping game
Introduction to the rules
I believe many people have played mine sweeping games , But Xiaobian still tells us the rules of this minesweeping game .
This is the first interface of a minesweeping game
Players find all the thunder arranged by the computer in these squares, that is, the game victory , Point to thunder and be killed .
The rules are simple , Now let's start with what we have learned C Language knowledge to achieve it .
C Language code implementation ideas
This program is divided into three parts ,game.h In the file, we write some header files and interface function declarations ,game.c In the file, we write specific interface functions ,test.c Write out the main functions, game menus and other functions in the file .
First we print a game menu
void menu()
{
printf("**********************\n");
printf("******* 1. play ******\n");
printf("******* 0. exit ******\n");
printf("**********************\n");
}
Players choose to enter the game
The following is the specific implementation framework of the game
void game()
{
char mine[ROWS][COLS] = {
0 };// Store the information of the arranged mine
char show[ROWS][COLS] = {
0 };// Store the information of the detected mine
// Initializes the number to the specified element
//mine The array is always when there is no thunder ‘0’
InitBoard(mine, ROWS, COLS,'0');
//show The array is always ‘*’
InitBoard(show, ROWS, COLS, '*');
// Arrange thunder
SetMine(mine, ROW, COL);
//
// Print chessboard
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
This is the implementation of the main function
int main()
{
int input=0;
// Set the starting point for generating random numbers
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(" Input error \n");
break;
}
} while (input);
return 0;
}
Next, we implement the specific interface function
First, we initialize the chessboard
// Initialize 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
// Print chessboard
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- The Minesweeper game --------\n");
for (j = 0;j <= col;j++) // Print column number
{
printf("%d ", j);
}
printf("\n");
for (i = 1;i <= row;i++)
{
printf("%d ", i); // Print line number
for (j = 1;j <= col;j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game --------\n");
}
Arrange thunder
// Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
Returns the number of mines around the selected coordinates
int get_mine_count(char board[ROWS][COLS],int x,int y)
{
return (board[x - 1][y - 1] +
board[x - 1][y] +
board[x - 1][y + 1] +
board[x][y - 1] +
board[x][y + 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] - 8 * '0');
}
Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;// Find the number of non thunder
while (win<row*col-EASY_COUNT)
{
printf(" Please enter the coordinates of the mine to be checked :");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf(" The coordinates have been checked , You can't check again !\n");
}
else
{
// If it's ray
if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else // If it wasn't ray
{
// Statistics mine Array of x y There are several mines around the coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0'; // Convert to numeric characters
DisplayBoard(show, ROW, COL);
}
}
}
else
printf(" The coordinates entered are illegal , Please re-enter !\n");
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}
Complete implementation code
game.h
#pragma once
#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 // The number of mines
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set); // Initialize chessboard
avoid DisplayBoard(char board[ROWS][COLS], int row, int col);// Print chessboard
void SetMine(char board[ROWS][COLS], int row, int col);// Arrange thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row,int col);// Check the thunder
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
// Initialize 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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- The Minesweeper game --------\n");
for (j = 0;j <= col;j++) // Print column number
{
printf("%d ", j);
}
printf("\n");
for (i = 1;i <= row;i++)
{
printf("%d ", i); // Print line number
for (j = 1;j <= col;j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game --------\n");
}
// Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS],int x,int y)
{
return (board[x - 1][y - 1] +
board[x - 1][y] +
board[x - 1][y + 1] +
board[x][y - 1] +
board[x][y + 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] - 8 * '0');
}
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;// Find the number of non thunder
while (win<row*col-EASY_COUNT)
{
printf(" Please enter the coordinates of the mine to be checked :");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf(" The coordinates have been checked , You can't check again !\n");
}
else
{
// If it's ray
if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else // If it wasn't ray
{
// Statistics mine Array of x y There are several mines around the coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0'; // Convert to numeric characters
DisplayBoard(show, ROW, COL);
}
}
}
else
printf(" The coordinates entered are illegal , Please re-enter !\n");
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#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 the information of the arranged mine
char show[ROWS][COLS] = {
0 };// Store the information of the detected mine
// Initializes the number to the specified element
//mine The array is always when there is no thunder ‘0’
InitBoard(mine, ROWS, COLS,'0');
//show The array is always ‘*’
InitBoard(show, ROWS, COLS, '*');
// Arrange thunder
SetMine(mine, ROW, COL);
//
// Print chessboard
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
int input=0;
// Set the starting point for generating random numbers
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(" Input error \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- It's not that you have a bad mind, but that you haven't found the right tool
- Deeply understand the underlying implementation principle of countdownlatch in concurrent programming
- Some errors encountered in MySQL data migration
- JDBC常见面试题
- rust猜数字游戏
- tese_Time_2h
- 从底层结构开始学习FPGA----RAM IP的定制与测试
- 穿越派与贸大合作,为大学生增添效率
- Speed regulation and stroke control based on Ti drv8424 driving stepper motor
- Idea start view project port
猜你喜欢
In depth understanding of condition source code interpretation and analysis of concurrent programming
Mongodb學習篇:安裝後的入門第一課
穿越派 你的数据云行
excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
论文学习记录随笔 多标签之LIFT
Mongodb学习篇:安装后的入门第一课
HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
Advanced cross platform application development (II): uni app practice
2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
【医学分割】u2net
随机推荐
千万不要把笔记视频乱放!
In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
π盘,让您电脑变成个人的私有云
不是你脑子不好用,而是因为你没有找到对的工具
Ucosiii --- engineering transplantation
C语言初阶——牛客网精选好题
数据治理:数据治理管理(第五篇)
It's not that you have a bad mind, but that you haven't found the right tool
MySQL converts milliseconds to time string
boot+jsp的高校社团管理系统(附源码下载链接)
Some errors encountered in MySQL data migration
JSON data comparer
葫芦儿 APP 使用帮助
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
Multi table operation - foreign key cascade operation
C语言初阶——实现扫雷游戏
Trust guessing numbers game
Idea start view project port
Educational administration management system (free source code)
What things you didn't understand when you were a child and didn't understand until you grew up?