扫雷C语言(丐中丐版,尚有部分功能未完善)

扫雷C语言(丐中丐版,尚有部分功能未完善),第1张

扫雷C语言(丐中丐版,尚有部分功能未完善)

test.c

#include"game.h"

void meun()
{
	printf("****************************n");
	printf("****************************n");
	printf("********欢迎来到扫雷********n");
	printf("*****按1进入或者按0退出*****n");
	printf("****************************n");
	printf("****************************n");
}

void game()
{
	char mine[ROWS][COLS] = { 0 };
	char display[ROWS][COLS] = { 0 };

	initBoard(mine, ROWS, COLS, '0');
	initBoard(display, ROWS, COLS, '*');

	setBoom(mine, ROW, COL);
	showBoard(display, ROW, COL);

	findBoom(mine,display,ROW,COL);

}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		meun();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("已经成功退出游戏!欢迎下次体验!n");
			break;
		default:
			printf("输入错误,请重新选择是否进入游戏!n");
			break;
		}
	} while (input);
	return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void initBoard(char arr[ROWS][COLS], int row, int col, char set)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			arr[i][j] = set;
		}
	}
}

void setBoom(char arr[ROWS][COLS], int row, int col)
{
	int count = BOOM;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

void showBoard(char arr[ROWS][COLS], int row, int col)
{
	printf("----------扫雷----------n");
	for (int i = 0; i <=col; i++)
	{
		printf("%d ", i);
	}
	printf("n");

	for (int i = 1; i <= row; i++)
	{
		printf("%d", i);
		for (int j = 1; j <= col; j++)
		{
			printf(" %c", arr[i][j]);
		}
		printf("n");
	}
	printf("----------扫雷----------n");
}

void findBoom(char mine[ROWS][COLS], char display[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < col * row - BOOM)
	{
		printf("请输入要排查的坐标>>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("被炸死了!n");
				showBoard(mine, ROW, COL);
				break;
			}			
			else
			{
				int count = getMineCount(mine, x, y);
				display[x][y] = count + '0';
				showBoard(display, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,重新输入!n");
		}
	}
	if (win == row * col - BOOM)
	{
		printf("恭喜你,扫雷成功!n");
		showBoard(mine, ROW, COL);
	}
}

int getMineCount(char mine[ROWS][COLS], int x, int y)
{
	return 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] +
		mine[x - 1][y + 1] - 8 * '0';
}


game.h

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define BOOM 10

void initBoard(char arr[ROWS][COLS], int row, int col, char set);

void setBoom(char arr[ROWS][COLS], int row, int col);

void showBoard(char arr[ROWS][COLS], int row, int col);

int getMineCount(char mine[ROWS][COLS], int x, int y);

void findBoom(char mine[ROWS][COLS], char display[ROWS][COLS], int row, int col);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5714075.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存