效果展示
开始的界面
输入0结束程序
输入1开始游戏
选择标记地雷或者选择踩坐标
输入0标记地雷模式
输入坐标
输入1踩坐标模式
输入坐标
在输入坐标处输入0 0结束游戏
踩到炸弹,出现炸弹位置
(1表示炸弹的位置,0表示没有炸弹的位置)
输入0结束程序
输入1重新开始游戏
胜利
输入0结束程序
输入1重新开始游戏
代码
我创建了两个.c源文件,一个.h头文件
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
int main()
{
int exi = 0;
srand((unsigned int)time(NULL));
board();
printf("请输入是否开始游戏:>");
scanf("%d", &exi);
do
{
switch (exi)
{
case 1:
{
game();
printf("是否输入1重新开始游戏:>");
scanf("%d", &exi);
if (exi == 0)
{
printf("游戏结束");
}
break;
}
case 0:
{
printf("游戏结束");
break;
}
default:
{
printf("输入错误,请重新输入:>");
scanf("%d", &exi);
if (exi == 0)
{
printf("游戏结束\n");
}
break;
}
}
} while (exi);
return 0;
}
game.h
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define WID 9
#define LON 9
#define WIDS WID+2
#define LONS LON+2
#define RAN 5
void board();
//打印开始的面板
void game();
//游戏运行的起点
void initialization(char mane[WIDS][LONS], char siz, int x, int y);
//把数组内框初始化为siz
void display(char mane[WIDS][LONS], int x, int y);
//打印数组内框的字符
void random(char mane[WIDS][LONS], int count);
//在数组中随机赋予count个炸弹
int look(char mane[WIDS][LONS], int x, int y);
//计算mane数组x,y位置周围有多少炸弹
void judge(char mane[WIDS][LONS], char show[WIDS][LONS],char include[WIDS][LONS]);
//判断输入是否获得胜利
void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS], int X, int Y);
//判断周围没有雷,会向外继续推,直到出现雷
void change(char show[WIDS][LONS], int x, int y, char siz);
//改变数组show位置(x,y)为字符siz
void jishu();
//统计选择了几次的位置,包括类推的位置,实现一点出现一大片的功能
game扫雷.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
static int a = 0;
void board()
{
printf("****************************\n");
printf("****************************\n");
printf("********* 1.play **********\n");
printf("********* 0.exit **********\n");
printf("****************************\n");
printf("****************************\n");
}
//数组初始化
void initialization(char mane[WIDS][LONS], char siz, int x, int y)
{
int i = 0;
for (i = 0; i <= x+1; i++)
{
int j = 0;
for (j = 0; j <= y+1; j++)
{
mane[i][j] = siz;
}
}
}
//打印第一个面板
void display(char mane[WIDS][LONS], int x,int y)
{
int i = 0;
int j = 0;
printf("-----------扫雷-----------\n");
printf("0 | ");
for (j = 1; j <= y; j++)
{
printf("%d ",j);
}
printf("\n");
printf("- - -");
for (j = 1; j <= y; j++)
{
printf(" -");
}
for (i = 1; i <= x; i++)
{
printf("\n");
printf("%d | ",i);
for (j = 1; j <= y; j++)
{
printf("%c ", mane[i][j]);
}
}
printf("\n-----------扫雷-----------\n");
}
void random(char mane[WIDS][LONS],int count)
{
int x = 0;
int y = 0;
while (count)
{
x = rand() % WID + 1;
y = rand() % LON + 1;
if (mane[x][y] == '0')
{
mane[x][y] = '1';
count--;
}
}
}
int look(char mane[WIDS][LONS],int x,int y)
{
return mane[x][y + 1] +
mane[x][y - 1] +
mane[x - 1][y + 1] +
mane[x - 1][y - 1] +
mane[x + 1][y + 1] +
mane[x + 1][y - 1] +
mane[x - 1][y] +
mane[x + 1][y]-8*'0';
}
void jishu()
{
a++;
}
void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS],int X,int Y)
{
if (include[X][Y] != '1')
{
int count = 0;
count = look(mane, X, Y);
show[X][Y] = count + '0';
include[X][Y] = '1';
jishu();
if (count == 0)
{
xunhuan(mane, show, include, X + 1, Y + 1);
xunhuan(mane, show, include, X - 1, Y - 1);
xunhuan(mane, show, include, X + 1, Y);
xunhuan(mane, show, include, X - 1, Y);
xunhuan(mane, show, include, X, Y + 1);
xunhuan(mane, show, include, X, Y - 1);
xunhuan(mane, show, include, X + 1, Y - 1);
xunhuan(mane, show, include, X - 1, Y + 1);
}
}
}
void change(char show[WIDS][LONS], int x, int y,char siz)
{
show[x][y] = siz;
}
void judge(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS])
{
int X = 0;
int Y = 0;
display(show, WID, LON);
do
{
int num = a;
if (num == WID * LON - RAN)
{
printf("恭喜你获得胜利!\n\n");
display(mane, WID, LON);
break;
}
printf("想要标记地雷就输入0,想要选择就输入1):>");
int choose = 0;
scanf("%d", &choose);
printf("\n");
if (choose==1)
{
printf("输入0 0结束游戏\n");
printf("请输入你选择的坐标:>");
scanf("%d%d", &X, &Y);
if (X == 0 && Y == 0)
{
printf("\n结束此次游戏\n\n");
break;
}
if (X >= 1 && X <= 9 && Y >= 1 && Y <= 9)
{
if (mane[X][Y] == '1')
{
printf("\n你吃到炸弹啦,死翘翘了\n\n");
display(mane, WID, LON);
break;
}
else
{
xunhuan(mane, show, include, X, Y);
display(show, WID, LON);
//display(mane, WID, LON);
}
}
else
{
printf("\n你输的超过范围啦,");
}
}
else
{
printf("\n输入0 0结束游戏\n");
printf("请输入你选择的坐标:>");
scanf("%d%d", &X, &Y);
if (X == 0 && Y == 0)
{
printf("\n结束此次游戏\n\n");
break;
}
change(show,X,Y,'F');
display(show, WID, LON);
}
} while (1);
}
void chu(char mane[WIDS][LONS], char siz,int x, int y)
{
int i = 0;
for (i = 1; i <= x ; i++)
{
int j = 0;
for (j = 1; j <= y ; j++)
{
mane[i][j] = siz;
}
}
}
void game()
{
char mane[WIDS][LONS];
char show[WIDS][LONS];
char include[WIDS][LONS];
initialization(mane, '0', WID, LON);
initialization(show, '*', WID, LON);
initialization(include, '1', WID, LON);
chu(include, '0', WID, LON);
random(mane,RAN);
//display(mane, WID, LON);
//display(show, WID, LON);
judge(mane,show,include);
}
我写的这个小游戏还很粗糙,不过才开始学,进步空间还是很大的,代码就上传到gitee了
以上就是基于C语言实现简单的扫雷游戏的详细内容,更多关于C语言 扫雷的资料请关注编程网其它相关文章!