引言
《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。
相信大家一定对《扫雷》这个游戏不陌生,我们只要知道游戏运行的逻辑,就可以把扫雷游戏用C语言写出来
和上一期写的三子棋小游戏一样,我们这里用game.h game.c文件实现游戏 用fox.c文件放游戏运行逻辑
一、 目录
运用while循环和switch选择语句做出游戏运行逻辑,并打印菜单
#define _CRT_SECURE_NO_WARNINGS 1;
#include"game.h"
void menu()
{
printf("*****************************\n");
printf("******* 1. play ******\n");
printf("******* 0. exit ******\n");
printf("*****************************\n");
}
void test()
{
srand((unsigned)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);
}
int main()
{
test();
return 0;
}
二、 初始化雷区
假设我们要设置9*9的雷区,里面布置十个雷,那我们就需要初始化两个雷区(9*9的字符型数组),一个用来显示附近雷的数量,另外一个就是雷场,为了后面避免越界检查,我们把雷区设为11*11的数组,只显示1 ~ 9就行了
#define _CRT_SECURE_NO_WARNINGS 1;
#define _CRT_SECURE_NO_WARNINGS 1;
#include"game.h"
//初始化棋盘
void init_board(char board[ROWS][COLS], int row, int col, char set)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
board[i][j] = set;
}
}
这个是雷的数组(0代表安全,1代表雷,雷还没埋呢,后面代码会埋地雷)
这个是显示给玩家看到的数组
三、 打印雷区
各位发挥自己的聪明才智,巧妙运用循环,打出更好看的游戏界面
//打印棋盘
void print_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("-------------扫雷------------\n");
printf(" ");
for (j = 1; j <= col; j++)
{
printf("%2d ", j);
}
printf("\n -");
for (j = 0; j <= 3 * col - 2; j++)
{
printf("-");
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d |", i);
for (j = 1; j <= col; j++)
{
printf("%c", board[i][j]);
if (j < col)
printf(" ");
}
printf("|%2d", i);
printf("\n");
}
printf(" ");
for (j = 0; j <= 3 * col - 2; j++)
{
printf("-");
}
printf("\n");
printf("-------------扫雷------------\n");
}
四、布置雷区
运用rand()函数随机生成坐标x y,埋十个
//布置雷区
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
此时的雷区:
五、 排查地雷
排查地雷是这个游戏最难的部分,我们需要一步一步把玩家选的这个格子周围都排查一下有没有地雷,只要有一颗雷就+1,这样周围有x个雷就能显示x
5.1 运用递归增加游戏可玩性
假设9*9的雷区,有10个地雷,那么玩家就要点击71次才能把简单的排雷搞定,那这也太不人性化了!因此我们运用递归函数,当周围没有地雷的时候,程序自动扩展周围的探索范围,直到周围有地雷为止
int better_count_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count = 0;
if (1 <= x && x <= ROW && 1 <= y && y <= COL && show[x][y] == '*')
{
int ret;
ret = (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');
if (0 == ret)
{
show[x][y] = ' ';
count++;
better_count_mine(mine, show, x - 1, y);
better_count_mine(mine, show, x - 1, y - 1);
better_count_mine(mine, show, x, y - 1);
better_count_mine(mine, show, x + 1, y + 1);
better_count_mine(mine, show, x + 1, y);
better_count_mine(mine, show, x + 1, y + 1);
better_count_mine(mine, show, x, y + 1);
better_count_mine(mine, show, x - 1, y + 1);
}
else
{
show[x][y] = ret + '0';
count++;
}
}
return count;
}
5.2 判断输赢
我的方法是当显示的数组只有10个(雷的个数)且玩家还没有被炸飞的时候,就排雷成功
int is_win(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
count++;
}
}
return count;
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int ret = 0;
int count = row * col - EASY_COUNT;
while (1)
{
printf("请输入要排查的雷的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("该位置已被排查,请重输\n");
continue;
}
if (mine[x][y] == '1')
{
printf("芜湖~ 你被炸飞咯!\n");
print_board(mine, ROW, COL);
break;
}
else
{
better_count_mine(mine, show, x, y);
print_board(show, ROW, COL);
}
}
else
{
printf("非法输入,请重新输入\n");
}
ret = is_win(show, ROW, COL);
if (ret == EASY_COUNT)
{
printf("恭喜你,排完了!\n");
print_board(mine, ROW, COL);
break;
}
}
}
六、 游戏全代码
6.1 game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化棋盘
void init_board(char board[ROWS][COLS], int row, int col, char set);
//打印棋盘
void print_board(char board[ROWS][COLS], int row, int col);
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);
//排查雷
void find_mine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
6.2 game.c
#define _CRT_SECURE_NO_WARNINGS 1;
#define _CRT_SECURE_NO_WARNINGS 1;
#include"game.h"
//初始化棋盘
void init_board(char board[ROWS][COLS], int row, int col, char set)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
board[i][j] = set;
}
}
//打印棋盘
void print_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("-------------扫雷------------\n");
printf(" ");
for (j = 1; j <= col; j++)
{
printf("%2d ", j);
}
printf("\n -");
for (j = 0; j <= 3 * col - 2; j++)
{
printf("-");
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d |", i);
for (j = 1; j <= col; j++)
{
printf("%c", board[i][j]);
if (j < col)
printf(" ");
}
printf("|%2d", i);
printf("\n");
}
printf(" ");
for (j = 0; j <= 3 * col - 2; j++)
{
printf("-");
}
printf("\n");
printf("-------------扫雷------------\n");
}
//布置雷区
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int better_count_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count = 0;
if (1 <= x && x <= ROW && 1 <= y && y <= COL && show[x][y] == '*')
{
int ret;
ret = (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');
if (0 == ret)
{
show[x][y] = ' ';
count++;
better_count_mine(mine, show, x - 1, y);
better_count_mine(mine, show, x - 1, y - 1);
better_count_mine(mine, show, x, y - 1);
better_count_mine(mine, show, x + 1, y + 1);
better_count_mine(mine, show, x + 1, y);
better_count_mine(mine, show, x + 1, y + 1);
better_count_mine(mine, show, x, y + 1);
better_count_mine(mine, show, x - 1, y + 1);
}
else
{
show[x][y] = ret + '0';
count++;
}
}
return count;
}
int is_win(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
count++;
}
}
return count;
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int ret = 0;
int count = row * col - EASY_COUNT;
while (1)
{
printf("请输入要排查的雷的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("该位置已被排查,请重输\n");
continue;
}
if (mine[x][y] == '1')
{
printf("芜湖~ 你被炸飞咯!\n");
print_board(mine, ROW, COL);
break;
}
else
{
better_count_mine(mine, show, x, y);
print_board(show, ROW, COL);
}
}
else
{
printf("非法输入,请重新输入\n");
}
ret = is_win(show, ROW, COL);
if (ret == EASY_COUNT)
{
printf("恭喜你,排完了!\n");
print_board(mine, ROW, COL);
break;
}
}
}
6.3 fox.c
#define _CRT_SECURE_NO_WARNINGS 1;
#include"game.h"
void menu()
{
printf("*****************************\n");
printf("******* 1. play ******\n");
printf("******* 0. exit ******\n");
printf("*****************************\n");
}
void game()
{
printf("扫雷游戏\n");
char mine[ROWS][COLS] = { 0 };//布置好的雷的信息
char show[ROWS][COLS] = { 0 };//排查出的雷的信息
//初始化棋盘
init_board(mine, ROWS, COLS,'0');
init_board(show, ROWS, COLS,'*');
//打印棋盘
//print_board(mine, ROW, COL);
print_board(show, ROW, COL);
//布置雷
set_mine(mine, ROW, COL);
print_board(mine, ROW, COL);
//排查雷
find_mine(mine, show, ROW, COL);
}
void test()
{
srand((unsigned)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);
}
int main()
{
test();
return 0;
}
这样你就得到了一个扫雷小游戏,快和你的舍友分享把~
记得点个小赞~