C语言小游戏之扫雷

一、游戏设计思路

        1.设计一个9*9棋盘

        2.在棋盘上埋好雷

        3.通过坐标判断周围雷的数量和该坐标是否有雷

 

二、游戏设计过程

         1. 首先我们要设计一个菜单,通过一个menu函数实现

void menu()
{
	printf("****************\n");
	printf("*****1.Start****\n");
	printf("*****2.End******\n");
	printf("****************\n");
}

      设计好菜单以后,我们要设计一个可选择的菜单

 
int main()
{
		int input = 0;
		menu();
		do
		{
			
			printf("做出你的选择:");
			scanf("%d", &input);
			switch (input)
			{
			case 1:
				game();
				break;
			case 2:
				printf("退出游戏~)\n");
				break;
			default:
				printf("请重新选择\n");
				break;
			}
		} while (input);
		return 0;
}

设计好菜单以后,我们开始设计游戏啦!

   2.游戏主体设计

   <1>初始化棋盘

           我们的思路是将‘1’定义为雷‘0’是没有雷,但是我们要将雷用'*'隐藏起来,因此我们需要再初始化一个新的棋盘,我们在扫雷的时候因为靠边的行和列也需要检测,因此我们的棋盘在设计的时候需要设成11*11具体如下:

game.c

void Initboard(char board[ROWS][COLS], int rows,int cols,char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

<2>棋盘的显示

        我们要对棋盘进行显示,在第一行显示0~10,在第一列显示0~10,其余的显示*,具体设计如下:

void displayboard(char board[ROWS][COLS],int rows,int cols)
{
	int i = 0;
	int j = 0;
	for (int i = 0; i < rows-1; i++)
	{
		printf("%d ", i);
		if (i == 9)
		{
			printf("\n");
		}
	}
	for (i = 1; i <= rows-2; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= cols-2; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

3.开始埋雷

        通过使用rand函数随机在棋盘上布置10个雷

void setmine(char mine[ROW][COL], int rows, int cols)
{
	int count = 10;
	while (count)
	{
		int x = 0;
		int y = 0;
		x = rand() % rows + 1;
		y = rand() % cols + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

4.开始扫雷

        在扫雷的过程中通过用户输入的坐标进行判断,首先要判断坐标是否合法,当坐标合法之后,进行判断该坐标是否为雷,如果不是雷,则对周围的坐标进行扫描,从而判断周围有几个雷

并在displayboard中进行显示,当扫雷进行到只剩下雷的时候,游戏结束。

void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	int i = 0;
	int j = 0;

	while (1)
	{
		printf("请输入想要的坐标:");
		scanf("%d %d", &x, &y);
		if (x <= ROW && x >= 1 && y <= COL && y >= 1)
		{
			if (mine[x][y] == '1')
			{
				printf("憨批你输了\n");
				displayboard(show, rows, cols);
				break;
			}
			else
			{
				int n = get_mine_count(mine, x, y);
				show[x][y] = n + '0';
				system("cls");
				displayboard(show, rows, cols);
				

			}
		}
		else
		{
			printf("傻逼你再乱输入,我就锤你!!!!\n");
			break;
		}
		int mines = 0;
		for (i = 0; i < ROW; i++)
		{
			for (j = 0; j < COL; j++)
			{
				if (show[i][j]=='*')
				{
					mines++;
				}
			}
		}
		if (counts == mines)
		{
			printf("win");
			break;
		}
	}
	
}

此外我们还需要一个返回值进行判断该坐标周围雷的数量,通过将每个坐标的数组加起来减去一个字符‘0’,通过ASCII码得到返回值,具体如下

static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + 
			mine[x - 1][y] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x + 1][y + 1] + mine[x][y+1]- 8 * '0';
}

void count(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y)
{
	int i = 0;
	int j = 0;
	if (get_mine_count(mine, x, y) == 0)
	{
		show[x][y] = ' ';
		for (i = x - 1; i <= x + 1; i++)
		{
			for (j = y - 1; j <= y + 1; j++)
			{
				if (i > 0 && i <= ROW && j > 0 && j <= COL && mine[i][j] != '1' && show[i][j] == '*')
				{
					count(show, mine, i, j);
				}
			}
		}
	}
	else
		show[x][y] = '0' + get_mine_count(mine, x, y);
}

这样一个扫雷游戏基本完成,但是还有一些不足的地方仍需改进。

三、全部代码

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void menu()
{
	printf("****************\n");
	printf("*****1.Start****\n");
	printf("*****2.End******\n");
	printf("****************\n");
}
void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 }; 
	//初始化棋盘
	Initboard(mine, ROWS, COLS, '0');
	Initboard(show, ROWS, COLS, '*');
	//埋雷
	setmine(mine, ROW, COL);
	//显示棋盘
	displayboard(show, ROWS, COLS);
	//排雷
	Findmine(mine,show,ROWS, COLS);
}
 
int main()
{
		int input = 0;
		srand((unsigned int)time(NULL));
		menu();
		do
		{
			
			printf("做出你的选择:");
			scanf("%d", &input);
			switch (input)
			{
			case 1:
				game();

				break;
			case 2:
				printf("退出游戏~)\n");
				break;
			default:
				printf("请重新选择\n");
				break;
			}
		} while (input);
		return 0;
}

game.h

#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
#define counts 10
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include<time.h>

//初始化棋盘
void Initboard(char board[ROWS][COLS], int rows, int cols,char set);
//展示棋盘
void displayboard(char board[ROWS][COLS], int rows, int cols);
//埋雷
void setmine(char mine[ROW][COL], int rows, int cols);
//排雷
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);
void count(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void Initboard(char board[ROWS][COLS], int rows,int cols,char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}


void displayboard(char board[ROWS][COLS],int rows,int cols)
{
	int i = 0;
	int j = 0;
	for (int i = 0; i < rows-1; i++)
	{
		printf("%d ", i);
		if (i == 9)
		{
			printf("\n");
		}
	}
	for (i = 1; i <= rows-2; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= cols-2; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}


void setmine(char mine[ROW][COL], int rows, int cols)
{
	int count = 10;
	while (count)
	{
		int x = 0;
		int y = 0;
		x = rand() % rows + 1;
		y = rand() % cols + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}


static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + 
			mine[x - 1][y] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x + 1][y + 1] + mine[x][y+1]- 8 * '0';
}

void count(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y)
{
	int i = 0;
	int j = 0;
	if (get_mine_count(mine, x, y) == 0)
	{
		show[x][y] = ' ';
		for (i = x - 1; i <= x + 1; i++)
		{
			for (j = y - 1; j <= y + 1; j++)
			{
				if (i > 0 && i <= ROW && j > 0 && j <= COL && mine[i][j] != '1' && show[i][j] == '*')
				{
					count(show, mine, i, j);
				}
			}
		}
	}
	else
		show[x][y] = '0' + get_mine_count(mine, x, y);
}


void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	int i = 0;
	int j = 0;

	while (1)
	{
		printf("请输入想要的坐标:");
		scanf("%d %d", &x, &y);
		if (x <= ROW && x >= 1 && y <= COL && y >= 1)
		{
			if (mine[x][y] == '1')
			{
				printf("憨批你输了\n");
				displayboard(show, rows, cols);
				break;
			}
			else
			{
				int n = get_mine_count(mine, x, y);
				show[x][y] = n + '0';
				system("cls");
				displayboard(show, rows, cols);
				

			}
		}
		else
		{
			printf("傻逼你再乱输入,我就锤你!!!!\n");
			break;
		}
		int mines = 0;
		for (i = 0; i < ROW; i++)
		{
			for (j = 0; j < COL; j++)
			{
				if (show[i][j]=='*')
				{
					mines++;
				}
			}
		}
		if (counts == mines)
		{
			printf("win");
			break;
		}
	}
	
}

最后还是感谢大家看到这里!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值