通过二维数组来编写一个扫雷游戏
实现的功能:
1、布置雷
2、排雷
3、踩到雷被炸死
4、排雷成功
示例代码如下:
头文件 game.h
//核心位置的行和列
//整个数组的大小
void init_board(char board[ROWS][COLS],int rows,int cols,char set); //初始化
void print_board(char board[ROWS][COLS],int row,int col); //打印棋盘
void set_mine(char board[ROWS][COLS],int row,int col); //设置雷
void clear_mine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col); //排雷
int get_mine_count(char board[ROWS][COLS],int x,int y); //统计雷的个数
测试代码 test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game ()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
init_board(mine,ROWS,COLS,'0');
set_mine(mine,ROW,COL);
init_board(show,ROWS,COLS,'*');
printf ("\n");
print_board(show,ROW,COL);
printf ("\n");
clear_mine(mine,show,ROW,COL);
}
void menu ()
{
printf ("\n");
printf ("\n");
printf ("————————————欢迎来到扫雷游戏————————————\n");
printf ("\n");
printf (" 1.开始游戏 \n");
printf (" 0.退出游戏 \n");
printf ("\n");
printf ("————————————————————————————————\n");
}
int main ()
{
int input = 0;
do
{
menu();
printf ("请输入你的选择:");
scanf ("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default :
printf ("输入错误,请重新选择!\n");
break;
}
}while(input);
return 0;
}
核心代码 game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char board[ROWS][COLS],int rows,int cols,char set)
{
memset (board,set,rows * cols * sizeof(board[0][0]));
}
void print_board(char board[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
printf (" ");
for (i = 1;i <= col;i++)
{
printf ("%d ",i);
}
printf ("\n");
printf ("\n");
for (i = 1;i <= row;i++)
{
printf ("%d ",i);
for (j = 1;j <= col;j++)
{
printf ("%c ",board[i][j]);
}
printf("\n");
}
}
void set_mine(char board[ROWS][COLS],int row,int col)
{
int i = EASY;
int x = 0;
int y = 0;
srand((unsigned int )time(NULL));
while (i)
{
x = rand()%ROW+1;
y = rand()%ROW+1;
if (board[x][y] == '0')
{
board[x][y] = '1';
i--;
}
}
}
int get_mine_count(char board[ROWS][COLS],int x,int y)
{
return 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]+
board[x+1][y]+
board[x+1][y-1]-8*'0';
}
void move_mine(char mine[ROWS][COLS], int x, int y)
{
mine[x][y] = '0';
while (1)
{
int x = rand() % ROW + 1;
int y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
break;
}
}
}
void circle_print(char show[ROWS][COLS],char mine[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
char count = get_mine_count(mine, x, y)+'0';
if (count == '0')
{
for (i = (x - 1); i <= (x + 1); i++)
{
for (j = (y - 1); j <= (y + 1); j++)
{
show[i][j] = get_mine_count(mine,i,j) + '0';
}
}
}
}
int is_win(char show[ROWS][COLS])
{
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++;
}
}
if (count == EASY)
{
return -1;
}
return 0;
}
void clear_mine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
int x = 0;
int y = 0;
int count = 0;
int a = 0;
while (a != -1)
{
printf ("请输入你的坐标:");
scanf ("%d%d",&x,&y);
if (mine[x][y] == '1' && count == 0)
{
move_mine( mine, x, y);
goto flag;
}
else if (mine[x][y] == '1' && count != 0)
{
printf ("被炸死了!\n");
print_board(mine,ROW,COL);
return;
}
else if (mine[x][y] =='0')
{
flag:
show [x][y] = get_mine_count(mine,x,y)+'0';
circle_print(show,mine,x,y);
print_board(show,ROW,COL);
a = is_win(show);
if(a == -1)
{
printf ("恭喜你赢了!\n");
print_board(mine,ROW,COL);
break;
}
}
count++;
}
}
以上代码在vs2008运行成功