在初阶学习C语言时,我们学习一段时间过后,可以通过写一个小游戏/小程序来提高我们写代码的能力,同时也为学习编程增添了一些乐趣,增加学习动力。
一.主函数
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;
}
二.menu()函数
打印菜单
void menu()
{
printf("\n****************************************\n");
printf("****** 1.play ********\n");
printf("****** 0.exit ********\n");
printf("****************************************\n");
}
三.game.h文件中的准备工作
在其它文件中引用我们自定义的头文件“game.h”即可
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MAX_COUNT 15
//初始化
void InitBoard(char board[ROWS][COLS], int row , int col,char c);
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void MakeBoard(char board[ROWS][COLS], int row, int col);
//排查雷
int FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
四.game()函数
1.初始化
(1)代码
void game()
{
char mineboard[ROWS][COLS] = { 0 };
char showboard[ROWS][COLS] = { 0 };
//初始化
//初始化两个盘,一个mineboard用来存放雷的位置
InitBoard(mineboard, ROWS, COLS, '0');
//showboard是给用户呈现的,让用户进行扫雷的操作
InitBoard(showboard, ROWS, COLS, '*');
//打印
//布置雷
//排查雷
}
//初始化
void InitBoard(char board[ROWS][COLS], int row, int col ,char c)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = c;
}
}
}
//将mineboard里的元素初始化成字符0
//将showboard里的元素初始化成字符*
将mineboard的元素也初始化为字符,与showboard的元素类型相同,是为了后续使用方便。
2.打印
(1)代码
void game()
{
char mineboard[ROWS][COLS] = { 0 };
char showboard[ROWS][COLS] = { 0 };
//初始化
InitBoard(mineboard, ROWS, COLS, '0');
InitBoard(showboard, ROWS, COLS, '*');
//打印
DisplayBoard(showboard, ROW, COL);
//布置雷
//排查雷
}
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 1;
int j = 1;
printf("************** 扫雷 *******************\n");
for (i = 0; i <= row; i++)
{
printf("%d ",i); //打印行号
}
printf("\n");
for (i = 1; i <= row; i++)
{
for (j = 0; j <= col; j++)
{
if (0 == j)
{
printf("%d ", i); //打印列号
}
else
{
printf("%c ", board[i][j]);
}
}
printf("\n");
}
}
(2)运行效果
3.布置雷
(1)代码
void game()
{
char mineboard[ROWS][COLS] = { 0 };
char showboard[ROWS][COLS] = { 0 };
//初始化
InitBoard(mineboard, ROWS, COLS, '0');
InitBoard(showboard, ROWS, COLS, '*');
//打印
DisplayBoard(showboard, ROW, COL);
//布置雷
MakeBoard(mineboard, ROW, COL);
//排查雷
//布置雷
void MakeBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = MAX_COUNT;
while(1)
{
//随机坐标
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
//当坐标处未被埋雷时,成功埋雷,count--
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
//当埋雷达到预定数字时,跳出循环
if (0 == count)
{
break;
}
}
}
(2)运行示例
4.排查雷
(1)代码
void game()
{
char mineboard[ROWS][COLS] = { 0 };
char showboard[ROWS][COLS] = { 0 };
//初始化
InitBoard(mineboard, ROWS, COLS, '0');
InitBoard(showboard, ROWS, COLS, '*');
//打印
DisplayBoard(showboard, ROW, COL);
//布置雷
MakeBoard(mineboard, ROW, COL);
DisplayBoard(mineboard, ROW, COL);
//排查雷
//使用ret来接收FindBoard的返回值
int ret = FindBoard(mineboard, showboard, ROW, COL);
if (ret == 0)
printf("\n恭喜你!扫雷成功!\n");
}
void Menu()
{
printf("\n 1 . 排查坐标 \n");
printf("\n 2 . 标记雷 \n");
printf("\n 3 . 取消标记雷 \n");
}
//排查雷
int FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int count = MAX_COUNT; //count是剩余未被标记的雷的个数
while (1)
{
Menu();
printf("请输入选择: ");
int input = 0;
scanf("%d", &input);
switch (input)
{
case 1:
{
printf("请输入需要排查的坐标: ");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
int count = 0;
//确认输入的坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//确认所要排查的坐标是否未被排查过
if (show[x][y] == '*')
{
//当所要排查的坐标为'1'时,踩到雷,游戏结束
if (mine[x][y] == '1')
{
printf("\n很遗憾,踩到雷,游戏结束!\n");
printf("\n雷的位置如下:\n");
DisplayBoard(mine, ROW, COL);
return 1;
}
//当所要排查的坐标为'0'时,计算它周围的八个坐标的雷个数之和
else if (mine[x][y] == '0')
{
count = 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';
show[x][y] = count + '0';
//定义一个BlankBoard函数,来对应所要排查的坐标以及它周围的八个坐标都为'0'的情况
//这种情况下直接展开多个坐标,显示为' '
BlankBoard(mine, show, x, y);
//一次排查结束后,打印一下,让用户知道排查后的情况
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("已排查,请重新输入\n");
}
}
else
{
printf("输入错误,请重新输入!\n");
}
}
break;
case 2:
//实现标记雷的功能
{
printf("请输入需要标记的坐标: ");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (show[x][y] == '*')
{
//将所选坐标标记为'#'
show[x][y] = '#';
DisplayBoard(show, ROW, COL);
//如果用户标记的坐标是雷,则剩余未被标记的雷的个数-1
if (mine[x][y] == '1')
{
count--;
}
}
else
{
printf("输入错误!\n");
}
}
break;
case 3:
//实现取消标记的功能
{
printf("请输入需要取消标记的坐标: ");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (show[x][y] == '#')
{
show[x][y] = '*';
DisplayBoard(show, ROW, COL);
}
else
{
printf("输入错误!\n");
}
}
break;
default:
printf("输入错误,请重新输入\n");
break;
}
if (count == 0)
break;
}
return count;
}
//若无雷,展开空白
//此功能也可不写
void BlankBoard(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
if ('0' == show[x][y])
{
show[x][y] = ' ';
int i = x;
int j = y;
while (mine[i][j] == '0')
{
j = y;
if (show[i][j] == '*')
show[i][j] = ' ';
while ((show[i][j + 1] == '*') && (mine[i][j + 1] == '0'))
show[i][j + 1] = ' ';
j = y;
while ((show[i][j - 1] == '*') && (mine[i][j - 1] == '0'))
show[i][j - 1] = ' ';
if (mine[i][j] != '0')
break;
i--;
}
i = x;
j = y;
while (mine[i][j] == '0')
{
i = x;
if (show[i][j] == '*')
show[i][j] = ' ';
while ((show[i + 1][j] == '*')&&(mine[i+1][j] == '0'))
show[i + 1][j] = ' ';
i = x;
while (show[i - 1][j] == '*' && mine[i - 1][j] == '0')
show[i - 1][j] = ' ';
if (mine[i][j] != '0')
break;
j--;
}
i = x;
j = y;
while (mine[i][j] == '0')
{
if (show[i][j] == '*')
show[i][j] = ' ';
while (show[i][j - 1] == '*' && mine[i][j - 1] == '0')
show[i][j - 1] = ' ';
j = y;
while ((show[i][j + 1] == '*') && (mine[i][j + 1] == '0'))
show[i][j + 1] = ' ';
if (mine[i][j] != '0')
break;
i++;
}
i = x;
j = y;
while (mine[i][j] == '0')
{
if(show[i][j] == '*')
show[i][j] = ' ';
while (show[i - 1][j] == '*' && mine[i - 1][j] == '0')
show[i - 1][j] = ' ';
i = x;
while ((show[i + 1][j] == '*') && (mine[i + 1][j] == '0'))
show[i + 1][j] = ' ';
if (mine[i][j] != '0')
break;
j++;
}
}
}
五.运行示例
六.补充说明
除了标记雷、排查雷等功能,我们还可以给它增加计时、显示剩余雷数(总雷数减去标记的数量,不管是否标记正确),我们甚至可以给它增加一个功能:规定时间内未扫雷成功即关机。。。。。。
总之,我们想增加功能,无非就是在地基上盖房子,不断添砖加瓦。同时,房子的样式自然也是各不相同的,你可以实现这些功能,也可以实现那些功能,一切取决于你自己。