当前位置:网站首页>Mining game (C language)
Mining game (C language)
2022-08-02 06:44:00 【Fruit Chenchen】
文章目录
Game preface preparation
1.Initialize the game layout for Minesweeper using a 2D array,Changed by changing the row and column of the array,用ROWto specify the number of rows in the array,COLto specify the number of columns in the array,Minesweeper requires two two-dimensional arrays,An array is used to store mine information,An array is used to store display interface information,Used for fear of border crossingROW+2和COL+2array to avoid out of bounds;
2.Displays the minesweeper game interface using a 2D array,In addition to displaying array information,It is also necessary to display the coordinates in the first row and first column so that the player can determine the coordinates of the mine clearance;
3.设置雷区,A fixed number of mines are randomly placed according to the difficulty of the game,使用#defineIt is defined to change the value at any time to achieve a different number of mines;
4.排雷,Whenever the coordinates selected by the player are not mine,Then calculate the number of mines in a circle of eight grids around this coordinate.
5.判断输赢,If the player steps on a thunder,The game ends immediately,If the number of unlined areas is equal to the number of mines and the player is not killed,则玩家赢.
一、建立游戏菜单
void menu()
{
printf("********************************\n");
printf("********* 扫雷游戏 ********\n");
printf("********************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("********************************\n");
printf("********************************\n");
}
nt main()
{
int input = 0;
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
二、建立棋盘
void init_broad(char broad[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++)
{
broad[i][j] = set;
}
}
}
三、初始化棋盘
void display_broad(char broad[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
//列号
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
//行号
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", broad[i][j]);
}
printf("\n");
}
}
四、布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = DEFAULT_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
五、Check for mines and determine whether to step on mines
int get_mine_count(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 find_mine(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 - DEFAULT_COUNT)
{
printf("请输入要排查雷的坐标:>\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display_broad(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, row, col);
show[x][y] = count + '0';
display_broad(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - DEFAULT_COUNT)
{
printf("恭喜你,排雷成功\n");
display_broad(mine, ROW, COL);
}
}
六、源码
1.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("********************************\n");
printf("********* 扫雷游戏 ********\n");
printf("********************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("********************************\n");
printf("********************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };//Design arrays to store information
char show[ROWS][COLS] = {
0 };
//初始化棋盘
init_broad(mine, ROWS, COLS, '0');
init_broad(show, ROWS, COLS, '*');
//打印棋盘
display_broad(show, ROW, COL);
//布置雷
set_mine(mine, ROW, COL);
//排雷
find_mine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
2.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_broad(char broad[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++)
{
broad[i][j] = set;
}
}
}
void display_broad(char broad[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
//列号
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
//行号
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", broad[i][j]);
}
printf("\n");
}
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = DEFAULT_COUNT;
while (count)
{
int x = rand() % row + 1;
int 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)
{
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)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - DEFAULT_COUNT)
{
printf("请输入要排查雷的坐标:>\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display_broad(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, row, col);
show[x][y] = count + '0';
display_broad(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - DEFAULT_COUNT)
{
printf("恭喜你,排雷成功\n");
display_broad(mine, ROW, COL);
}
}
3.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 DEFAULT_COUNT 10
//初始化棋盘
void init_broad(char broad[ROWS][COLS],int rows,int cols,char set);
//打印棋盘
void display_broad(char broad[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);
边栏推荐
- What are the ways to improve software testing capabilities?After reading this article, it will take you up a notch
- Nacos installation configuration and single-machine deployment tutorial
- Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
- C语言基础知识梳理总结:零基础入门请看这一篇
- C语言操作符详解(2)
- Nacos客户端启动出现9848端口错误分析(非版本升级问题)
- MarkDown Formula Instruction Manual
- [C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
- Difference between npm and yarn
- 淘系资深工程师整理的300+项学习资源清单(2021最新版)
猜你喜欢
随机推荐
APT + Transform 实现多模块应用Application生命周期分发
The international top conference OSDI included Taobao system papers for the first time, and the device-cloud collaborative intelligence was recommended by the keynote speech of the conference
制作web3d动态产品展示的优点
关于 VS Code 优化启动性能的实践
Alluxio为Presto赋能跨云的自助服务能力
pytorch basic operations: classification tasks using neural networks
npm ---- install yarn
程序员写PPT的小技巧
深入剖析成员变量和局部变量的初始化问题
Tips for programmers to write PPT
物联网如何改变城市运行效率
What is the most important ability of a programmer?
金蝶国际:半年亏掉去年一年,疯狂烧钱的商业模式如何持续
go里面的基本知识
Thread Basics (1)
oracle 远程连接数据库
Home NAS server (4) | MergerFS and SnapRaid data backup
【OpenCV从入门到实践】图像处理技术[像素](全网最详细)
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
如何进行并发数计算(稳定性测试和压力测试)?









