当前位置:网站首页>c语言基础篇:扫雷
c语言基础篇:扫雷
2022-07-13 16:58:00 【虎太郎的继承】
前言
本篇同样是对数组等内容的加深,希望大家也可以通过一些项目来加深对知识的把握。
注:小编使用的是VS2019.
一、设计思路
1、菜单界面选择开始或者退出游戏
2、棋盘初始化与展示
3、布置雷
4、排查雷
二、程序设计
1、菜单界面选择开始或者退出游戏
选择1开始游戏,选择0退出游戏,其他数字报错重新选择。
//菜单
void menu()
{
printf("******************************\n");
printf("********* 1.play *********\n");
printf("********* 0.exit *********\n");
printf("******************************\n");
}
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入:>\n");
break;
}
效果展示
2、棋盘初始化与展示
我们设计两个棋盘,一个存放雷(mine),一个用来存放排查出雷信息(show)。
定义一个变量set,mine初始化为’‘0’,show初始化为’*'。
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 <= row; 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");
}
效果展示
3、布置雷
使用rand函数随机生成雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
//1.生成随机下标(1,9)
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
效果展示
4、排查雷
玩家通过输入坐标来排查指定位置的雷,当坐标不再棋盘范围内需要重新输入。
由于排雷是一个循环的过程所以使用while循环,当win < row * col - EASY_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("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\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("坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
三、主函数与声明部分
主函数
int main()
{
int input = 0;
srand((unsigned)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;
}
声明部分
#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 menu();
//game函数
void game();
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//显示棋盘
void 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()函数
//游戏
void game()
{
char mine[ROWS][COLS] = {
0 };//用来布置雷的棋盘
char show[ROWS][COLS] = {
0 };//用来排查出雷信息的棋盘
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//显示棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
边栏推荐
- 数据仓库体系之贴源层、历史层
- (转载)PlantUML快速指南
- Blockbuster: released by domestic IDE, developed by Alibaba, completely open source (high performance + high customization)
- 年轻人开始“整顿”商场刺客
- 【回归预测-LSTM】基于attention机制的LSTM实现时间序列回归预测附matlab代码
- Technology sharing | common proxy tools for interface testing
- 【读书会第13期】第二章视频文件的封装格式和编码格式笔记
- 测试/开发程序员的薪资不平衡?忙碌生活各种跳槽......
- 深度学习神经网络的正向传播(一)
- 测试如何发挥更大价值?聊聊测试左移和测试右移
猜你喜欢

FLASH W74M12JWSSIQ_W25Q64FWZPIG规格,存储器

快速教你如何搭建数据驱动自动化测试框架?

被final修饰的变量到底能不能被修改
![[number recognition] handwritten number recognition based on knowledge base with matlab code](/img/06/6adab955a339f453249543baab1dc6.png)
[number recognition] handwritten number recognition based on knowledge base with matlab code

五屏、VR、投影,“蔚小理”在智能座舱上卷起来了
![error [XXX.zip]: start of central directory not found; zipfile corrup](/img/16/211bcd22c3ba7f50477e537a12ba37.png)
error [XXX.zip]: start of central directory not found; zipfile corrup

Expert, Alibaba Daniel quit and brought out the internal "high concurrency system design" learning manual
![[regression prediction LSTM] LSTM implementation based on attention mechanism regression prediction of time series with matlab code](/img/a1/2da8bebde1e788f4b5326b8c0fb2dd.png)
[regression prediction LSTM] LSTM implementation based on attention mechanism regression prediction of time series with matlab code

Detailed explanation and precautions of JDBC

An article takes you to know about state-owned enterprise programmers (super detailed)
随机推荐
Gdi+ high speed drawing transparent forms
这几款手机安全浏览器,好用不止一点点
【回归预测-LSTM】基于attention机制的LSTM实现时间序列回归预测附matlab代码
error [XXX.zip]: start of central directory not found; zipfile corrup
提示您与该网站的连接不是私密连接怎么办?
poj3617Best Cow Line
JDBC的详细讲解和注意事项
Continous Gesture Recognition with hand-orented spatiotemporal feature
手势方面论文列表
织梦内容图片添加A链接新窗口打开
enum类
从矩阵中查找数字是否存在
Why are you a programmer? Some people are poor, some people dream, but I am
fxksmdb.exe进程说明
tensorflow训练出的参数转化为caffe框架下的.caffemodel模型
如何关闭卡死的程序进程?
这个地图资源除了NB我不知道该说什么
数字化转型的定义、发展、应用和展望
技术分享 | 接口测试常用代理工具
FLASH W74M12JWSSIQ_ W25q64fwzpig specification, memory