目标:1.使用控制台实现游戏
2.9*9棋盘(使用数组)
3.随机布置10个雷
4.排查雷: 显示周围雷;炸死游戏结束;10个雷找完游戏结束
5.分别使用“game.h”"game.c""test.c"分别实现游戏的函数声明、函数实现及主函数调用
思路:
1.打印9*9棋盘,未知处用字符‘*’代替,用‘1’表示雷,用‘0’表示非雷
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | * | * | * | * | * | * | * | * | * |
2 | * | * | * | * | * | * | * | * | * |
3 | * | * | * | * | * | * | * | * | * |
4 | * | * | * | * | * | * | * | * | * |
5 | * | * | * | * | * | * | * | * | * |
6 | * | * | * | * | * | * | * | * | * |
7 | * | * | * | * | * | * | * | * | * |
8 | * | * | * | * | * | * | * | * | * |
9 | * | * | * | * | * | * | * | * | * |
此棋盘因为游戏显示画面,但扫雷游戏中为了方便实现代码逻辑,需要一个原棋盘、布雷后的棋盘和用于游戏展示的棋盘,所以将在此命名:
viod InitBoard();//初始化棋盘全部为‘0’
void DisplayBoard();//打印棋盘
void SetMine();//布雷棋盘
void FindMine();//排雷棋盘
2.由于排雷游戏需要实现排除一个雷时统计周围的雷的个数这一功能,我们得使用将排雷坐标周围一圈的数字相加处理得到周围雷的个数。
此时要注意两点:字符‘0’‘1’不是简单的相加;当排除雷的坐标处于棋盘边缘时因为越界无法统计周围字符,所以棋盘得作出改动变成11*11格子棋盘实现排雷周围统计这一功能。
如下:黄色部分为棋盘,绿色部分为实现统计排除坐标统计周围雷这一功能实现而扩展数组,可以在外围加上行号和列号使其易观察一些:
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
1 | |||||||||||
2 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
3 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
4 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
5 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
6 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
7 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
8 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
9 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
10 | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ‘*’ | ||
11 |
3.由于满足后期扫雷游戏扩展难度的需要,我们需要变长数组但由于作者使用VS2022无法使用便创建一些变量来实现如下:
#define ROW 9//定义9*9棋盘的行
#define COL 9//定义9*9棋盘的列
#define ROWS ROW+2//定义11*11棋盘的行
#define COLS COL+2//定义11*11棋盘的列
4.同样为自定义埋雷的个数需要定义一个变量
#define EASY_COUNT 10//意为简单模式下雷的数量
Add:此处分享define的用法(已标明转载处)
#定义 标识符 内容
#define name stuff
#define MAX 100 // 将 MAX 这个标识符和 100 这个数字关联起来
#define REG register // 为 register 这个关键字,创建一个简短的名字REG
#define STR "test_string" // 用 STR 这样一个名字来代替 test_string 这样一个字符串
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_58165485/article/details/124146570
5.接下来实现代码
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("********** 1.play**********\n");
printf("********** 0.exit**********\n");
}
void game()//游戏本体
{
char mine[ROWS][COLS] = { 0 };//原棋盘且存放雷
char show[ROWS][COLS]={ 0 };//展示棋盘
InitBoard(mine, ROWS, COLS,'0');//初始化棋盘
InitBoard(show, ROWS, COLS,'*');//初始化棋盘
//DisplayBoard(mine, ROW, COL);//打印棋盘
SetMine(mine,ROW,COL);//在9*9棋盘上布置8个雷
//DisplayBoard(mine, ROW, COL);//布雷情况
DisplayBoard(show, ROW, COL);//打印棋盘
FindMine(mine, show, ROW,COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("扫雷游戏开始\n");
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
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 = 0;
printf("----------扫雷游戏---------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");//打印列号
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ",i);//打印行号
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void SetMine(char arr[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)//统计周围雷 ‘1’-49 ‘0’-48 ‘2’-50
{
return mine[x-1][y-1] + mine[x-1][y] + mine[x-1][y+1] + mine[x][y-1] + mine[x][y+1] + mine[x+1][y-1] + mine[x+1][y] + mine[x+1][y+1] - 8*'0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y, 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] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count= GetMineCount(mine,x,y);//统计周围雷
show[x][y] = count + '0';//存疑?
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("已排查过该坐标\n");
}
}
else
{
printf("重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
game.h
#pragma once
#define ROW 9
#define COL 9
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10//不要加‘;’
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.h中会方便的许多