1.扫雷游戏的功能说明
使用控制台实现经典的扫雷游戏
游戏可以通过菜单实现继续玩或者退出游戏
扫雷的棋盘是9*9的格子
默认随机布置10个雷
可以排查雷
如果位置不是雷,就显示周围有几个雷
如果位置是雷,就炸死游戏结束
把除10个雷之外的所有雷都找出来,排雷成功,游戏结束
2游戏的分析和设计
数据结构分析
扫雷过程中,布置的雷和排出的雷的信息都需要存储,所以我们需要一定的数据结构来存储这些信息。
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
0 | |||||||||
1 | |||||||||
2 | |||||||||
3 | |||||||||
4 | |||||||||
5 | |||||||||
6 | |||||||||
7 | |||||||||
8 |
那如果这个位置布置雷,我们就存放1,没有布置雷就存放0.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
6 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
7 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |
8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
test.c--完成对扫雷游戏的测试
game.c--游戏模块
game.h--游戏模块
//用menu()做菜单
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;
}
//存放数据的类型太多,容易产生歧义
//用两个数组,一个存储雷的信息,一个存放排查出的雷的信息
char show[11][11];//初始化为‘*’
char mine[11][11];//放雷和非雷
雷--‘1’
非雷--‘0’
//在统计一个坐标周围的雷的个数的时候,可能会越界
//那么就在周围加一圈棋盘,变成11*11
将行和列改用符号表示更方便后面的更改
char mine[ROWS][COLS];
char show[ROWS][COLS];
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
初始化棋盘
Initboard(mine, ROWS, COLS,'0');//'0'
Initboard(show, ROWS, COLS,'*');//'*'
这里别忘了函数的声明
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);
初始化棋盘的代码实现
void Initboard(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
//行
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
下面是关于打印棋盘的代码
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
DisplayBoard(mine,ROW,COL);//显示雷的位置
DisplayBoard(show, ROW, COL);
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 1;
printf("**********扫雷**********\n");
//先打引列号
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 0; j < col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
//布置雷
SetMine(mine, ROW, COL);
void SetMine(char arr[ROWS][COLS], int row, int col);
void SetMine(char arr[ROWS][COLS], int row, int col)
{
//布置十个雷
int count = 10;
while (count)
{
//布置雷
int x = rand()%row+1;//1~9
int y = rand()%col+1;//1~9
//布置成功一个雷,count--
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
排查雷
FindMine(mine, show, ROW, COL);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
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 - 10)
{
printf("请输入要排查的坐标:");
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 n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - 10)
{
printf("恭喜你,扫雷成功\n");
DisplayBoard(show, ROW, COL);
}
}
总结
以下为game.h
#pragma once
#include"game.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//初始化棋盘
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void Initboard(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
//行
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 1;
printf("**********扫雷**********\n");
//先打引列号
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 0; j < col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void SetMine(char arr[ROWS][COLS], int row, int col)
{
//布置十个雷
int count = 10;
while (count)
{
//布置雷
int x = rand()%row+1;//1~9
int y = rand()%col+1;//1~9
//布置成功一个雷,count--
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
static 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';
}
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 - 10)
{
printf("请输入要排查的坐标:");
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 n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - 10)
{
printf("恭喜你,扫雷成功\n");
DisplayBoard(show, ROW, COL);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void menu()
{
printf("**********************\n");
printf("**** 1.play ****\n");
printf("**** 0.exit ****\n");
printf("**********************\n");
}
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
//初始化棋盘
Initboard(mine, ROWS, COLS,'0');//'0'
Initboard(show, ROWS, COLS,'*');//'*'
//打印棋盘
//DisplayBoard(mine,ROW,COL);//显示雷的位置
//DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);//显示雷的位置
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
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;
}