简易扫雷游戏实现的功能:
1、实现棋盘初始化
2、实现棋盘打印
3、实现设置雷
4、实现排雷
5、实现统计出x,y坐标周围雷的个数
6、实现判断第一次是否触雷
7、实现排除周围没有雷的区域8、检测是否排完雷
完整代码:
game.h
#define __GAME_H__
#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 InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void PrintBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char board[ROWS][COLS], int x, int y);
void FirstStep(char mine[ROWS][COLS],int *px,int *py); //避免首次触雷
void CheckRound(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y);//递归检测周围地雷个数
int CheckWin(char mine[ROWS][COLS],char show[ROWS][COLS]);//检查是否排完雷
#endif //__GAME_H__
game.c
#include "game.h"
//实现棋盘初始化
void InitBoard(char board[ROWS][COLS],int rows, int cols, char set)
{
int i=0;
int j=0;
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
board[i][j]=set;
}
}
}
//实现棋盘打印
void PrintBoard(char board[ROWS][COLS], int row, int col)
{
int i=0;
int j=0;
for(i=0; i<=col; i++) //每列标号
{
printf("%d ",i);
if(i==col)
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;
while(count)
{
int x=rand()%9+1;
int y=rand()%9+1;
if(board[x][y]=='0')
{
board[x][y]='1';
count--;
}
}
}
//实现排雷
void ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x=0;
int y=0;
int n=0;
while(1)
{
printf("请输入一个坐标:\n");
scanf("%d%d",&x,&y);
if(x>=1 && x<=row && y>=1 && y<=col) //检测输入坐标的合法性
{
if (n==0)
{
FirstStep(mine,&x,&y); //判断第一次是否触雷
CheckRound(mine,show,x,y);//递归遍历
PrintBoard(show,ROW,COL);
}
if(mine[x][y]=='1')
{
printf("你被炸死了\n");
PrintBoard(mine, row, col); //打印出炸死雷的坐标
break;
}
else
{
int count=GetMineCount(mine, x, y);//统计出x,y坐标周围雷的个数
show[x][y]=count+'0';
PrintBoard(show, row, col);
if ( (CheckWin(mine,show) ) )//判断是否排完雷
{
printf("恭喜你,排雷成功\n");
PrintBoard(mine,ROW,COL);
break;
}
n++;
continue;//跳至循环头,输入排雷坐标
}
}
else
{
printf("坐标不正确\n");
}
}
}
//实现统计出x,y坐标周围雷的个数
int GetMineCount(char board[ROWS][COLS], int x, int y)
{
return board[x-1][y]+
board[x-1][y-1]+
board[x][y-1]+
board[x+1][y-1]+
board[x+1][y]+
board[x+1][y+1]+
board[x][y+1]+
board[x-1][y+1]-8*'0';
}
//实现判断第一次是否触雷
void FirstStep(char mine[ROWS][COLS],int *px,int *py)
{ int a=*px;
int b=*py;
if (mine[(*px)][(*py)]=='1')
{
mine[(*px)][(*py)]='0';
do
{
a=rand()%ROW-1;
b=rand()%COL-1;
}
while ( (mine[a][b]=='1') || ((a)<1)||((b)<1 ));
mine[a][b]='1';
}
}
//实现排除周围没有雷的区域
void CheckRound(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y)
{
int ret;
ret=GetMineCount(mine,x,y);
if (ret==0)
{
show[x][y]=' ';
if ((x-1)>0&&(y-1)>0&&(show[x-1][y-1]=='*'))
CheckRound(mine,show,x-1,y-1);
if ((x-1)>0&&(y)>0&&(show[x-1][y]=='*'))
CheckRound(mine,show,x-1,y);
if ((x-1)>0&&(y+1)>0&&(show[x-1][y+1]=='*'))
CheckRound(mine,show,x-1,y+1);
if ((x)>0&&(y-1)>0&&(show[x][y-1]=='*'))
CheckRound(mine,show,x,y-1);
if ((x)>0&&(y+1)>0&&(show[x][y+1]=='*'))
CheckRound(mine,show,x,y+1);
if ((x+1)>0&&(y-1)>0&&(show[x+1][y-1]=='*'))
CheckRound(mine,show,x+1,y-1);
if ((x+1)>0&&(y)>0&&(show[x+1][y]=='*'))
CheckRound(mine,show,x+1,y);
if ((x+1)>0&&(y+1)>0&&(show[x+1][y+1]=='*'))
CheckRound(mine,show,x+1,y+1);
}
else
show[x][y]=ret+'0';
}
//检测是否排完雷
int CheckWin(char mine[ROWS][COLS],char show[ROWS][COLS])
{
int i=0;
int j=0;
for (i=1;i<ROW;i++)
{
for (j=1;j<COL;j++)
{
if (show[i][j]=='*')
{
if (mine[i][j]!='1')
{
return 0;
}
}
}
}
return 1;
}
test.c
#include <stdio.h>
#include "game.h"
void menu()
{
printf("*****欢迎进入扫雷游戏*****\n");
printf("**********请选择**********\n");
printf("**********1.play**********\n");
printf("**********0.exit**********\n");
}
void game()
{
char mine[ROWS][COLS]={0}; //显示雷的个数棋盘
char show[ROWS][COLS]={0}; //展示用户的棋盘
InitBoard(mine, ROWS, COLS,'0');//初始化显示雷的个数棋盘
InitBoard(show, ROWS, COLS,'*');//初始化显示用户的棋盘
PrintBoard(mine, ROW, COL); //打印棋盘
PrintBoard(show, ROW, COL);
SetMine(mine,ROW,COL);//设置雷
PrintBoard(mine,ROW,COL);//打印设置好的雷
ClearMine(mine,show,ROW,COL);//排查雷
}
void test()
{
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;
}