初学C语言的人都希望自己可以实现一些小游戏的制作,今天就用纯C语言实现扫雷游戏的制作,因为学习C语言图形库需要自己安装,所以目前的扫雷游戏是在黑窗口中进行的。
创建两个二维数组,一个用来存放雷阵,一个用于玩家排雷,由于每次扫雷都需要排查周围8个坐标的雷的个数,但对于边界的坐标来说不好实现,所以我们干脆创建一个比雷阵大一圈即(n+2)*(n+2)的矩阵
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10//雷的个数
#include
#include"mine.h"
//用于选择是否开始游戏
void text()
{
int judge = 1;
while (judge)
{
menu();
printf("请选择>\n");
scanf("%d", &judge);
switch (judge)
{
case 1:
game(); break;
case 0:
printf("退出游戏\n"); break;
default:
printf("选择错误,请重新输入\n"); break;
}
}
}
int main()
{
text();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
//打印菜单栏
void menu()
{
printf("***************************\n");
printf("******** play.1 ********\n");
printf("******** exit.0 ********\n");
printf("***************************\n");
}
//将数组初始化为“*”
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0, 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, j;
printf(" ");
for (i = 1; i < col + 1; i++)
printf("%d ", i);
printf("\n");
for (i = 0; i < col + 1; i++)
printf("%c", 32);
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
printf("%c ", board[i][j]);
printf("\n");
}
printf("\n");
}
//布雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x, y;
srand((unsigned int)time(NULL));
for (; count;)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
//用来获取此时雷阵中某一个坐标周围8个坐标的雷的个数
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
int count = 0;
int i, j;
for (j = y - 1; j < y + 2; j++)
for (i = x - 1; i < x + 2; i++)
if (mine[j][i] == '1')
count++;
return count;
}
//递归用于实现消除一片的效果
void DisPlayBoard_(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count;
int i, j;
for (j = y - 1; 0 <= j && j < y + 2 && j <= ROWS; j++)
for (i = x - 1; 0 <= i && i < x + 2 && i <= COLS; i++)
{
count = get_mine_count(mine, i, j);
if (count == 0 && mine[j][i] == '0')
{
show[j][i] = 32;
mine[j][i] = 32;
DisPlayBoard_(mine, show, i, j);
}
else show[j][i] = count + '0';
}
}
//用于获取雷阵的剩余的雷的个数
int Count(char show[ROWS][COLS], int row, int col)
{
int count = 0;
int i, j;
for (i = 1; i <= row; i++)
for (j = 1; j <= col; j++)
if (show[i][j] == '*')
{
count++;
}
return count;
}
//开始排雷过程
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
printf("请输入排查雷的坐标>\n");
int x, y;
int win = row * col;
for (; win > EASY_COUNT;)
{
scanf("%d%d", &x, &y);
if (x >= 1 && x <= col && y >= 1 && y <= row)
{
if (mine[y][x] == '1')
{
printf("很遗憾!!!你被炸死了...\n");
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[y][x] = count + '0';
if (count == 0)
DisPlayBoard_(mine, show, x, y);
DisPlayBoard(show, ROW, COL);
win = Count(show, row, col);//寻找show数组中*的个数
}
}
else printf("输入坐标非法,请重新输入坐标>\n");
}
if (win == EASY_COUNT)
printf("恭喜你!排雷成功!!!\n");
}
//开始游戏后的前期准备,创建、初始化、布雷等
int game(void)
{
char show[ROWS][COLS];
char mine[ROWS][COLS];
InitBoard(show, ROWS, COLS, '*');
InitBoard(mine, ROWS, COLS, '0');
SetMine(mine, ROW, COL);
DisPlayBoard(mine, ROW, COL);//此句用于打印雷阵,不想看到雷阵的可以去掉
DisPlayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
return 0;
}