当前位置:网站首页>C语言初阶——实现扫雷游戏
C语言初阶——实现扫雷游戏
2022-07-01 05:35:00 【小吕同学lc】
C语言实现扫雷游戏
规则介绍
相信很多人都玩过扫雷游戏,但小编还是给大家说一下这个扫雷游戏的规则。
这是一个扫雷游戏的初识界面

玩家在这些个方格中找出电脑布置的所有雷即游戏胜利,点到雷则被炸死。
规则很简单,下面我们开始用所学的C语言知识来实现它。
C语言代码实现思路
这个程序我们分三个部分写,game.h文件中我们写一些头文件和接口函数的声明,game.c文件中我们写具体的接口函数,test.c文件中写出主函数和游戏菜单等功能。
首先我们打印一个游戏的菜单
void menu()
{
printf("**********************\n");
printf("******* 1. play ******\n");
printf("******* 0. exit ******\n");
printf("**********************\n");
}
玩家选择后进入游戏
下面是游戏的具体实现框架
void game()
{
char mine[ROWS][COLS] = {
0 };//存放布置好的雷的信息
char show[ROWS][COLS] = {
0 };//存放排查出的雷的信息
//初始化数字为指定元素
//mine数组在没有布置雷的时候都为‘0’
InitBoard(mine, ROWS, COLS,'0');
//show数组在没有排查雷的时候都为‘*’
InitBoard(show, ROWS, COLS, '*');
//布置雷
SetMine(mine, ROW, COL);
//
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
这是主函数的实现
int main()
{
int input=0;
//设置随机数的生成起点
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}
下面我们实现具体的接口函数
首先我们初始化棋盘
//初始化棋盘
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;
}
}
}
打印棋盘
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- 扫雷游戏 --------\n");
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 ", board[i][j]);
}
printf("\n");
}
printf("---------- 扫雷游戏 --------\n");
}
布置雷
//布置雷
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');
}
排查雷
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("请输入要排查的雷的坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("该坐标被排查过了,不能重复排查!\n");
}
else
{
//如果是雷
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else //如果不是雷
{
//统计mine数组的x y 坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0'; //转化为数字字符
DisplayBoard(show, ROW, COL);
}
}
}
else
printf("输入的坐标非法,请重新输入!\n");
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}

完整实现代码
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 //雷的数量
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set); //初始化棋盘
avoid DisplayBoard(char board[ROWS][COLS], int row, int col);//打印棋盘
void SetMine(char board[ROWS][COLS], int row, int col);//布置雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row,int col);//排查雷
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//初始化棋盘
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;
}
}
}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- 扫雷游戏 --------\n");
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 ", board[i][j]);
}
printf("\n");
}
printf("---------- 扫雷游戏 --------\n");
}
//布置雷
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');
}
//排查雷
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("请输入要排查的雷的坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("该坐标被排查过了,不能重复排查!\n");
}
else
{
//如果是雷
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else //如果不是雷
{
//统计mine数组的x y 坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0'; //转化为数字字符
DisplayBoard(show, ROW, COL);
}
}
}
else
printf("输入的坐标非法,请重新输入!\n");
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\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 };//存放布置好的雷的信息
char show[ROWS][COLS] = {
0 };//存放排查出的雷的信息
//初始化数字为指定元素
//mine数组在没有布置雷的时候都为‘0’
InitBoard(mine, ROWS, COLS,'0');
//show数组在没有排查雷的时候都为‘*’
InitBoard(show, ROWS, COLS, '*');
//布置雷
SetMine(mine, ROW, COL);
//
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
int input=0;
//设置随机数的生成起点
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}
边栏推荐
- [RootersCTF2019]babyWeb
- ssm+mysql二手交易网站(论文+源码获取链接)
- Use and principle of wait notify
- Educational administration management system (free source code)
- [SRS] use of Vhost isolated stream: push / pull Stream Address
- CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记
- More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv.so‘.
- Mongodb learning chapter: introduction after installation lesson 1
- Rust hello-word
- 导数的左右极限和左右导数的辨析
猜你喜欢

Simple implementation of database connection pool

In depth understanding of condition source code interpretation and analysis of concurrent programming

Spanner 论文小结

数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化

云原生存储解决方案Rook-Ceph与Rainbond结合的实践

How to create a progress bar that changes color according to progress

Precautions for use of conductive slip ring

使用 Nocalhost 开发 Rainbond 上的微服务应用

busybox生成的东西
![[RootersCTF2019]babyWeb](/img/b4/aa8f8e107a9dacbace72d4717b1834.png)
[RootersCTF2019]babyWeb
随机推荐
运行时候的导包搜索路径虽然pycharm中标红但不影响程序的执行
SSM的教务管理系统(免费源码获取)
【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
Summary of spanner's paper
在Rainbond中一键部署高可用 EMQX 集群
boot+jsp的高校社团管理系统(附源码下载链接)
Simple implementation of database connection pool
eBPF Cilium实战(2) - 底层网络可观测性
Understand several related problems in JVM - JVM memory layout, class loading mechanism, garbage collection
ssm+mysql二手交易网站(论文+源码获取链接)
Detailed explanation of set
0xc000007b the application cannot start the solution normally (the pro test is valid)
Data governance: data governance framework (Part I)
Data governance: metadata management implementation (Part IV)
【问题思考总结】为什么寄存器清零是在用户态进行的?
Web Security (IX) what is JWT?
CockroachDB 分布式事务源码分析之 TxnCoordSender
小程序常用组件小结
Precautions for use of conductive slip ring
【QT】qt加减乘除之后,保留小数点后两位