#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define jiandan 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void menu()
{
printf("***********************\n");
printf("********1.play*********\n");
printf("********0.exit*********\n");
printf("***********************\n");
}
void chushihua(char board[ROWS][COLS],int rows,int cols,char neirong)
{
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
board[i][j]=neirong;
}
}
}
void dayin(char board[ROWS][COLS],int row,int col)
{
printf("------扫雷游戏------\n");
for(int j=0;j<=col;j++){
printf("%d ",j);
}
printf("\n");
for(int i=1;i<=row;i++){
printf("%d ",i);
for(int j=1;j<=col;j++){
printf("% c ",board[i][j]);
}
printf("\n");
}
}
void shezhi(char board[ROWS][COLS],int row,int col)
{
int count=jiandan;
while(count)
{
int x=rand()%row+1;
int y=rand()%row+1;
if(board[x][y]=='0')
{
board[x][y]='1';
count--;
}
}
}
int tongji(char board[ROWS][COLS],int x,int y)
{
int a1=(board[x-1][y]-'0');
int a2=(board[x-1][y-1]-'0');
int a3=(board[x][y-1]-'0');
int a4=(board[x+1][y]-'0');
int a8=(board[x+1][y-1]-'0');
int a5=(board[x+1][y+1]-'0');
int a6=(board[x][y+1]-'0');
int a7=(board[x-1][y+1]-'0');
return a1+a2+a3+a4+a5+a6+a7+a8;
}
int paicha(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
int win=0;
int x,y;
while(win<(ROW*COL-jiandan)){
printf("输入排查的坐标:>");
scanf("%d %d",&x,&y);
if(show[x][y]!='*')
{
printf("这个位置已经被排查过了,请重新选择\n");
}
else
{
if(x>=1&&x<=row&&y>=1&&y<=col){
if(mine[x][y]=='1')
{
dayin(mine,ROW,COL);
printf("很遗憾,你踩到雷了\n");
break;
}
else
{
int count = tongji(mine,x,y);
show[x][y]=count+'0';
win++;
dayin(show,ROW,COL);
}
}
else
{
printf("输入的坐标有问题");
}
if(win==ROW*COL-jiandan){
printf("恭喜你排完了所有的雷,挑战成功\n");
break;
}
}
}
}
void game()
{
char mine[ROWS][COLS]={0};
char show[ROWS][COLS]={0};
chushihua(mine,ROWS,COLS,'0');
chushihua(show,ROWS,COLS,'*');
shezhi(mine,ROW,COL);
dayin(show,ROW,COL);
paicha(mine,show,ROW,COL);
}
int main()
{
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);
}
扫雷——c实现
于 2023-09-27 22:43:45 首次发布