当前位置:网站首页>排雷小游戏(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);
边栏推荐
- flex layout (flexible layout)
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- 无代码生产新模式探索
- 双重for循环案例(用js打印九九乘法表)
- Smart people's game improvement: Chapter 3, Lesson 2: "Number of Tongtong" (number)
- Redis(十一) - 异步优化秒杀
- C语言入门实战(13):十进制数转二进制
- H5 access payment process - WeChat payment & Alipay payment
- leetcode一步解决链表合并问题
- Guarantee WIFI security in home and enterprise-with AC and AP networking experiment
猜你喜欢
随机推荐
Stress testing and performance analysis of node projects
leetcode括号匹配问题——32.最长有效括号
面试官:设计“抖音”直播功能测试用例吧
There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?
机器学习——支持向量机原理
使用TinkerPop框架对GDB增删改查
51单片机外设篇:点阵式LCD
Important concepts of target detection - IOU, receptive field, hole convolution, mAP
The virtual reality real estate display system foresees the future decoration effect in advance
kubernetes affinity, anti-affinity, taint, tolerance
Use the browser's local storage to realize the function of remembering the user name
coredns介绍
[OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
21 Day Learning Challenge Schedule
软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
驱动页面性能优化的3个有效策略
golang generics
A list of 300+ learning resources compiled by senior engineers of the Tao Department (the latest version in 2021)
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you









