初級掃雷

fuhang95發表於2020-12-13
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10

char show[ROWS][COLS];
char mine[ROWS][COLS];

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

void menu();
void test();
void game();
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//初始化陣列
void DispalyBoard(char board[ROWS][COLS], int row, int col);
//列印陣列 並且實現座標視覺化
void SetMine(char board[ROWS][COLS], int row, int col);
//由隨機數實現地雷安放
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//玩家排雷的過程邏輯 
int get_mine_count(char mine[ROWS][COLS], int x, int y);
//顯示玩家輸入座標地點的周圍一共有多少雷. 
void KuoZhan(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
//遞迴實現如果一片區域內沒有雷的情況下自動擴充套件
int syzds();
//判斷剩餘雷的個數


void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) {
	int i = 0, j = 0;
	for(i = 0; i < rows ; i++) {
		for(j = 0; j < cols ; j++) {
			board[i][j] = set;//由於一共存在兩個陣列,一個是雷區陣列,由系統隨機生成,不可視. 一個是玩家陣列,是可視的.所以要傳入兩種引數 
		}
	}
}
void DispalyBoard(char board[ROWS][COLS], int row, int col) {  
	int i = 0, j = 0;
	for(i = 0; 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 ", board[i][j]);
		}
		printf("\n");
	}
}
void SetMine(char board[ROWS][COLS], int row, int col) {	
	int count = EASY_COUNT;
	while(count) {
		int x = rand() % ROW + 1;
		int y = rand() % COL + 1;
		if(board[x][y] == '0') {
			board[x][y] = '1';
			count--;
		}
	}
}
int syzds() {	 
	int zd = 0;
	int i = 0, j = 0;
	for(i = 1; i <= ROW; i++)	//注意:由於show[ROWS][COLS]是(ROW+2)*(COL+2)開始,這個裡面應當由1開始而不是零, 
	{
		for(j = 1; j <= COL; j++)
		{
			if(show[i][j] == '*')
			{
				zd++;
			}
		}
	}
	return zd;
}
void KuoZhan(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) {
	int ret;
	ret = get_mine_count(mine, x, y);
	if(ret == 0) {
		show[x][y] = ' ';
		if((x-1) > 0 && (y-1) > 0 && (show[x-1][y-1] == '*')) {
			KuoZhan(mine, show, x-1, y-1);
		}
		if((x-1) > 0 && (y) > 0 && (show[x-1][y] == '*')) {
			KuoZhan(mine, show, x-1, y);
		}
		if((x-1) > 0 && (y+1) > 0 /*1*/&& (y+1) <= COL/*1*/ && (show[x-1][y+1] == '*')) {
			KuoZhan(mine, show, x-1, y+1);
		}
		if((x) > 0 && (y-1) > 0 && (show[x][y-1] == '*')) {
			KuoZhan(mine, show, x, y-1);
		}
		if((x) > 0 && (y+1) > 0 /*2*/&& (y+1) <= COL/*2*/ && (show[x][y+1] == '*')) {
			KuoZhan(mine, show, x, y+1);
		}
		if((x+1) > 0 && (y-1) > 0 /*3*/&& (x+1) <= ROW/*3*/ && (show[x+1][y-1] == '*')) {
			KuoZhan(mine, show, x+1, y-1);
		}
		if((x+1) > 0 && (y) > 0 /*4*/&& (x+1) <= ROW/*4*/ && (show[x+1][y] == '*')) {
			KuoZhan(mine, show, x+1, y);
		}
		if((x+1) > 0 && (y+1) > 0 /*5*/&& (x+1) <= ROW && (y+1) <= COL/*5*/ && (show[x+1][y+1] == '*')) {
			KuoZhan(mine, show, x+1, y+1);
		}
	} else {
		show[x][y] = ret + '0';
	}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y) {
	return mine[x - 1][y]+
	       mine[x - 1][y - 1]+
	       mine[x - 1][y + 1]+
	       mine[x][y - 1]+
	       mine[x][y + 1]+
	       mine[x + 1][y - 1]+
	       mine[x + 1][y]+
	       mine[x + 1][y + 1] - 8*'0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0, y = 0;
	int win = 0;
	while(1)
	{
		if(win == EASY_COUNT)
		{
			printf("掃雷成功\n");
			break; 
		}
		else
		{ 
		printf("請輸入座標:");
		scanf("%d%d", &x, &y);
		printf("\n-------------------\n");
		if(x >= 1 && x <= ROW && y >= 1 && y <= COL)
			{ //座標合法
				if(mine[x][y] == '1')
				{ //踩雷
					printf("很遺憾,您踩雷了.雷區情況如下:\n");
					DispalyBoard(mine, ROW, COL);
					break;
				} 
				else 
				{ //沒踩雷,此時就需要計算(X,y)座標周圍有幾個雷
					int count = get_mine_count(mine, x, y);
					show[x][y] = count + '0';
					KuoZhan(mine,show,x,y);
					DispalyBoard(show, ROW, COL);
					win = syzds();
					printf("-------------------\n");
				}
			}
			else 
			{ //座標非法
				printf("輸入座標錯誤,請重新輸入:\n");
			}
		}
	}
}
void game() {	//遊戲執行邏輯
	//初始化
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//列印棋盤
	printf("=====================\n");
	DispalyBoard(show, ROW, COL);
	//佈置雷區
	SetMine(mine, ROW, COL);
	//掃雷
	FindMine(mine, show, ROW, COL);
}

void menu()
{
	printf("************************************\n");
	printf("************** 1.開始 **************\n");
	printf("************** 0.退出 **************\n");
	printf("************************************\n");
} 

void test()
{
	int input = 0;
	srand((unsigned int )time(NULL));
	
	do
	{
		menu();
		printf("\n選擇:>");
		scanf("%d",&input);
		switch(input)
		{
			case 1:
				game();
				break;
			case 0:
				printf("退出遊戲\n");
				break;
			default:
				printf("選擇錯誤,請重新選擇:\n");
				break;  
		}
	}while(input);
}

int main (void)
{
	test();
	return 0; 
} 

相關文章