数组的应用-扫雷(简易)

C语言真的非常强大,用数组做了一个扫雷,有趣又对C语言数组用了更深刻的认识。

废话不多说上代码。

game.c

#include"game.h"
void init_board(char mine[ROWS][COLS],char set, int row, int col)//初始化
{
	memset(mine,set,(row*col*sizeof(mine[0][0])));
}
void set_mine(char mine[ROWS][COLS])//设置地雷
{
	
	int count = MAX;
	int x = 0;
	int y = 0;
	srand((unsigned int)time(NULL));
	while(count)
	{
		 x = rand()%9+1;
		 y = rand()%9+1;
		if(mine[x][y]=='0')
		{
			mine[x][y]='1';
			count--;
		}
	}
}
void display(char mine[ROWS][COLS], int row, int col)//打印扫雷界面
{
	int i = 0;
	int j = 0;
	printf("  ");
	for(i=1;i<=COL;i++)
	{
		printf("%d ",i);
	}
	printf("\n");
	for(i=1;i<=ROW;i++)
	{
		printf("%d ",i);
		for(j=1;j<=COL;j++)
		{
			printf("%c ",mine[i][j]);
		}
		printf("\n");

	}

}
int get_mine_count(char mine[ROWS][COLS], int x, int y)//获得当前位置附近的地雷数目
{
	return 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]+
		   mine[x+1][y+1]+mine[x][y+1]-8*'0';
}
void move(char mine[ROWS][COLS], int x, int y)//确保玩家第一步不会被炸。
{
	int q = 0;
	int p = 0;
	int count = 1;
	mine[x][y] = '0';
	q = y;
	p = x;
	
	
	
	srand((unsigned int)time(NULL));
	while(count)
	{
		 x = rand()%9+1;
		 y = rand()%9+1;
		if((mine[x][y]=='0')&&(x!=p||y!=q))
		{
			mine[x][y] = '1';
			count--;
		}
	}
}


game.h

#ifndef __GAME_H__
#define __GAME_H__

#define COLS 11
#define ROWS 11

#define COL (COLS-2)
#define ROW (ROWS-2)

#define MAX 10

#include <stdio.h>
#include <stdlib.h>//srand()
#include <time.h> //time()
#include <string.h>//memset()

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);
int get_mine_count(char mine[ROWS][COLS], int x, int y);

#endif//__GAME_H__


test.c

#include"game.h"
void menu()//菜单
{
	printf("*************************\n");
	printf("***** 1.Start 0.Exit ****\n");
	printf("*************************\n");

}
enum Option
{
	EXIT,
	PLAY
};
void game()
{
	//c语言必须把变量声明在最前面
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	int x = 0;
	int y = 0;
	int t = 0;
	int count = 0;
	init_board(mine,'0',ROWS,COLS);
	init_board(show,'*',ROWS,COLS);
	set_mine(mine);
	display(mine,ROWS,COLS);
	display(show,ROWS,COLS);
	while(count<((COL*ROW)-MAX))
	{
      L:
		printf("请输入坐标>");
		scanf("%d%d",&x,&y);
		if((x>=1)&&(x<=ROW)&&(y>=1)&&(y<=9))
		{
			//这是一个11*11  刚好输入X Y 就是9*9里的位置
			if(count==0)
			{
				if(mine[x][y]!='1')
				{
					t = get_mine_count(mine,x,y);
					show[x][y] = t +'0';
					display(show,ROWS,COLS);
					count++;
					goto L;
				}
				else
				{
					move(mine,x,y);
					t = get_mine_count(mine,x,y);
					show[x][y] = t +'0';
					display(show,ROWS,COLS);
					count++;
					goto L;

				}

			}
			if(mine[x][y]!='1')
			{
				t = get_mine_count(mine,x,y);
				show[x][y] = t +'0';
				display(show,ROWS,COLS);
				count++;
			}
			else
			{
				printf("你被雷炸死了!\n");
				break;
			}

		}
		else
		{
			printf("输入错误!\n");
		}

	}
	if(count==((COL*ROW)-MAX))
	{
		printf("恭喜你取得胜利!\n");
	}
}
void test()
{
	
	int input;
	do
	{
		menu();
		printf("请选择>");
		scanf("%d",&input);
		switch(input)
		{
		case PLAY:
			game();
			break;
		case EXIT:
			break;
		default:
			printf("输入错误!\n");
			break;
		}
	}while(input);
}
int main()
{
	test();
	return 0;
}
测试图:

文件分类



测试地雷数统计是否准确


第一次不会被炸


游戏结束

游戏胜利(总共81个我设置了80个地雷)



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值