简单扫雷游戏(C语言)

扫雷游戏怎么玩?

给定一个范围,踩雷了游戏就结束,没有踩雷,游戏继续,且显示周围的雷个数。

思路

1、游戏封面

2、创建2个数组,show数组用来存放显示的‘*‘,mine数组用来存放周围的雷的个数。

3、给玩家显示show数组的内容

4、在mine数组中建立雷

5、扫雷(输入坐标,有雷就结束)

#include<stdio.h>
#include<stdlib.h>//产生随机数
#include<time.h>//使得每一次的随机数不同

#define EASYCOUNT 10//雷的个数
#define ROW 9//行
#define COL 9//列
#define ROWS  ROW+2//行多2行是因为要判断周围雷的个数,特别针对在周围的下标,多余的两行设置为‘0’,‘0’不是雷,‘1’为雷
#define COLS COL+2//同上
void InitBoard(char board[ROWS][COLS], int row, int col,char set);//赋值
void DisplayBoard(char board[ROWS][COLS], int row, int col);//显示
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void SetMine(char mine[ROWS][COLS], int row, int col);

游戏封面

void menu() {
	printf("********************************\n");
	printf("********************************\n");
	printf("**********   1.play   **********\n");
	printf("**********   0.exit   **********\n");
	printf("********************************\n");
	printf("********************************\n");
}

 

int main() {
	
	int input = 0;
	srand((unsigned int)time(0));//随机函数,使得每一次产生不同的随机数
	do {
		menu();  //封面显示
		printf("请选择:>");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();//游戏的开始
			printf("扫雷\n"); break;
		case 0:
			printf("退出游戏\n"); break;
		default:
			printf("选择错误,重新选择\n"); break;
		}
	} while (input);
	return 0;

 2、建立数组和游戏流程

void game() {
	char mine[ROWS][COLS];//存放周围雷的个数
	char show[ROWS][COLS];存放雷
	InitBoard(mine, ROWS, COLS, '0');//赋值
	InitBoard(show, ROWS, COLS, '*');
	//DisplayBoard(mine, ROW, COL);(用于看是否运行成功,建立成功mine数组)
	DisplayBoard(show, ROW, COL);//显示show数组的内容
	SetMine(mine, ROW, COL);//创建雷
	//DisplayBoard(mine, ROW, COL);(用于看是否运行成功,看雷是否建立成功)
	FindMine( mine,  show,  ROW,  COL);

}

3、给两个数组分别赋值,并且显示

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

4、创建雷

void SetMine(char mine[ROWS][COLS], int row, int col) {
	int count = EASYCOUNT;
	while (count) {
		int x = rand() % row + 1;//使产生的随机数范围在1-9
		int y = rand() % col + 1;
		if (mine[x][y] == '0') {
			mine[x][y] = '1';
			count--;
		}
	}

5、扫雷

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x ;
	int y ;
	int win = 0;
	while (win<row*col-EASYCOUNT) {
		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("坐标非法,重新输入\n");
	}if (win == row * col - EASYCOUNT) {
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

6、返回是否为雷的结果(为雷结束游戏,不为雷返回雷的个数)

int GetMineCount(char mine[ROWS][COLS], int x, int y) {
	int i, j, s = 0;
	for (i = x - 1; i <= x + 1; i++)
		for (j = y - 1; j <= y + 1; j++) {
			if (i == x & j == y)continue;
			else s += mine[i][j] - '0';
		}return s;
}

7、整个程序

game.c

#include<stdio.h>
#include"game.h"
void InitBoard(char board[ROWS][COLS], int row, int col,char set) {
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)board[i][j] = set;
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col) {
	int i, j;
	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[ROWS][COLS], int row, int col) {
	int count = EASYCOUNT;
	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) {
	int i, j, s = 0;
	for (i = x - 1; i <= x + 1; i++)
		for (j = y - 1; j <= y + 1; j++) {
			if (i == x & j == y)continue;
			else s += mine[i][j] - '0';
		}return s;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x ;
	int y ;
	int win = 0;
	while (win<row*col-EASYCOUNT) {
		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("坐标非法,重新输入\n");
	}if (win == row * col - EASYCOUNT) {
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

game.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define EASYCOUNT 10
#define ROW 9
#define COL 9
#define ROWS  ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int row, int col,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void SetMine(char mine[ROWS][COLS], int row, int col);

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];
	char show[ROWS][COLS];
	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);

}

int main() {
	
	int input = 0;
	srand((unsigned int)time(0));
	do {
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			printf("扫雷\n"); break;
		case 0:
			printf("退出游戏\n"); break;
		default:
			printf("选择错误,重新选择\n"); break;
		}
	} while (input);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值