简易的扫雷展示

21 篇文章 0 订阅
9 篇文章 0 订阅
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW + 3
#define COLS COL + 3
#define MINE_COUNT 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[][COLS], int row, int col, char c);
void ShowBoard(char board[][COLS], int row, int col);
void SetMine(char board[][COLS], int row, int col);
int GetMineCount(char b[][COLS], int x, int y);
void JudgeMine(char user_display[][COLS], char mine[][COLS], int row, int col);


void Menu()
{
	printf("******************\n");
	printf("***欢迎来到扫雷***\n");
	printf("******0.exit******\n");
	printf("******1.play******\n");
	printf("******************\n");
}
void InitBoard(char board[][COLS], int row, int col, char c)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = c;
		}
	}
}

void ShowBoard(char board[][COLS], int row, int col)
{
	printf("--------------begin-----------\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 ", board[i][j]);
		}
		printf("\n");
	}
	printf("---------------end------------\n");
}
void SetMine(char board[][COLS], int row, int col)
{
	int mine_count = MINE_COUNT;
	while (mine_count > 0)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if(board[x][y] == '0')
		{
			board[x][y] = '1';
			mine_count--;
		}
	}
}
int GetMineCount(char b[][COLS], int x, int y)
{
	return (b[x - 1][y - 1]) + (b[x - 1][y]) + (b[x - 1][y + 1]) +
		(b[x][y - 1]) + (b[x][y + 1]) + (b[x + 1][y - 1]) +
		(b[x + 1][y]) + (b[x + 1][y + 1]) - 8 * '0';
}
void JudgeMine(char user_display[][COLS], char mine[][COLS], int row, int col)
{
	int step_count = 0;
	while (step_count < row * col - MINE_COUNT)
	{
		printf("please enter x,y: ");
		fflush(stdout);
		int x, y;
		scanf("%d,%d", &x, &y);

		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("踩雷了!\n");
				ShowBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int mine_count = GetMineCount(mine, x, y);
				printf("mine_count : %d\n", mine_count);
				user_display[x][y] = mine_count + '0';
				ShowBoard(user_display, ROW, COL);

				step_count++;
			}
		}
		else
		{
			printf("输入的坐标错误, 请重新输入\n");
		}
	}

	if (step_count == ROW * COL - MINE_COUNT)
	{
		printf("你就是扫雷之王?\n");
	}

}

void Game()
{
	char user_display[ROWS][COLS];
	char mine_board[ROWS][COLS];
	InitBoard(user_display, ROWS, COLS, '?');
	InitBoard(mine_board, ROWS, COLS, '0');
	ShowBoard(user_display, ROW, COL);
	SetMine(mine_board, ROW, COL);
	JudgeMine(user_display, mine_board, ROW, COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	setvbuf(stdout, NULL, _IONBF, 0);
	int input;
	do
	{
		Menu();
		printf("请输入你的选择:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("再见拜拜");
			break;
		case 1:
			Game();
			break;
		default:
			printf("请重新输入!\n");
		}
	} while (input != 0);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值