C实现---扫雷游戏

扫雷游戏,即在10*10的矩阵上扫雷。用编程实现,输入对应的坐标,判断是否踩中雷

下面这个游戏由一个test.c测试源文件

                          一个game.c玩游戏的源文件

                         其对应的头文件game.h三部分组成

这写代码的好处有利于测试, 方便管理和以后的更改和优化游戏

game.h    //头文件
#ifndef __GAME_H__
#define __GAME_H__

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

#define Rows 10
#define Cols 10    
#define NUM_MINE 10   //设置雷数

void display(char arr[Rows + 2][Cols + 2], int rows, int cols);                        //打印雷
void set_mine(char arr[Rows + 2][Cols + 2], int rows, int cols);                      //布置雷
int del_mine(char arr[Rows + 2][Cols + 2], int x, int y);                             //判断点击的地方一周的雷数
int aroud(char arr[Rows + 2][Cols + 2], char app[Rows + 2][Cols + 2], int x, int y); /*把点击的地方一周没有雷的地方                                                                                        都显示出来,并且显示周围雷                                                                                          的个数*/
void first(char arr[Rows + 2][Cols + 2], int x, int y);                             /*如果第一步踩到雷,就把乃个雷移到别的位置*/   

#endif  //__GAM


test.c  //测试源文件  用来调用测试各个功能的函数

# define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

enum lala
{
	EXIT,
	PLAY
};

void menu()
{
	printf("!!!**************************************!!!\n");
	printf("  !!!**** <welcome to play games> *****!!!\n");
	printf("  !!!***********  PLAY   1 ************!!!\n");
	printf("  !!!***********  EXIT   0 ************!!!\n");
	printf("!!!**************************************!!!\n");
}

void game()
{
	srand((unsigned int)time(NULL));
	char mine[Rows+2][Cols+2] = { 0 };           //用来布置雷的
	char show[Rows+2][Cols+2] = { 0 };           //用来显示给玩家看的
	int x = 0;
	int y = 0;
	int win = 0;        //计算排除了多少个空格
	int step = 0;      //计算步数
	memset(mine, '0', sizeof(char)*(Rows+2)*(Cols+2));
	memset(show, '*', sizeof(char)*(Rows + 2)*(Cols+2));
	set_mine(mine, Rows, Cols);
	//display(mine, Rows, Cols);      //用来测试时观察雷的位置
	printf("      <--------游戏进行中...------>\n");
	display(show, Rows, Cols);
	printf("您的当前的步数[ %d ]\n", step);
	printf("您排除的格子数[ %d ]\n", win);
	printf("请输入对应坐标:");
	while (win < (Rows*Cols - NUM_MINE))
	{
		scanf("%d%d", &x, &y);
		fflush(stdin);
		system("cls");
		if ((x <= Rows) && (x > 0) && (y <= Cols) && (y > 0))   //判断输入的坐标是否在给定坐标盘上
		{
			if ('*' == show[x][y])                      //判断展示的坐标盘上是否被赋过值,是否被前面输过的坐标所占用
			{
			    step++;                                   //计算步数
				if (step == 1)
				{
					first(mine, x, y);
				}
				if ('0' == mine[x][y])                   //判断输入的坐标是否踩中雷
				{
					win += aroud(mine, show, x, y);         //把点击的地方一周没有雷的地方都显示出来,并且显示周围雷的个数计算排除了多少个空格
					                                        //并返回排除没雷格子的数目
					if (win >= (Rows*Cols - NUM_MINE)) 
					{
						break;                                            
					}
					printf("      <--------游戏进行中...------>\n");
					display(show, Rows, Cols);
					printf("您的当前的步数[ %d ]\n", step);
					printf("您排除的格子数[ %d ]\n", win);
					printf("请输入对应坐标:");
				}
				else                        //若踩中雷,则游戏结束
				{
						printf("< %c%c%c  Game  over  %c%c%c >\n", 001, 001, 001, 001, 001, 001);
						del_mine(mine, x, y);
						show[x][y] = '@';
						display(show, Rows, Cols);
						printf("您的当前的步数[ %d ]\n", step);
						printf("您排除的格子数[ %d ]\n", win);
						printf("      <--------所有雷的位置------>\n");
						display(mine, Rows, Cols);
						system("pause");
						system("cls");
						break;
				}
			}
			else                             //若被赋过值、被前面输过的坐标所占用,则从新输入
			{
				printf("      <--------游戏进行中...------>\n");
				display(show, Rows, Cols);
				printf("您的当前的步数[ %d ]\n", step);
				printf("您排除的格子数[ %d ]\n", win);
				printf("该坐标已被占用,请重新输入:\a\a\a\n");
			}
		}
		else           //若输入的坐标不在棋盘上,则提示重新输入
		{
			printf("      <--------游戏进行中...------>\n");
			display(show, Rows, Cols);
			printf("您的当前的步数[ %d ]\n", step);
			printf("您排除的格子数[ %d ]\n", win);
			printf("输入有误,请重新输入:\a\a\a\n");
		}
	}
	if (win >= (Rows*Cols - NUM_MINE))    //判断所有雷排完则赢
	{
		printf("       <-----%c%c%c恭喜你赢了%c%c%c----->\n", 001, 001, 001, 001, 001, 001);
		display(show, Rows, Cols);
		printf("您的当前的步数[ %d ]\n", step);
		printf("您排除的格子数[ %d ]\n", win);
		//display(mine, Rows, Cols);
		system("pause");
		system("cls");
	}
}
int main()
{
	int key = 0;
	do
	{
		menu();
		printf("请选择0或1选项开始游戏:");
		scanf("%d", &key);
		fflush(stdin);
		switch (key)
		{
		case EXIT:system("cls");
				printf("%c%c%c谢谢使用%c%c%c\n", 001, 001, 001, 001, 001, 001);
				system("pause");
				exit(0);
		case PLAY:
			  system("cls");
		      game();
			  break;		
		default: printf("<!!无效选项!!>\n");
		        Sleep(1000);
		        system("cls");
		        break;
		}
	} while (key);
	return 0;
}

game.c   //这个源文件里全部写的是扫雷游戏各个功能的实现

# define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

int aroud(char arr[Rows + 2][Cols + 2], char app[Rows + 2][Cols + 2], int x, int y) //把点击的地方一周没有雷的地方都显示出来,并且显示周围雷的个数
{
	int num = 0;
	int i = 0;
	int j = 0;
	int win = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = y - 1; j <= y + 1; j++)
		{
		if (i>0&&i<Rows+1&&j>0&&j<Cols+1&&arr[i][j] != '1')
		{
			num = del_mine(arr, i, j);
			if (app[i][j] == '*')
			{
				win++;
				app[i][j] = num + '0';
			}
		}
		}
	}
	return win;
}
	

int del_mine(char arr[Rows + 2][Cols + 2], int x, int y) //判断点击的地方一周的雷数
{
	return ((arr[x - 1][y - 1] - '0') +
		(arr[x][y - 1] - '0') +
		(arr[x + 1][y - 1] - '0') +
		(arr[x - 1][y] - '0') +
		(arr[x + 1][y] - '0') +
		(arr[x - 1][y + 1] - '0') +
		(arr[x][y + 1] - '0') +
		(arr[x + 1][y + 1] - '0'));
}

void first(char arr[Rows + 2][Cols + 2], int x, int y)//如果第一步踩到雷,就把乃个雷移到别的位置   
{
	if (arr[x][y] == '1')
	{
		arr[x][y] = '0';
		while (1)
		{
			x = rand()%10-1;
			y = rand()%10-1;
			if (arr[x][y] == '0')
			{
				arr[x][y] = '1';
				break;
			}
		}
	}
}

void set_mine(char arr[Rows + 2][Cols + 2], int rows, int cols)//布置雷
{
	int count = NUM_MINE;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % 10+1;
		y = rand() % 10+1;
		if ('0' == arr[x][y])
		{
			count--;
			arr[x][y] = '1';
		}
	}
}

void display(char arr[Rows + 2][Cols + 2], int rows, int cols)    //打印雷
{
	int i = 0;
	int j = 0;
	printf("   ");
	for (i = 1; i <= rows; i++)
	{  
		printf("%d   ", i);
	}
	printf("\n  ________________________________________\n");
	for (i = 1; i <= rows; i++)
	{
		printf("%2d| ", i);
		for (j = 1; j <= cols; j++)
		{
			printf("%c | ", arr[i][j]);
		}
		printf("\n  |---|---|---|---|---|---|---|---|---|---|\n");
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值