典中典1(扫雷)

可能作为第一个要学习的游戏,这里补充一下,⼀般而言,函数的声明、类型的声明放在头⽂件(.h)中,函数的实现是放在源⽂件(.c)⽂件中。我们分别命名为game.h,game.c,和test.c。
1.假设我们要在9*9的棋盘上布置雷的信息和排查雷,⾸先就是创建⼀个9*9的数组来存放
如果这个位置布置雷,我们就存放1,没有布置雷就存放0.如下图所示

2.由于扫雷的游戏逻辑是,围绕一个位置的九宫格的范围排查是否有雷,所以为了防止边缘位置溢出,我们应该将数组创建为11*11的范围。如下图所示。

 3.另外,我们还需要考虑到雷实际设置的位置可以作为一个新的棋盘在结束后展示,因此我们实际上要设置两个棋盘,分别用来存放和排查。

 

4. 谈谈我所遇到的问题,在实际编写的过程中,要注意头文件的添加;

同时,确保函数的定义与使用名称的一致(字母大小写);

理清逻辑顺序:初始化棋盘,打印棋盘,布置雷,排查雷。

下面是具体的代码。

#pragma once

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

#define EASY_COUNT 10
#define ROW 9
#define COL 9

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

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(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);
#define  _CRT_SECURE_NO_WARNINGS 1

#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf("--------扫雷游戏-------\n");
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		int j = 0;
		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 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');
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - EASY_COUNT)
	{
		printf("请输入要排查的坐标:>");
		(void)scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,被炸死了\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = x + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}
#define  _CRT_SECURE_NO_WARNINGS 1

#include "game.h"
void menu()
{
	printf("****************\n");
	printf("*******1.play****\n");
	printf("*******0.exit***\n");
	printf("****************\n");

}
void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	InitBoard(mine, ROWS, COLS,'0');
	InitBoard(show, ROWS, COLS,'*');
	DisplayBoard(show, ROW, COL);
	SetMine(mine, ROW, COL);
	FindMine(mine, show, ROW, COL);
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		(void)scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

总的来说,整个游戏的逻辑还是比较好理解的,但是如果作为初学者想要在短时间内独立地编写和完善好程序,还是有不小的难度的,不能急于求成,也不用妄自菲薄。

万事开头难,坚持学习下去,能力自然会慢慢提升。

休息一下,给大家分享的第三首歌 Sacred Play Secret Place(神圣的游戏秘密场所) - Matryoshka - 单曲 - 网易云音乐歌曲名《Sacred Play Secret Place》,别名《神圣的游戏秘密场所》,由 Matryoshka 演唱,收录于《Laideronnette》专辑中。《Sacred Play Secret Place》下载,《Sacred Play Secret Place》在线试听,更多相关歌曲推荐尽在网易云音乐icon-default.png?t=N7T8https://music.163.com/song?id=26093064&userid=597846121

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值