c语言实现简单扫雷游戏
游戏介绍
布置一个棋盘,在棋盘中点击一个地区可以显示出周围八个坐标有多少个雷
当选择到有雷的地方则失败,游戏结束
当把所有的雷都排查出来则成功,游戏结束
此处我们选择设置一个9行9列的棋盘,设置十个雷
—————————————————————————————————
菜单及主函数实现
void menu()
{
printf("**************************\n");
printf("******* 1.play *********\n");
printf("******* 0.exit *********\n");
printf("**************************\n");
}
int main()
{
int input = 0;
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;
}
—————————————————————————————————
游戏代码实现
设置棋盘
此处需要设置两个棋盘
一个棋盘初始化为全0,用来放置雷,雷设置为1
另一个棋盘初始化为全*,用来显示排查雷的信息
此外,由于在棋盘边缘得不到八个坐标的值,所以实际上需要创建11行11列的棋盘
在头文件中定义棋盘大小
#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)
{
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)
//传参时传递的是整个11*11的数组所以此处形参需要用ROWS和COLS
{
int i = 0;
int j = 0;
printf("-------扫雷游戏-----\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
for (i = 1; i <= row; i++)//只显示9行9列,此时i和j从1开始
{
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)
{
int count = EASY_COUNT;//布置雷10个 count=10
while (count)
{
int x = rand() % row + 1;//0到8加1变成1到9
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
此处使用到随机数,还需要设置随机数生成起点
srand((unsigned int)time(NULL));
头文件中定义如下
#include <time.h>
#include <stdlib.h>
#define EASY_COUNT 10
此时棋盘显示如下
排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//排查雷
{
int x = 0;
int y = 0;
while (1)
{
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 = get_mine_count(mine, x, y);
show[x][y] = count + '0';//数字转换成字符加上一个字符0
Displayboard(show, ROW, COL);
}
}
else
{
printf("输入的地址非法,请重新输入\n");
}
}
}
此时还需要一个函数统计选择的坐标周围有几个雷
int get_mine_count(char board[ROWS][COLS], int x, int y)//周围的8个字符加起来减去八个0的ASC值为有几个字符1
{
return
(
board[x - 1][y] +
board[x - 1][y - 1] +
board[x][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0'
);
}
设置赢得比赛的条件
我们通过修改上一步的while循环来实现设置比赛的获胜条件
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 (show[x][y] != '*')
{
printf("该坐标被排查过了,不能重复排查\n");
}
else
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
Displayboard(mine, ROW, COL);
break;
}
else//不是雷---->统计周围八个坐标有几个雷
{
win++;
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//数字转换成字符加上一个字符0
Displayboard(show, ROW, COL);
}
}
else
{
printf("输入的地址非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
——————————————————简易扫雷完成———————————————————
完整代码如下
头文件
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#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);//排查雷
int get_mine_count(char board[ROWS][COLS], int x, int y);//排查周围多少雷
源文件1
#include"game.h"
void menu()
{
printf("**************************\n");
printf("******* 1.play *********\n");
printf("******* 0.exit *********\n");
printf("**************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化数组的内容
//mine数组没有放置雷的时候都是‘0’
//show数组没有排查雷的时候都是‘*’
InitBoard(mine, ROWS, COLS,'0');//初始化函数
InitBoard(show, ROWS, COLS,'*');//初始化函数
SetMine(mine, ROW, COL);//放置雷
FindMine(mine, show, ROW, COL);//排查雷
//Displayboard(mine, ROW, COL);//一般不会打印出来mine棋盘
Displayboard(show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
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;
}
源文件2(游戏主体)
#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)
//传参时传递的是整个11*11的数组所以此处形参需要用ROWS和COLS
{
int i = 0;
int j = 0;
printf("--------------------扫雷游戏---------------------\n");
for(j=0 ; j<=col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)//只显示9行9列,此时i和j从1开始
{
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)
{
int count = EASY_COUNT;//布置雷10个 count=10
while (count)
{
int x = rand() % row + 1;//0到8加1变成1到9
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)//周围的8个字符加起来减去八个0的ASC值为有几个字符1
{
return
(
board[x - 1][y] +
board[x - 1][y - 1] +
board[x][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[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 (show[x][y] != '*')
{
printf("该坐标被排查过了,不能重复排查\n");
}
else
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
Displayboard(mine, ROW, COL);
break;
}
else//不是雷---->统计周围八个坐标有几个雷
{
win++;
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//数字转换成字符加上一个字符0
Displayboard(show, ROW, COL);
}
}
else
{
printf("输入的地址非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}