c语言扫雷游戏源码,C语言扫雷游戏的实现代码

本文详细介绍了如何使用C语言实现一个简单的扫雷游戏,包括初始化棋盘、随机布雷、查看棋盘和排查雷区的代码示例。通过游戏流程展示编程逻辑,适合初学者学习C语言编程和游戏开发基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文实例为大家分享了C语言扫雷游戏的具体代码,供大家参考,具体内容如下

扫雷游戏的实现

1.game.h模块

代码实现如下:

#define _CRT_SECURE_NO_WARNINGS 1

#include

#include

#include

#define EASY_COUNT 10

#define ROW 9

#define COL 9

#define ROWS ROW+2

#define COLS COL+2

//初始化

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 mine[ROWS][COLS], int row, int col);

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col);

//一定的编程逻辑+

//数组-二维数组

//函数 - 组合在一起 - 功能

//循环-变量

2.game.c块:

代码实现如下:

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.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;

int j = 0;

printf("----------------------------------\n");

//列号的打印

for (i = 0; i <= col; i++)

{

printf("%d ", i);

}

printf("\n");

for (i = 1; i <= row; i++)

{

//每一行最前面的行号

printf("%d ", i);

for (j = 1; j <= col; j++)

{

printf("%c ", board[i][j]);

}

printf("\n");

}

printf("----------------------------------\n");

}

void SetMine(char board[ROWS][COLS], int row, int col)

{

//1. 随机找坐标布置雷

//布置多少个雷 - 10

int count = EASY_COUNT;

while (count)

{

//布置成功一个雷,count--

//1. 生产随机的坐标

int x = rand()%row+1;//1-9

int y = rand()%col+1;//1-9

//2. 布置

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] +

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';

}

//

//扫雷游戏是怎么结束的?

//1. 炸死了

//2. 正常排查了所有不是雷的位置

//

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

{

printf("请输入要排查的坐标:>");

scanf("%d%d", &x, &y);

if (x >= 1 && x <= row && y >= 1 && y <= col)

{

//判断x,y坐标处是否是雷

if (mine[x][y] == '1')

{

printf("很遗憾,你被炸死了\n");

DisplayBoard(mine, row, col);

break;

}

else

{

//如果x,y坐标不是雷,就统计周围有几个雷

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);

}

}

3.test.c模块

代码实现如下:

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void menu()

{

printf("**********************\n");

printf("**** 1. play ***\n");

printf("**** 0. exit ***\n");

printf("**********************\n");

}

void game()

{

//真正扫雷的过程

//创建2个数组

//存放布置好的雷

char mine[ROWS][COLS] = {0};//'0'

//存放排查出来的雷的信息

char show[ROWS][COLS] = {0};//'*'

InitBoard(mine, ROWS, COLS, '0');

InitBoard(show, ROWS, COLS, '*');

DisplayBoard(show, ROW, COL);

//布置好的雷的信息不应该轻易打印

//DisplayBoard(mine, ROW, COL);

//1. 布置雷

SetMine(mine, ROW, COL);

DisplayBoard(mine, ROW, COL);

//2. 扫雷

FindMine(mine, show, ROW, COL);

}

int main()

{

int input = 0;

//time_t -- 整形

srand((unsigned int)time(NULL));//设置随机数的生成起点的

do

{

menu();

printf("请选择:>");

scanf("%d", &input);

switch (input)

{

case 1:

game();//扫雷游戏

break;

case 0:

printf("退出游戏\n");

break;

default:

printf("选择错误!\n");

break;

}

//

} while (input);

return 0;

}

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值