相信扫雷游戏小伙伴们肯定都玩过吧,学习了C语言中的数组、函数等基础内容之后就可以自己写一个简易的扫雷小游戏了,今天就我写扫雷小游戏的过程及思路写一篇博客,希望大家看完我的博客能有所收获。
VS2013
(说明:以下所提到的mine为置雷的棋盘,show为显示排雷信息的棋盘)
扫雷游戏:输入坐标如果该位置不是雷,则显示数字代表该坐标周围有几个雷;如果该位置是雷则游戏结束。
优化:如果输入坐标该位置不是雷,且该位置周围没有雷,则可以展开一片,直到周围有雷的地方。
优化排雷应该注意的条件:
void SetSpace(char board1[ROWS][COLS],char board2[ROWS][COLS], int x, int y) //board1为放置雷的棋盘,board2为显示排雷信息的棋盘
{
while (board1[x][y] == '0' && board2[x][y] =='*' && x >=1 && x <= ROW && y >= 1 && y <= COL)
{
if (GetMine(board1, x, y) == 0)//周围没有雷
{
board2[x][y] = ' ';
SetSpace(board1, board2, x, y + 1);
SetSpace(board1, board2, x - 1, y + 1);
SetSpace(board1, board2, x - 1, y);
SetSpace(board1, board2, x - 1, y - 1);
SetSpace(board1, board2, x, y - 1);
SetSpace(board1, board2, x + 1, y - 1);
SetSpace(board1, board2, x + 1, y);
SetSpace(board1, board2, x + 1, y + 1);
}
else //周围有雷
{
board2[x][y] = GetMine(board1, x, y) + '0';
break;
}
}
}
game.h头文件
#include
#include
#include
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MINE_NUM 10 //放置雷的数量
#define TIME ROW*COL-MINE_NUM
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void InitBoard(char board[ROWS][COLS], int row, int col, char s);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col);
void FindMinePlus(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col); //优化排雷
void SetSpace(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y);
game.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
int GetMine(char board1[ROWS][COLS], int x, int y) //统计坐标x,y周围的雷的个数
{
return (board1[x-1][y ] + board1[x - 1][y - 1] + board1[x ][y-1] + board1[x + 1][y - 1] + board1[x+1][y ]
+ board1[x + 1][y + 1] + board1[x ][y+1] + board1[x - 1][y + 1] - 8 * '0');
}
void InitBoard(char board[ROWS][COLS], int row, int col, char s)
{
int i = 0, j = 0;
for ( i = 0; i < row; i++)
{
for ( j = 0; j < col; j++)
{
board[i][j] = s;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
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 mine_count = MINE_NUM;
while (mine_count > 0)
{
int x = rand() % col + 1;
int y = rand() % row + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
mine_count--;
}
}
}
void FindMine(char board1[ROWS][COLS],char board2[ROWS][COLS], int row, int col)
{
int x = 0, y = 0,time = 0;
while (time < TIME)
{
printf("请输入要排查的坐标:\n");
scanf("%d%d", &x, &y);
if (board1[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(board1, row, col);
break;
}
else
{
if (board2[x][y] == '*')
{
time++;
}
board2[x][y] = GetMine(board1,x,y)+'0';
DisplayBoard(board2, row, col);
}
}
if (time == TIME)
{
printf("恭喜排雷成功!\n");
}
}
void SetSpace(char board1[ROWS][COLS],char board2[ROWS][COLS], int x, int y)
{
while (board1[x][y] == '0' && board2[x][y] =='*' && x >=1 && x <= ROW && y >= 1 && y <= COL)
{
if (GetMine(board1, x, y) == 0)//周围没有雷
{
board2[x][y] = ' ';
SetSpace(board1, board2, x, y + 1);
SetSpace(board1, board2, x - 1, y + 1);
SetSpace(board1, board2, x - 1, y);
SetSpace(board1, board2, x - 1, y - 1);
SetSpace(board1, board2, x, y - 1);
SetSpace(board1, board2, x + 1, y - 1);
SetSpace(board1, board2, x + 1, y);
SetSpace(board1, board2, x + 1, y + 1);
}
else //周围有雷
{
board2[x][y] = GetMine(board1, x, y) + '0';
break;
}
}
}
int Mine_Num(char board[ROWS][COLS], int row, int col)
{
int i = 0, j = 0,count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (board[i][j] == '*')
count++;
}
}
return count;
}
void FindMinePlus(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
while (1)
{
printf("请输入要排查的坐标:\n");
scanf("%d%d", &x, &y);
if (board1[x][y] == '1')
{
printf("很遗憾,你被炸死了!\n");
DisplayBoard(board1, ROW, COL);
break;
}
else{
SetSpace(board1, board2, x, y);
DisplayBoard(board2, ROW, COL);
if (Mine_Num(board2, ROW, COL) == MINE_NUM)
{
printf("恭喜你,排雷成功!\n");
break;
}
}
}
}
main.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf(" (^=^) \n");
printf("**************欢迎进入扫雷游戏*************\n");
printf("请选择: \n");
printf(" 1.扫雷 \n");
printf(" 0.退出 \n");
}
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
SetMine(mine,ROW,COL);
DisplayBoard(mine, ROW, COL);
FindMinePlus(mine, show, ROW, COL);
}
void playGame()
{
int choice = 0;
do{
menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
game();
playGame();
break;
case 0:
exit(0);
break;
default:
printf("选择错误,请重新选择正确的选项!\n");
break;
}
} while (choice !=1 && choice!= 0);
}
int main()
{
srand((unsigned int)time(NULL));
playGame();
return 0;
}
(为了演示所以直接打印了置雷棋盘)
简易扫雷:
此过程需要你把棋盘上所有不是雷的位置找出来最后才能获得游戏胜利,过程未免太过漫长,给玩家带来不好的体验。
以上代码以及思路如有不妥之处望积极提出,代码我放在我的码云上啦,需要看的小伙伴戳这里!!https://gitee.com/yuan-xinyi/c-language/tree/master/findmine