扫雷游戏

对于扫雷游戏而言, 需要创建三个文件: game.h(用于声明函数), game.c(用于定义函数), test.c(用于运行游戏)

该游戏主要就是: 扫雷棋盘的初始化, 打印棋盘,  布置雷和排查雷这四种功能 

game.h中存放的是整个游戏中所需要的函数的声明(函数需要先声明, 再使用), 包括一些库函数(避免在两个 .c  文件中重复调用), 宏的定义.

#pragma once
#define ROW 9
#define COL 9

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

#define EASY_COUNT 10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


// 函数声明

// 初始化扫雷棋盘
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 mine[ROWS][COLS], int row, int col);
// 排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c是和game.h相对应的源文件, 里面是对于此次游戏使用的所有函数的定义

由于game.c调用了自定义头文件game.h中的东西, 所以要调用game.h这个头文件

game.c中包括对于扫雷棋盘的初始化, 打印棋盘,  布置雷和排查雷这四种功能 , 是扫雷游戏的实施文件

#define _CRT_SECURE_NO_WARNINGS
#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 row, int col) {
	int i = 0;
	int j = 0;
	// 打印列号
	for (int i = 0; i <= col; i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++) {
		// 打印行号
		printf("%d ", i);
		for (j = 1; j <= col; j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

// 布置雷
void SetMine(char mine[ROWS][COLS], int row, int col) {
	// 雷的个数
	int count = EASY_COUNT;
	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] +
		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("请输入排查的坐标: ");
		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] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		} else {
			printf("请注意! 坐标有误, 重新输入!");
		}
	}
	if (win == row * col - EASY_COUNT) {
		printf("恭喜你, 排雷成功!\n");
		DisplayBoard(mine, ROW, COL);
	}
}

对于test.c这个文件而言,是用于运行游戏和测试游戏的.

这个源文件相当于是为扫雷游戏打好一个框架, 然后再在game.h 和 game.c 中去对框架进行填充

从game.c中不断调用函数去实现各个功能

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

void menu() {
	printf("**************************\n");
	printf("*** 1. play    0. exit ***\n");
	printf("**************************\n");
}
void game() {
	// 存放布置好的雷的信息
	char mine[ROWS][COLS] = { 0 };
	// 存放排查出的雷的信息
	char show[ROWS][COLS] = { 0 };
	// 初始化扫雷的棋盘: mine中初识话为'0', show中初始化为'*'
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	// 打印扫雷棋盘(带上行和列)
	//DisplayBoard(mine, ROW, COL);
	//DisplayBoard(show, ROW, COL);
	// 布置雷
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	// 排查雷
	FindMine(mine, show, ROW, COL);
}

void test() {
	int input = 0;
	srand((unsigned int)time(NULL));
	// 可以一直玩这个游戏
	do {
		menu();
		printf("请选择: ");
		scanf("%d", &input); // 玩家进行选择
		// 玩家选择后, 游戏就应该给出相应的提示
		switch (input) {
		case 1:
			printf("扫雷游戏开始!\n");
			game();
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("选择错误!\n");
			break;
		}
	} while (input);
}
int main() {
	test();
	return 0;
}

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1扫雷游戏1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值