天天看点

C语言实现扫雷小游戏

C语言实现扫雷小游戏

通过实现扫雷小游戏来提高自己的编程能力,是对数组知识点的巩固与强化;

首先创建3个文件写代码,以保证条理清晰;1,头文件(game.h),2.game.c 和test.c 文件

1.game.h 文件主要是预处理信息和函数所用头文件。

#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 board[ROWS][COLS],int row ,int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
           

2.game.c 文件是对游戏函数的实现

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"



void InitBoard(char board[ROWS][COLS], int rows, int 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 board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;//1-9
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}

}
//'0'-'0'=0
//'1'-'0'=1
//assica码表转换原理
int get_mine_count(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, y = 0;
	int win = 0;
	//9*9-10=71
	while (win<row * col - EASY_COUNT) {
		printf("请输入要查雷的坐标:>");
		scanf("%d%d",&x,&y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			//坐标合法
			//1.踩雷
			if (mine[x][y] == '1') {
				printf("很遗憾,你被雷炸死了\n");
				DisplayBoard(mine,row,col);
				break;
			}
			else {//不是雷
				//计算xy坐标周围有多少雷
				int count = get_mine_count(mine,x,y);
				show[x][y] = count + '0';
				DisplayBoard(show,row,col);
				win++;
			}
		}
		else {
			printf("输入非法坐标,请重新输入!\n");
		}
	}if (win == row * col - EASY_COUNT) {
		printf("恭喜你,排雷成功!\n");
		DisplayBoard(mine, row, col);
	}

}

           

test.c主要写扫雷游戏的主体框架代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
menu() {
	printf("******************************\n");
	printf("*******1.PLAY  0.exit*********\n");
	printf("******************************\n");

}
void game() {
	//1.布置雷的信息
	char mine[ROWS][COLS] = {0};
	//2.排出雷的信息
	char show[ROWS][COLS] = {0};
	//初始化
	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);
	//扫雷
	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:
			game();
			break;
		case 2:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入错误,请重新选择\n");
			break;
		}


	} while (input);
}

int main() {
	test();
	return 0;
}

           

*注意事项

刚开始的时候不能向玩家展示地雷棋盘,而且注意里面assca码的转换,因为一开始里面存的是字符型,要转化为int 型

‘0’-‘0’=1

‘3’-‘0’=3

谢谢大家,觉得有用的话就点个赞吧!

继续阅读