扫雷游戏

一、game.h头文件

在这里有主要使用到的一些函数库和我定义的宏,以及一些使用到的函数的声明。

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

#define ROW 15
#define COL 15
#define MINE_NUM 30
#define EXIT -10000
#define MINE '*'
#define GRASS '*'
#define START 1
#define END 0
#define ERROR -1

void Menu();
void Play();
void UpdateMineMap(char mine_map[ROW][COL], int row, int col, int now_row, int now_col);
void Init(char play_map[ROW][COL], char mine_map[ROW][COL], int row, int col, int mine);
void ShowMap(char play_map[ROW][COL], int row, int col);
int UpdatePlayMap(char play_map[ROW][COL], char mine_map[ROW][COL],
 int row, int col, int now_row, int now_col);
int Player(char play_map[ROW][COL], char mine_map[ROW][COL], int row, int col);

二、主要函数

1、主函数和菜单函数

如下:

void Menu()
{
 printf("*********************************************\n");
 printf("********          0. 退出游戏       *********\n");
 printf("********          1. 开始游戏       *********\n");
 printf("*********************************************\n");
}

int main()
{
 	int input = START;
 	
 	while (input)
 	{
  		Menu();
  		printf("请选择您要进行的操作:>");
  		if (!scanf("%d", &input))
  		{
   			input = ERROR;
   			getchar();
  		}
  		system("cls");
  		switch(input)
  		{
  		case END:
   			printf("游戏结束!\n");
   			break;
  		case START:
   			printf("游戏开始!\n");
  	 		Play();
   			break;
  		default:
   			printf("输入有误,请重新选择!\n");
   			break;
  		}
 	}
 	
 	system("pause");
 	return 0;
}

2、游戏函数

在这里实现各种函数的调用。

void Play()
{
 	int count = 0;
 	char play_map[ROW][COL];
 	char mine_map[ROW][COL];
 
 	Init(play_map, mine_map, ROW, COL, MINE_NUM);
 	ShowMap(play_map, ROW, COL);
 	//ShowMap(mine_map, ROW, COL);
 	while (1)
 	{
  		count += Player(play_map, mine_map, ROW, COL);
  		system("cls");
  		
  		if (count < 0)
  		{
   			break;
  		}
  		else if (count == ROW * COL - MINE_NUM)
  		{
   			printf("您赢了!\n");
   			break;
  		}
  		
  		printf("距离完成还有区域:> %d个\n", ROW * COL - MINE_NUM - count);
  		ShowMap(play_map, ROW, COL);
  		//ShowMap(mine_map, ROW, COL);
 	}
 	
 	printf("游戏结束!\n");
}

3、初始化地图

void Init(char play_map[ROW][COL], char mine_map[ROW][COL], int row, int col, int mine)
{
 	int now_row, now_col;
 	for (now_row = 0; now_row < row; now_row++)
 	{
  		for (now_col = 0; now_col < col; now_col++)
  		{
   			play_map[now_row][now_col] = GRASS;
   			mine_map[now_row][now_col] = '0';
  		}
 	}

 	srand((unsigned int)time(NULL));
 	while (mine)
 	{
  		now_row = rand() % row;
  		now_col = rand() % col;
  		if (now_row >= 0 && now_row < row && now_col >= 0 && now_col < col)
  		{
   			if (mine_map[now_row][now_col] != MINE)
   			{
    				mine_map[now_row][now_col] = MINE;
    				mine--;
   			}
  		}
	 }
 	for (now_row = 0; now_row < row; now_row++)
 	{
  		for (now_col = 0; now_col < col; now_col++)
  		{
   			if (mine_map[now_row][now_col] != MINE) 
   			{	
    				UpdateMineMap(mine_map, row, col, now_row, now_col);
   			}
  		}
 	}
}

4、计算地图中雷的个数函数

void UpdateMineMap(char mine_map[ROW][COL], int row, int col, int now_row, int now_col)
{
 	if (now_row - 1 >= 0 && now_col - 1 >= 0)
 	{
  		if (mine_map[now_row - 1][now_col - 1] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}
 	if (now_row - 1 >= 0)
 	{
  		if (mine_map[now_row - 1][now_col] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}	
 	if (now_row - 1 >= 0 && now_col + 1 < col)
 	{
  		if (mine_map[now_row - 1][now_col + 1] == MINE)
  		{
  	 		mine_map[now_row][now_col]++;
  		}
 	}
 	if (now_col - 1 >= 0)
 	{
  		if (mine_map[now_row][now_col - 1] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}
 	if (now_col + 1 < col)
 	{
  		if (mine_map[now_row][now_col + 1] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}
 	if (now_row + 1 < row && now_col - 1 >= 0)
 	{
  		if (mine_map[now_row + 1][now_col - 1] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}
 	if (now_row + 1 < row)
 	{
  		if (mine_map[now_row + 1][now_col] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}
 	if (now_row + 1 < row && now_col + 1 < col)
 	{
  		if (mine_map[now_row + 1][now_col + 1] == MINE)
  		{
   			mine_map[now_row][now_col]++;
  		}
 	}
}

5、打印地图

void ShowMap(char play_map[ROW][COL], int row, int col)
{
 	int now_row, now_col;
	printf("    |");
 	for (now_col = 0; now_col < col; now_col++)
 	{
  		printf(" %2d ", now_col + 1);
 	}
 	putchar('\n');
 	for (now_col = 0; now_col < col + 1; now_col++)
 	{
  		printf("----");
 	}
 	putchar('\n');
 	for (now_row = 0; now_row < row; now_row++)
 	{
  		printf(" %2d |", now_row + 1);
  		for (now_col = 0; now_col < col; now_col++)
  		{
   			printf(" %2c ", play_map[now_row][now_col]);
  		}
  		putchar('\n');
 	}
 	putchar('\n');
}

6、更新显示地图

int UpdatePlayMap(char play_map[ROW][COL], char mine_map[ROW][COL], 
 			int row, int col, int now_row, int now_col)
{
 	int count = 1;
 	play_map[now_row][now_col] = mine_map[now_row][now_col];

 	if (mine_map[now_row][now_col] == '0')
 	{
  		if (now_row - 1 >= 0 && now_col - 1 >= 0 && play_map[now_row - 1][now_col - 1] == GRASS)
  		{
   			if (play_map[now_row - 1][now_col - 1] = mine_map[now_row - 1][now_col - 1])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row - 1, now_col - 1) - 1;
   			}
   			count++;
  		}
  		if (now_row - 1 >= 0 && play_map[now_row - 1][now_col] == GRASS)
  		{
   			if (play_map[now_row - 1][now_col] = mine_map[now_row - 1][now_col])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row - 1, now_col) - 1;
   			}
   			count++;
  		}
  		if (now_row - 1 >= 0 && now_col + 1 < col && play_map[now_row - 1][now_col + 1] == GRASS)
  		{
   			if (play_map[now_row - 1][now_col + 1] = mine_map[now_row - 1][now_col + 1])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row - 1, now_col + 1) - 1;
   			}
   			count++;
  		}
  		if (now_col - 1 >= 0 && play_map[now_row][now_col - 1] == GRASS)
  		{
   			if (play_map[now_row][now_col - 1] = mine_map[now_row][now_col - 1])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row, now_col - 1) - 1;
   			}
   			count++;
  		}	
  		if (now_col + 1 < col && play_map[now_row][now_col + 1] == GRASS)
  		{
   			if (play_map[now_row][now_col + 1] = mine_map[now_row][now_col + 1])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row, now_col + 1) - 1;
   			}
   			count++;
  		}
  		if (now_row + 1 < row && now_col - 1 >= 0 && play_map[now_row + 1][now_col - 1] == GRASS)
  		{
  	 		if (play_map[now_row + 1][now_col - 1] = mine_map[now_row + 1][now_col - 1])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row + 1, now_col - 1) - 1;
   			}
   			count++;
  		}
  		if (now_row + 1 < row && play_map[now_row + 1][now_col] == GRASS)
  		{
   			if (play_map[now_row + 1][now_col] = mine_map[now_row + 1][now_col])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row + 1, now_col) - 1;
   			}
   			count++;
  		}
  		if (now_row + 1 < row && now_col + 1 < col && play_map[now_row + 1][now_col + 1] == GRASS)
  		{
   			if (play_map[now_row + 1][now_col + 1] = mine_map[now_row + 1][now_col + 1])
   			{
    				count += UpdatePlayMap(play_map, mine_map, row, col, now_row + 1, now_col + 1) - 1;
   			}
   			count++;
  		}
 	}

 	return count;
}

三、问题分析

在扫雷中,其实可以在玩家游戏时在进行周围雷数的计算,但我这里的也完全可行,我也计划去完成一个更加真实的扫雷游戏。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值