当前位置:网站首页>排雷小游戏(C语言)
排雷小游戏(C语言)
2022-08-02 05:18:00 【果辰辰果】
游戏前言准备
1.使用二维数组初始化扫雷的游戏布局,通过改变数组的行列来改变,用ROW来规定数组行数,COL来规定数组列数,扫雷需要用到两个二维数组,一个数组用来存放雷的信息,一个数组用来存放显示界面信息,因为害怕边界越界所以使用ROW+2和COL+2的数组避免越界;
2.使用二维数组显示扫雷游戏界面,除了显示数组信息外,还需要在第一行和第一列显示坐标便于玩家确定排雷时的坐标;
3.设置雷区,根据游戏难度来随机放置固定个数的雷,使用#define定义好随时改变值以达到不同的雷的个数;
4.排雷,每当玩家选择的这个坐标没雷时,则计算这个坐标周围一圈八个格子的雷的个数。
5.判断输赢,如果玩家踩到雷,则直接游戏结束,若未排的区域个数等于雷的个数且玩家没被炸死,则玩家赢。
一、建立游戏菜单
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--;
}
}
}
五、排查雷并判断是否踩雷
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 };//设计数组存放信息
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);
边栏推荐
- Different ways of shell scripting
- What is the most important ability of a programmer?
- The advantages of making web3d dynamic product display
- There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?
- Leetcode parentheses matching problem -- 32. The longest parentheses effectively
- 回文串求解的进阶方法
- 保证家里和企业中的WIFI安全-附AC与AP组网实验
- The virtual reality real estate display system foresees the future decoration effect in advance
- 51单片机外设篇:点阵式LCD
- CPU使用率和负载区别及分析
猜你喜欢

Features and installation of non-relational database MongoDB

Linux CentOS8安装Redis6

【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台

C语言基础知识梳理总结:零基础入门请看这一篇

Packaging and deployment of go projects

leetcode一步解决链表反转问题

Polar Parametrization for Vision-based Surround-View 3D Detection Paper Notes

18 years of programmer career, read more than 200 programming books, pick out some essence to share with you

Double for loop case (use js jiujiu printing multiplication table)

eggjs controller层调用controller层解决方案
随机推荐
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
leetcode括号匹配问题——32.最长有效括号
软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
淘系资深工程师整理的300+项学习资源清单(2021最新版)
leetcode一步解决链表反转问题
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
Point Density-Aware Voxels for LiDAR 3D Object Detection 论文笔记
PIL与numpy格式之间的转换
路由规划中级篇
51单片机外设篇:DS18B20
go里面的基本知识
CPU使用率和负载区别及分析
Different ways of shell scripting
洛谷小游戏大全(用洛谷的人都得知道)
Point Density-Aware Voxels for LiDAR 3D Object Detection Paper Notes
DNS的解析流程
[OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
C语言入门实战(13):十进制数转二进制
C# 编码规范手册