栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言小游戏--扫雷

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言小游戏--扫雷

c语言学习进行时

第一次涉及较长篇幅的代码设计

一个简易到不能再简易的小游戏-扫雷

主函数GameMain.c

功能:显示菜单&进入游戏

#include"game.h"

int main(int argc, char* argv[])
{
	int select = 1;
	while (select)
	{
		printf("*******************************n");
		printf("*           扫雷游戏          *n");
		printf("*******************************n");
		printf("*           [1]Play           *n");
		printf("*           [0]Exit           *n");
		printf("*******************************n");
		printf("请选择:>");
		scanf_s("%d", &select);
		if (select == EXIT)
			break;
		if (select != PLAY)
		{
			printf("输入有误,请重新输入。。。n");
			continue;
		}
		//开始游戏
		StartGame();

	}
	printf("扫雷游戏结束n");
	return 0;
}

 头文件utili.h

#ifndef _UTILI_H_
#define _UTILI_H_

#include
#include


#endif

头文件game.h

#ifndef _GAME_H_
#define _GAME_H_

#include"utili.h"

#define EXIT 0
#define PLAY 1

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

#define GAME_LEVEL 20

void InitBoard(char board[ROWS][COLS],char set);

void DisplayBoard(char board[ROWS][COLS], int row, int col);

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

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

void StartGame();

//开始游戏
void StartGame();

#endif

游戏函数game.c

功能:实现定义棋盘->显示棋盘->埋雷->扫雷全过程

#include"game.h"

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

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <= row; ++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");
		}
}

void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = GAME_LEVEL;
	srand(time(0));
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x - 1][y] + 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, y;
	int win = 0;
	while (win < row * col - GAME_LEVEL)
	{
		printf("输入扫雷的位置:>n");
		scanf_s("%d %d", &x, &y);
		if ((x<1 || x>row) || (y<1 || y>col))
		{
			printf("wrong,请重新输入。n");
			continue;
		}
		if (mine[x][y] == '1')
		{
			DisplayBoard(mine, ROW, COL);

			printf("game overn");
			break;
		}
		int count = GetMineCount(mine, x, y);
		show[x][y] = count + '0';
		win++;
		system("cls");
		DisplayBoard(show, ROW, COL);
	}

	if (win >= row * col - GAME_LEVEL)
	{
		DisplayBoard(mine, ROW, COL);

		printf("you are winner.n");
	}


}

void StartGame()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];

	InitBoard(mine, '0');
	InitBoard(show, '*');

	SetMine(mine,ROW,COL );

	DisplayBoard(show, ROW, COL);

	FindMine(mine, show, ROW, COL);
}

 运行结果

总是一点就炸55

开始愉快的游戏吧! 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934184.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号