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

简单的扫雷游戏

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

简单的扫雷游戏

采用多文件来编写:

头文件:

#pragma once
#include
#include
#include
#include
#include
#define ROW 12
#define COL 12
#define NUM 20  //雷的个数
#define BOOM  '1'   //雷为1
#pragma warning (disable:4996)

extern void Game();

主函数:

#include"test.h"

int main() {
	srand((unsigned long)time(NULL));//种随机数种子
	int quit = 0;
	while (!quit) {
		Menu();
		printf("请做出你的选择:");
		int select = 0;
		scanf("%d", &select);
		switch (select) {
		case 1:
			Game();
			break;
		case 2:
			quit = 1;
				break;
		default:
			printf("输入有误,请重新输入n");
			break;
		}
	}
	printf("ByeBye!n");
	return 0;
}


函数调用:

#include"test.h"
void Menu() {
	printf("#####################################n");
	printf("#####1.play     2.exit    ###########n");
	printf("#####################################n");
}
 void SetMines(char mine_board[][COL], int row, int col) {
	int i = 0;
	while (i < NUM) {
		int _x = rand() %(row-2) + 1;
		int _y = rand() % (col-2) + 1;
		if (mine_board[_x][_y] == BOOM) {
			continue;
		}
		mine_board[_x][_y] = BOOM;
		i++;
	}
}
static void ShowBoard(char show_board[][COL], int row, int col) {
	for (int i = 1; i <= row-2; i++) {
		printf("%5d", i);
		}
	printf("n");
	printf("  ");
	for (int k = 1; k <= row-2; k++) {
		printf("-----");
	}
    printf("-n");
	for (int i = 1; i <= row - 2; i++) {
		printf("%2d|", i);
		for (int j=1;j<=col-2;j++) {
			printf(" %c | ",show_board[i][j]);
		}
		printf("n");
		printf("  ");
		for (int k = 1; k <= row-2; k++) {
			
			printf("-----");
		}
		printf("-n");
	}
}
static int CountMines(char mine_board[][COL],int x,int y) {

	return mine_board[x - 1][y - 1]+mine_board[x - 1][y]+mine_board[x - 1][y + 1]+
		mine_board[x][y - 1]+mine_board[x][y + 1]+mine_board[x + 1][y - 1]+
		mine_board[x + 1][y]+mine_board[x + 1][y + 1] - 8 * '0';

}
void Game() {
	
	char show_board[ROW][COL];
	char mine_board[ROW][COL];
	memset(show_board, '*', sizeof(show_board));//初始化数组,初始化为*
	memset(mine_board, '0', sizeof(mine_board));//初始化数组,初始化为0
	SetMines(mine_board,ROW,COL);
	int total = (ROW-2)*(COL-2)-NUM;   //总共扫多少次
	int clear = 0;    //已经扫过的
	while (1) {
		system("cls");//清屏
		int x = 0;
		int y = 0;
		ShowBoard(show_board,ROW,COL);
		printf("请输入坐标n");
		scanf("%d,%d", &x, &y);
		
		if (!(x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2)) {
			printf("扫雷的位置有问题,请重新输入:n");
			continue;
		}
		if (show_board[x][y] != '*') {
			printf("扫雷的位置已经被排除!n");
			continue;
		}
		if (mine_board[x][y] == '1') {
			ShowBoard(mine_board, ROW, COL);
			printf("对不起,你被炸死了!n");
			break;
		}
		else {
			int count = CountMines(mine_board,x,y);
			show_board[x][y] = count+'0';
			clear++;
		}
		if (clear >= total) {
			printf("恭喜你,游戏通过!n");
			break;
		}
	}

}

运行结果:

 

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

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

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