c项目之扫雷

扫雷游戏:(待完善)
设计思路:
    1、有难度的选择:初级、中级、高级,不同的难度级别会有不同数量的雷和不一样大小的网格;
    2、通过输入坐标来确定你选择的踩雷位置,若是该位置无雷,则会显示周边雷的个数,而当周边也无雷时,就会自行判断周边的周边雷的个数并显示,依次向外扩散,若是该位置有雷,游戏结束;
    3、在统计周边雷的个数时,为了省略对周边的位置是否超出数组大小,在定义数组时,定义为[+2][+2]形式。

#ifndef _CLEARMINES_H_
#define _CLEARMINES_H_

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

#pragma warning(disable:4996)

#define pROW 9
#define pCOL 9
#define pMineSum 10
#define iROW 16
#define iCOL 16
#define aROW 16
#define aCOL 30

void playPrimary();
void playIntermediate();
void playAdvanced();

#endif

#include"clearMines.h"

void view()
{
	printf("*********************************\n");
	printf("********* 欢迎来到扫雷!*********\n");
	printf("******** 1:paly!  0:exit! *******\n");
	printf("*********************************\n");
	printf("please select:");
}

void selectModelView()
{
	printf("**********************************\n");
	printf("********** 选择游戏难度 **********\n");
	printf("* a:初级(10个雷,9*9平铺网格)***\n");
	printf("* b:中级(40个雷,16*16平铺网格)*\n");
	printf("* c:高级(99个雷,16*30平铺网格)*\n");
	printf("**********************************\n");
	printf("please select:");
}

int main()
{
	int sel;
	char selmod;
	do{
		view();
		scanf("%d", &sel);
		switch (sel)
		{
		case 0:
			exit(0);
		case 1:
			system("cls");
			fflush(stdin);
			selectModelView();
			scanf("%c", &selmod);
			switch (selmod)
			{
			case 'a':
				playPrimary();
				break;
			case 'b':
				playIntermediate();
				break;
			case 'c':
				playAdvanced();
				break;
			default:
				printf("input error.try again!\n");
				break;
			}
			break;
		default:
			printf("input error.try again!\n");
			break;
		}
	} while (1);
	system("pause");
	return 0;
}

#include"clearMines.h"


void showView(char arrp[][pCOL + 2])
{
	int k = 1;
	printf("  ");
	for (k = 1; k <= pCOL; k++){
		printf("%4d", k);
	}
	printf("\n___");
	for (k = 0; k < pCOL; k++){
		printf("____");
	}
	printf("\n");

	int i = 1;
	for (; i <= pROW; i++){
		int j = 1;
		printf("%d ", i);
		for (; j <= pCOL; j++){
			printf("| %c ", arrp[i][j]);
		}
		printf("|\n  ");
		for (k = 0; k < pCOL; k++){
			printf("|___");
		}
		printf("|\n");
	}
}

void layMines(char arrp[][pCOL + 2], int start, int end)//设置雷区
{
	srand((unsigned)time(NULL));
	int x, y;
	int n = 0;
	do{
		x = (rand() % end - start + 1) + start;
		y = (rand() % end - start + 1) + start;
		if (arrp[x][y] == '0'){
			arrp[x][y] = '1';//雷区设置为字符'1':方便计算周围的雷总数
			n++;
		}
	} while (n < pMineSum);
}

char aroundMine(char arrM[][pCOL + 2], int x, int y)
{
	return (arrM[x - 1][y - 1] + arrM[x - 1][y] + arrM[x - 1][y + 1] + \
		arrM[x][y - 1] + arrM[x][y + 1] + \
		arrM[x + 1][y - 1] + arrM[x + 1][y] + arrM[x + 1][y + 1]) - 8 * '0';//周围雷的个数
}


char judge(char arrV[][pCOL + 2], char arrM[][pCOL + 2], int x, int y)
{
	if (arrM[x][y] == '1'){
		return 'f';//file
	}
	int count = 0;
	int x_a, y_a;
	arrV[x][y] = aroundMine(arrM, x, y)+ '0';
	count++;
	if (arrV[x][y] == '0'){
		for (int i = x - 1; i <= x + 1; i++){
			for (int j = y - 1; j <= y + 1; j++){
				if (i == x && j == y){
					continue;
				}
				x_a = i;
				y_a = j;
				arrV[x_a][y_a] = aroundMine(arrM, x_a, y_a) + '0';
				count++;
			}
		}
	}
	if (count == pROW*pCOL - pMineSum){
		return 's';//success
	}
	return 'n';
}

int start(char arrV[][pCOL + 2], char arrM[][pCOL + 2])
{
	int x, y;
	char j;
	do{
		printf("please input the position <x y> of your select!\n");
		scanf("%d%d", &x, &y);
		if (x < 1 || x > pROW || y < 1 || y > pCOL){
			printf("This position out of bounds!\n");
		}else if (arrV[x][y] == '#'){
			j = judge(arrV, arrM, x, y);
			switch (j){
			case 'f':
				return 1;
			case 's':
				return 0;
			case 'n':
				break;
			}
			break;
		}else{
			printf("This location has been determined!\n");
		}
	} while (1);
	return -1;
}

void playPrimary()
{
	char pView[pROW + 2][pCOL + 2];
	char pMine[pROW + 2][pCOL + 2];
	memset(pView, '#', (pROW + 2)*(pCOL + 2));
	memset(pMine, '0', (pROW + 2)*(pCOL + 2));//将布雷的数组初始化为'0'
	layMines(pMine, 1, pCOL);//布雷
	int res;
	do{
		system("cls");
		showView(pView);
		res = start(pView, pMine);
		if (res != -1){
			break;
		}
	} while (1);

	system("cls");
	switch (res){
	case 1:
		printf("你踩到雷了!\n");
		break;
	case 0:
		printf("扫雷成功!\n");
		break;
	}
	showView(pMine);
}

void playIntermediate()
{

}

void playAdvanced()
{

}

猜你喜欢

转载自blog.csdn.net/tec_1535/article/details/80033185
今日推荐