扫雷游戏总体来说较复杂,需要写较多函数,我们分三个文件来完成,分别是game.h,game.c,test.c。game.h主要是函数的声明,game.c主要写函数,test.c用来实现程序的主体。
在此处添加
完整代码在文章末尾。
程序首先用menu函数打印一个菜单,这个是写在test.c文件的。
void menu()
{
printf("***** 1. play ****\n");
printf("***** 0. exit ****\n");
}
以下是全部main函数的代码,剩下的其实基本其实函数。
int main()
{
menu();
int input;
srand((unsigned int)time(NULL));
do
{
printf("请选择");
scanf("%d", &input);
switch (input)
{
case 1:
{
game();
break;
}
case 0:
{
printf("退出游戏");
break;
}
default :
{
printf("输入错误,重新输入");
break;
}
}
} while (input);
return 0;
}
srand((unsigned int)time(NULL));用来对程序后面的rand生成随机数,后面会讲到。
然后用do~while实现程序的循环结构,功能主要是在不退出程序的情况下进行多次游戏。
然后是switch,主要用来处理菜单命令。当输入1时,会进入游戏函数,这个函数是整个程序的硬骨头。
先看一下game函数吧,这个也是写在test.c文件的。你会看到里面同样是一堆函数。我们首先需要定义两个字符数组,一个是用来存放雷的信息,一个是用来实现展示给玩家的“棋盘”。而ROWS和COLS的数量在定义时要比我们需要的棋盘格数大2.
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
initboard(mine, ROWS, COLS,'0');
initboard(show, ROWS, COLS, '*');
disboard(show, ROWS, COLS);
setline(mine, ROW, COL);
//disboard(mine, ROWS, COLS);
ppmine(mine,show,ROWS, COLS);
disboard(show, ROWS, COLS);
}
比如这个图是9*9的格子,ROWS和COLS定义时就要定义为11。这是因为我们需要计算选中的格子周围有几个雷,而在边界的格子,如果不定义为11,用函数进行计算时会相当麻烦,因为我们用的是数组进行计数,这样会多出几种不同情况的函数。
至于ROWS和COLS是在game.h中进行的宏定义。#pragma once具体功能自行查阅吧。主要是这个文件只引用一次的意思。
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 4
#define COL 4
#define ROWS ROW+2
#define COLS COL+2
下面我们就需要实现初始数组的函数了,这个就是initboard函数。 需要在game.h中进行声明,在game.c中进行编写,
void initboard(char board[ROWS][COLS], int rows, int cols, char d)
{
int i;
int k;
for( i=0;i<rows;i++)
{
for ( k = 0;k < cols ;k++)
{
board[i][k] = d;
}
}
}
注意rows和cols是小写,不能是大写,后面要有字符变量,来实现对不同数组进行不同字符的初始化。 对存放雷的mine函数要赋为'0',对于show函数则可以赋为‘*’。此时初始化的数组范围是整个数组
game.h中是这么声明的。
void initboard(char board[ROWS][COLS], int rows, int cols, char d);
test.c中像这样使用函数。
initboard(mine, ROWS, COLS,'0');
initboard(show, ROWS, COLS, '*');
下面需要实现打印棋盘的函数disboard
void disboard(char board[ROWS][COLS], int rows, int cols)
{
printf("-----扫雷游戏-----\n");
for (int i = 0;i <= rows-2;i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1;i <rows - 1;i++)
{
printf("%d ", i);
for (int k = 1;k < cols - 1;k++)
{
printf("%c ", board[i][k]);
}
printf("\n");
}
}
首先打印一个分割线,然后在棋盘上打印序号 ,如图。
然后函数就需要打印棋盘了,这时候打印的字符格式就是要呈现给玩家的格式,行和列的数量就是定义数组大小减2 。函数的声明和上个函数相同。
test.c文件中这样使用。
disboard(show, ROWS, COLS);
下面是一个设置雷的函数,encount就是定义的雷的个数,在game.h中进行的宏定义。随机数生成的数模上定义的数组大小加上1对应的就是棋盘在数组中的位置,对这个位置的数组赋值‘1’,就是对棋盘中随机布置雷。函数上面同上。
void setline(char board[ROWS][COLS], int rows, int cols)
{
int count = eacount;
while (count)
{
int x = rand() % rows + 1;
int y = rand() % cols + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
下面就是玩家实现游戏操控的函数,是一个排查雷的函数。
void ppmine(char board[ROWS][COLS],char show[ROWS][COLS], int rows, int cols)
{
int count = (rows-2) * (cols-2) - eacount;
while (count)
{
int x, y;
printf("请输入坐标,中间有一个空格");
scanf("%d %d", &x, &y);
if (x<1 || x>(rows - 2) || y<1 || y>(cols - 2))
{
printf("输入错误\n");
continue;
}
if (board[x][y] == '1')
{
printf("你被炸死了\n");
disboard(board, ROWS, COLS);
return;
}
if (show[x][y] != '*')
{
printf("此位置已经输入\n");
continue;
}
else
{
int w;
w = ccount(board, x, y);
show[x][y] = w+'0';
}
disboard(show, ROWS, COLS);
count--;
}
if (count == 0)
{
printf("你赢了\n");
disboard(board, ROWS, COLS);
}
}
int count = (rows-2) * (cols-2) - eacount;计算没有放雷的格子数,配合while循环,每成功输入一次并且没有踩雷的话,count减1,当count为零时,玩家胜利。当玩家输入格子没在规定范围时,对玩家进行提示,当玩家输入的格子是雷时,打印棋盘,及布置的雷的情况,并结束游戏。如果此位置已经输入,这提示已经输入。然后else里面需要一个计算格子旁白有多少个雷的函数ccount。
int ccount(char board[ROWS][COLS], int x, int y)
{
return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1]
+ board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y]
+ board[x + 1][y + 1]-8*'0';
}
1的ASCII和0的ASCII差1,则旁白8个格子的ASCII数相加,减去8个0的ASCII的值,结果就是旁白字符1的个数,也就是旁白雷的个数。 这个数加上0的ASCII,传给show数组,用%c打印出来,就能展示刚刚输入的那个格子旁白有几个雷了。上面过程进行一次后要打印一下棋盘,让玩家判断情况,进行下一次输入。while循环结束后,如果count等于0,则打印你赢了,打印布置雷的信息并退出。回到test.c,此时再打印一下棋盘。
下面是完整代码。
game.h
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 4
#define COL 4
#define ROWS ROW+2
#define COLS COL+2
#define eacount 2
void initboard(char board[ROWS][COLS], int rows, int cols, char d);
void disboard(char board[ROWS][COLS], int rows, int cols);
void setline(char board[ROWS][COLS], int rows, int cols);
void ppmine(char board[ROWS][COLS], int rows, int cols);
int ccount(char board[ROWS][COLS], int rows, int cols);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void initboard(char board[ROWS][COLS], int rows, int cols, char d)
{
int i;
int k;
for( i=0;i<rows;i++)
{
for ( k = 0;k < cols ;k++)
{
board[i][k] = d;
}
}
}
void disboard(char board[ROWS][COLS], int rows, int cols)
{
printf("-----扫雷游戏-----\n");
for (int i = 0;i <= rows-2;i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1;i <rows - 1;i++)
{
printf("%d ", i);
for (int k = 1;k < cols - 1;k++)
{
printf("%c ", board[i][k]);
}
printf("\n");
}
}
void setline(char board[ROWS][COLS], int rows, int cols)
{
int count = eacount;
while (count)
{
int x = rand() % rows + 1;
int y = rand() % cols + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int ccount(char board[ROWS][COLS], int x, int y)
{
return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1]
+ board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y]
+ board[x + 1][y + 1]-8*'0';
}
void ppmine(char board[ROWS][COLS],char show[ROWS][COLS], int rows, int cols)
{
int count = (rows-2) * (cols-2) - eacount;
while (count)
{
int x, y;
printf("请输入坐标,中间有一个空格");
scanf("%d %d", &x, &y);
if (x<1 || x>(rows - 2) || y<1 || y>(cols - 2))
{
printf("输入错误\n");
continue;
}
if (board[x][y] == '1')
{
printf("你被炸死了\n");
disboard(board, ROWS, COLS);
return;
}
if (show[x][y] != '*')
{
printf("此位置已经输入\n");
continue;
}
else
{
int w;
w = ccount(board, x, y);
show[x][y] = w+'0';
}
disboard(show, ROWS, COLS);
count--;
}
if (count == 0)
{
printf("你赢了\n");
disboard(board, ROWS, COLS);
}
}
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];
char show[ROWS][COLS];
initboard(mine, ROWS, COLS,'0');
initboard(show, ROWS, COLS, '*');
disboard(show, ROWS, COLS);
setline(mine, ROW, COL);
//disboard(mine, ROWS, COLS);
ppmine(mine,show,ROWS, COLS);
disboard(show, ROWS, COLS);
}
int main()
{
menu();
int input;
srand((unsigned int)time(NULL));
do
{
printf("请选择");
scanf("%d", &input);
switch (input)
{
case 1:
{
game();
break;
}
case 0:
{
printf("退出游戏");
break;
}
default :
{
printf("输入错误,重新输入");
break;
}
}
} while (input);
return 0;
}
本代码还有很多值得优化的地方,此代码用来实现简易扫雷,读者可以对感兴趣的部分进行优化。