扫雷游戏-C语言实现

test.c

#include "game.h"

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

void game() {
	char mine[ROWS][COLS] = { 0 };  //存放布置好的雷的信息
	char show[ROWS][COLS] = { 0 };	//存放排查出的雷的信息
	//初始化数组的元素为指定的内容
	InitBoard(mine,ROWS,COLS, '0');//没有布置雷的时候,都是'0'
	InitBoard(show,ROWS,COLS, '*');//没有排查雷的时候,都是'*'

	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);//展示游戏开始时的扫雷界面


	PutLei(mine, ROW, COL);//随机布置10个雷的位置
	DisplayBoard(mine, ROW, COL);

	char res = 'C';
	do{
		res = SaoLei(mine, show, ROW, COL);//玩家扫雷
		if (res=='S') {
			printf("踩到雷了,游戏结束!\n");
			DisplayBoard(mine, ROW, COL);//展示所有雷的信息
		}
		else if (res == 'W') {
			printf("恭喜你,全部排雷成功!\n");
			DisplayBoard(mine, ROW, COL);//展示所有雷的信息
		}
		else {//展示扫雷界面,用户继续扫雷
			DisplayBoard(show, ROW, COL);
		}
	} while (res == 'C');
}

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

	return 0;
}

game.h

#include <stdio.h>
#include <stdlib.h> //包含srand函数和rand函数
#include <time.h>

#define ROW 9
#define COL 9

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

void InitBoard(char arr[ROWS][COLS],int row,int col, char set);

void DisplayBoard(char arr[ROWS][COLS], int row, int col);

void PutLei(char arr[ROWS][COLS], int row, int col);

char SaoLei(char main[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c

#include "game.h"

//初始化
void InitBoard(char arr[ROWS][COLS], int row, int col, char set) {
	int i = 0;
	int j = 0;
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			arr[i][j] = set;
		}
	}
}

//展示界面
void DisplayBoard(char arr[ROWS][COLS], int row, int col) {
	int i = 0;
	int j = 0;
	printf("***** 扫雷游戏 ****\n");

	for (int j = 0; j <= col; j++) {
		printf("%d ", j);
	}
	printf("\n");

	for (int i = 1; i <= row; i++) {
		printf("%d ", i);
		for (int j = 1; j <= col; j++) {
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
	printf("***** 扫雷游戏 ****\n");
}

//布置10个雷
void PutLei(char arr[ROWS][COLS], int row, int col) {
	int count = MINE_COUNT;
	while (count) {
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0') {
			arr[x][y] = '1';//mine数组中元素为'1'的就是雷
			count--;
		}
	}
}


//统计用户扫雷界面show数组中剩余几个'*'
int remain(char arr[ROWS][COLS],int row,int col) {
	int count = 0;
	for (int i = 1; i <= row; i++) {
		for (int j = 1; j <= col; j++) {
			if (arr[i][j] == '*') {
				count++;
			}
		}
	}
	return count;
}


//玩家扫雷
//返回'D':表示die,踩雷炸死了
//返回'C':表示continue,继续扫雷
//返回'W':表示win,用户赢了
char SaoLei(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col) {
	int x = 0;
	int y = 0;
	while(1){
		printf("请输入扫雷坐标:>");
		scanf_s("%d %d", &x, &y);
		if (x > 0 && x <= row && y > 0 && y <= col) {
			if (show[x][y] == '*') {//输入的坐标内容之前是'*',表示没有占用
				if (mine[x][y] == '1') {//这个坐标内容是雷
					return 'S';//踩雷
				}
				int count = 0;//计数
				int i = 0;
				int j = 0;
				for (i = x - 1; i <= x + 1; i++) {
					for (j = y - 1; j <= y + 1; j++) {
						if (mine[i][j] == '1') {//统计挨着输入坐标周围8个位置的雷的个数
							count++;
						}
					}
				}
				show[x][y] = count + '0';//将雷的个数转化为字符,并赋值给此坐标的内容
				int mine = remain(show, row, col);
				if (mine == MINE_COUNT) {
					return 'W'; //当show数组剩余'*'的个数等于雷的个数时,则用户赢了
				}
				return 'C';//用户继续扫雷
			}
			else {//之前有输入此坐标,需要重新输入别的坐标
				printf("坐标已经占位!");
			}
		}
		else {
			printf("输入坐标非法!");
		}
	}
	return 'C';
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值