我今天给大家带来c语言如何实现扫雷游戏,废话不多说,直接上代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"
//菜单函数
void meau() {
printf("*******************************\n");
printf("*******< 按1开始游戏 >*******\n");
printf("*******< 按0退出游戏 >*******\n");
printf("*******************************\n");
}
//游戏函数
void game() {
char mine[Rows][Cols];//布置雷的棋盘
char show[Rows][Cols];//展示雷的棋盘
//初始化棋盘
InitBoard(mine, Rows, Cols, '0');
InitBoard(show, Rows, Cols, '*');
//打印棋盘
DisplayBoard(show, Row, Col);
//DisplayBoard(mine, Row, Col);
//布置地雷
Setmine(mine, Row, Col);
//DisplayBoard(mine, Row, Col);
//排除地雷
Findmine(mine,show, Rows, Cols);
}
//测试选项函数
void test() {
int input = 0;
srand((unsigned int)time(NULL));//生成随机值
do {
meau();
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("游戏结束,退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
}
int main() {
test();
return 0;
}
此页为程序的主程序test.c
这是我们这个游戏的主程序,我们设置了菜单函数,游戏函数,测试选项函数。
当玩家开始运行此程序时,映入眼帘的先是菜单函数,当玩家按照提示输入值后,就会进入游戏函数或者退出游戏。
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//设置棋盘大小为9*9
#define Row 9
#define Col 9
//为了防止越界,棋盘扩大一圈为11*11
#define Rows Row+2
#define Cols Col+2
//设置棋盘有十个雷
#define EASY_COUNT 10
//初始化棋盘声明
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.h
此页我们用于对我们要使用函数的声明,对库函数头文件的声明和定义参数
#define _CRT_SECURE_NO_WARNINGS 1
#include"test.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;
printf("----------扫雷游戏----------\n");
for (i = 0; i <= col; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++) {
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++) {
printf("%c ",board[i][j]);
}
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 GetMinecount(char mine[Rows][Cols], int x, int y) {
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1]
+ mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + 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("请输入你要排查的雷\n");
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);
}
}
此页为游戏实现的代码game.c
程序运行时的样子
以上就是扫雷游戏的程序啦,有什么问题可以提出来多多改正。