当前位置:网站首页>扫雷小游戏——C语言
扫雷小游戏——C语言
2022-07-31 04:23:00 【硌手的小虫子@】
目录
一、游戏规则:
二、运行逻辑:
三、基本步骤:
四、总代码:
一、游戏规则:
扫雷游戏是根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即失败。
二、运行逻辑:
1、创建和打印游戏菜单。
2、创建两个棋盘数组,一个是布置雷的棋盘数组,一个是排查雷的棋盘数组。
3、初始化两个棋盘,把布置雷的棋盘全部初始为' 0 ' ,把排查雷的棋盘全部初始化为' * ' 。
4、打印棋盘。
5、布置雷,雷的个数可以在头文件中通过宏定义设置。
6、排查雷,在布置雷的数组里排查,如果是雷则打印被炸死,并退出游戏。如果不是雷,则统计雷的个数,是0则展开空白,不是0则将雷的个数传给排查雷的那个数组。
7、判断输赢,如果空格的总的个数于行和列的乘积减去布雷的个数,则表示排雷成功。
三、基本步骤:
1、显示菜单界面:
void menu()
{
printf("************ 1. play **********\n");
printf("************ 0. exit **********\n");
}
2、设计两个数组存放信息:
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
3、初始化棋盘:
void init_board(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
4、布置雷:
void set_mine(char mine[ROWS][COLS], int row, int col)
{
//布置10个雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
5、打印棋盘:
void display_board(char board[ROWS][COLS], int row, int col)
{
//列号
for (int j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
6、排雷并判断雷是否排查完:
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 - EASY_COUNT)
{
printf("请输入要排查雷的坐标:");
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_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}
四、总代码:
使用多文件实现,一个头文件game.h,两个源文件game.c和test.c。
1、game.h:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#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 30
//初始化棋盘
void init_board(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void display_board(char board[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);
2、game.c:
#include"game.h"
//初始化棋盘
void init_board(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//打印棋盘
void display_board(char board[ROWS][COLS], int row, int col)
{
//列号
for (int j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
//布置10个雷
int count = EASY_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 - EASY_COUNT)
{
printf("请输入要排查雷的坐标:");
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_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}
3、test.c:
#include"game.h"
void menu()
{
printf("************ 1. play **********\n");
printf("************ 0. exit **********\n");
}
void game()
{
//设计2个数组存放信息
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
//mine初始化为全‘0’
//show初始化为全‘*’
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
//布置雷
set_mine(mine, ROW, COL);
//排雷
display_board(show, 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;
}
边栏推荐
- Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
- 【论文阅读】Mastering the game of Go with deep neural networks and tree search
- type_traits metaprogramming library learning
- 【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
- IDEA常用快捷键与插件
- Safety 20220709
- 微信小程序使用云函数更新和添加云数据库嵌套数组元素
- [Swift]自定义点击APP图标弹出的快捷方式
- 数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开
- qlib自动化quant
猜你喜欢
"A daily practice, happy water problem" 1331. Array serial number conversion
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
BUG消灭者!!实用调试技巧超全整理
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
The third is the code to achieve
手把手实现图片预览插件(三)
重磅 | 基金会为白金、黄金、白银捐赠人授牌
Daily practice of LeetCode - 138. Copy a linked list with random pointers
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
随机推荐
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
BUG destroyer!!Practical debugging skills are super comprehensive
interprocess communication
Basic knowledge of mysql (2)
BP神经网络
Component pass value provide/inject
Pytest e-commerce project combat (on)
The third is the code to achieve
[AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
$parent/$children and ref
(tree) Last Common Ancestor (LCA)
The idea project obviously has dependencies, but the file is not displayed, Cannot resolve symbol 'XXX'
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
进程间通信
[C language] General method of base conversion