用C语言实现简单扫雷游戏可以检验你对C语言一些基础知识的掌握和运用,比如函数的调用问,二维数组,递归,编程规范问题等等。
这个代码能实现扫雷的:
1.设置棋盘大小和布雷数量
2.计算某个坐标周围雷的个数,在坐标上显示
3.坐标周围都无雷可以展开
为了代码的可读性,分别用game.h
text.c
game.c
三个文件实现。
game.h
#ifndef __GAME_H__
#define __GAME_H__
#define COLS 11
#define ROWS 11
#define COL 9
#define ROW 9
#define COUNT 20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void init_board(char mine[ROWS][COLS],char set, int row, int col); //初始化棋盘
void set_mine(char mine[ROWS][COLS]); //随机布雷
void display(char mine[ROWS][COLS], int row, int col);
void move(char mine[ROWS][COLS], int x, int y);//第一次避雷保护
void clearing(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y); //周围无雷,实现展开
int addition(char mine[ROWS][COLS], int x, int y);//统计周围雷的数量
int check_win(char show[ROWS][COLS]);
#endif
game.c
#include"game.h"
//初始化棋盘
void init_board(char mine[ROWS][COLS],char set, int row, int col)
{
memset(mine,'0',row*col*sizeof(mine[0][0]));
}
//随机布雷函数
void set_mine(char mine[ROWS][COLS])
{
int x,y;
int count=0;
do{
x=rand()%9+1;
y=rand()%9+1;
if(mine[x][y]!='1')
{
mine[x][y]='1';
count++;
}
}
while(count<COUNT);
}
//打印棋盘
void display(char board[ROWS][COLS], int row, int col)//打印棋盘
{
int i,j;
printf(" ");
for(i=1; i<=ROW; i++)
{
printf(" %d", i);
}
printf("\n");
for(i=1; i<row-1; i++)
{
printf(" %2d ", i);
for(j=1; j<col-1; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
//第一次避雷保护
void move(char mine[ROWS][COLS], int x, int y)
{
int a,b;
if(mine[x][y]=='1')
{
mine[x][y]='0';
while(1)
{
a=rand()%9+1;
b=rand()%9+1; //随机布雷
if( mine[a][b]!='1')
{
mine[a][b]='1';
break ;
}
}
}
}
//展开无雷区域
void clearing(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y)
{
if(0 == addition(mine,x,y))
{
show[x][y]=' ';
if((x-1)>0 && (y-1)>0 && show[x-1][y-1]=='*')
clearing(mine,show,x-1,y-1);
if((y-1)>0 && show[x][y-1]=='*')
clearing(mine,show,x-1,y-1);
if((x+1)<=ROW &&(y-1)>0&&show[x+1][y-1]=='*')
clearing(mine,show,x+1,y-1);
if((x+1)<=ROW&&show[x+1][y]=='*')
clearing(mine,show,x+1,y);
if((x+1)<=ROW&&(y+1)<=COL&&show[x+1][y+1]=='*')
clearing(mine,show,x+1,y+1);
if((y+1)<=COL&&show[x][y+1]=='*')
clearing(mine,show,x,y+1);
if((x-1)>0&&(y+1)<=COL&&show[x-1][y+1]=='*')
clearing(mine,show,x-1,y+1);
if((x-1)>0&&show[x-1][y]=='*')
clearing(mine,show,x-1,y);
}
else
show[x][y]=addition(mine,x,y)+'0';
}
//计算周围雷数
int addition(char mine[ROWS][COLS], int x, int y)
{
return 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]
+mine[x-1][y]-8*'0';
}
int check_win(char show[ROWS][COLS])
{
int i,j,count=0;
for(i=1;i<=ROW;i++) { for(j=1;j<=COL;j++) { if(show[i][j]!='*') count++; } } return count; }
test.c
#include"game.h"
//初始化棋盘
void init_board(char mine[ROWS][COLS],char set, int row, int col)
{
memset(mine,'0',row*col*sizeof(mine[0][0]));
}
//随机布雷函数
void set_mine(char mine[ROWS][COLS])
{
int x,y;
int count=0;
do{
x=rand()%9+1;
y=rand()%9+1;
if(mine[x][y]!='1')
{
mine[x][y]='1';
count++;
}
}
while(count<COUNT);
}
//打印棋盘
void display(char board[ROWS][COLS], int row, int col)//打印棋盘
{
int i,j;
printf(" ");
for(i=1; i<=ROW; i++)
{
printf(" %d", i);
}
printf("\n");
for(i=1; i<row-1; i++)
{
printf(" %2d ", i);
for(j=1; j<col-1; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
//第一次避雷保护
void move(char mine[ROWS][COLS], int x, int y)
{
int a,b;
if(mine[x][y]=='1')
{
mine[x][y]='0';
while(1)
{
a=rand()%9+1;
b=rand()%9+1; //随机布雷
if( mine[a][b]!='1')
{
mine[a][b]='1';
break ;
}
}
}
}
//展开无雷区域
void clearing(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y)
{
if(0 == addition(mine,x,y))
{
show[x][y]=' ';
if((x-1)>0 && (y-1)>0 && show[x-1][y-1]=='*')
clearing(mine,show,x-1,y-1);
if((y-1)>0 && show[x][y-1]=='*')
clearing(mine,show,x-1,y-1);
if((x+1)<=ROW &&(y-1)>0&&show[x+1][y-1]=='*')
clearing(mine,show,x+1,y-1);
if((x+1)<=ROW&&show[x+1][y]=='*')
clearing(mine,show,x+1,y);
if((x+1)<=ROW&&(y+1)<=COL&&show[x+1][y+1]=='*')
clearing(mine,show,x+1,y+1);
if((y+1)<=COL&&show[x][y+1]=='*')
clearing(mine,show,x,y+1);
if((x-1)>0&&(y+1)<=COL&&show[x-1][y+1]=='*')
clearing(mine,show,x-1,y+1);
if((x-1)>0&&show[x-1][y]=='*')
clearing(mine,show,x-1,y);
}
else
show[x][y]=addition(mine,x,y)+'0';
}
//计算周围雷数
int addition(char mine[ROWS][COLS], int x, int y)
{
return 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]
+mine[x-1][y]-8*'0';
}
int check_win(char show[ROWS][COLS])
{
int i,j,count=0;
for(i=1;i<=ROW;i++) { for(j=1;j<=COL;j++) { if(show[i][j]!='*') count++; } } return count; }