c语言-扫雷小游戏的实现

game.h 头文件

#ifndef _GAME_H_
#define _GAME_H_
#include
#include
#include

#define ROW 9         //定义界面行列
#define COL 9

#define ROWS ROW+2  //防止数组越界,行列加2
#define COLS COL+2

#define MINE_NUM 10   //设置雷的数量
//初始化二维数组
void Initboard(char board[][COLS], int rows, int cols, char set);
//打印出游戏界面
void Showboard(char board[][COLS],int row,int col);
//设置雷
void Setmine(char board[][COLS], int row, int col);
//扫雷
void Findmine(char mine[][COLS], char mineinfo[][COLS], int row, int col);

#endif

game.c

#include"game.h"
void Initboard(char board[][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 Showboard(char board[][COLS], int row, int col) {
	int i = 0;
	int j = 0;
	printf("===================\n");//打印边框
	for (i = 0; i <= row; 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");
	}
	printf("==================\n");
}	
void Setmine(char mine[][COLS], int row, int col) {
	int x;
	int y;
	int count = 0;
	while (count < MINE_NUM) {
		x = rand() % row + 1;
		y = rand() % col + 1;//设定随机种子
		if (mine[x][y] == '0') {
			mine[x][y] = '1';//使用1统计雷的数量
			count++;
		}
	}
}
static int  Getminecount(char mine[][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 + 1][y + 1] + mine[x + 1][y] +
		mine[x + 1][y - 1] + mine[x][y - 1] - 8 * '0';//将数字字符转化成数字
}
void Findmine(char mine[][COLS], char mineinfo[][COLS], int row, int col) {
	int x;
	int y;
	int count = 0;
	while (count < row*col - MINE_NUM) {
		printf("请输入坐标:");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9) {//输入坐标限制
			if (mine[x][y] == '1') {
				printf("你被炸死了\n");
				Showboard(mine, row, col);//展示出整体界面
				break;
			}
			else {
				int ret = Getminecount(mine,x,y);//统计当前位置周围有几颗雷
				mineinfo[x][y] = ret + '0';
				Showboard(mineinfo, row, col);//继续打印出界面,游戏继续
				count++;//统计非雷坐标点,给定限制
			}
		}
		    else {
			printf("坐标不合法\n");
		    }
	}

}

test.c

#include"game.h"
void menu() {//打印一个菜单
	printf("*********扫雷********\n");
	printf("*********************\n");
	printf("*********1.play******\n");
	printf("*********0.exit******\n");
	printf("*********************\n");
	printf("*********************\n");
}
void game() {
	char mine[ROWS][COLS] = { 0 };
	char mineinfo[ROWS][COLS] = { 0 };

	Initboard(mine, ROWS,COLS, '0');
	Initboard(mineinfo, ROWS,COLS, '*');
	Showboard(mineinfo, ROW, COL);
	Setmine(mine, ROW, COL);
	Findmine(mine, mineinfo, ROW, COL);
}
int main() {
	int input = 0;
	do {
		menu();//调用菜单函数
		printf("请输入你的操作:");
		scanf("%d", &input);
		switch(input){
		case 1:
			game();//调用game函数,开始游戏
			break;
		case 0:
			printf("游戏退出\n");
			break;
		default:
			printf("输入有误,请重新输入\n");
			break;
		}
	} while (input);
	system("pause");
	return 0;
}

你可能感兴趣的:(c语言)