如何用C语言写一款扫雷游戏,只需掌握如下知识即可尝试
分支循环语句、函数、数组
1. 扫雷游戏的分析与设计
1.1 扫雷游戏的功能说明
- 使用C语言编译扫雷游戏
- 游戏实现继续完和退出选项
- 扫雷的棋盘大小和雷的个数可改变
- 默认9*9棋盘 10个雷
- 如果选择位置不是雷,会显示周围有几个雷
- 如果选择位置是雷,就会炸死,游戏结束
- 把除十个雷之外所有非雷的位置找出来,排雷成功,游戏结束
1.2游戏界面
1.3游戏的分析与设计
1.3.1数据结构的分析
扫雷过程中,布置雷和排查雷的信息都需要储存,因为我们要再9*9的棋盘中布置雷和排查雷,首先想到的就是用9*9的 二维数组来存放
空棋盘
如果某个位置布置雷我们就存放1,没有雷就存放0
假设我们排查(2, 5),我们统计周围一圈8个黄色位置雷的个数是1
假设我们排查(8, 6),我们统计周围一圈8个黄色位置雷的个数,最下面三个坐标就会越界。
因此,为了防止越界,我们在设计的时候给数组扩大一圈,周围一圈不布置雷
接下来开始存放信息 我们创建两个二维数组mine和show,mine数组用来存放雷的信息,初始化字符为'0',布置雷改为'1',同时为了保持神秘,show数组初始化为字符'*'。
1.3.2文件结构设计
我们设计三个文件
test.c //测试写游戏的逻辑
game.c //游戏中的函数实现
game.h //游戏中需要的数据类型和函数声明
2.游戏的代码实现
2.1 test.c
#include "game.h"
void menu()
{
printf("************************\n");
printf("**** 1. play ****\n");
printf("**** 0. exit ****\n");
printf("************************\n");
}
void game()
{
srand((unsigned int)time(NULL));
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(mine, ROW, COL);
//DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//测试成功
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
DisplayBoard(mine, ROW, COL);
}
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");
}
} while (input);
return 0;
}
2.2 game.c
#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 <= row; 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");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EasyCount;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
//排查雷
int Sum(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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
if (mine[x][y] == '1')
{
printf("对不起,你踩到雷了\n游戏结束\n");
break;
}
else
{
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("已排查过雷,请重新输入\n");
}
else
{
show[x][y] = Sum(mine, x, y) + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - EasyCount)
{
printf("排雷完成,游戏胜利\n");
break;
}
}
}
2.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 ROW+2
#define EasyCount 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);