扫雷(C语言,无雷自阔)

今天写了一个扫雷游戏的代码
大致分为几个部分
首先选择俩个棋盘,一个显示给用户,一个布雷

void InitBoard(char board[][COL], int row, int col, char elem)
{
	int i = 0;
	for (; i < row; i++){
		int j = 0;
		for (; j < col; j++){
			board[i][j] = elem;
		}
	}
}
void SetMine(char mine[][COL], int row, int col)
{
	int n = NUM;
	while (n){
		int x = rand() % (row - 2) + 1;
		int y = rand() % (col - 2) + 1;
		if (mine[x][y] == '1'){
			continue;
		}

		mine[x][y] = '1';
		n--;
	}
}

//然后显示棋盘,对用户输入的坐标显示,并显示坐标附近的雷数

void ShowBoard(char board[][COL], int row, int col)
{
	int i = 1;
	printf("   ");
	for (; i <= col - 2; i++){
		printf(" %-2d|", i);
	}
	printf("\n");
	for (i = 1; i <= col - 2; i++){
		printf("%3s", "----");
	}
	printf("---\n");

	for (i = 1; i <= row - 2; i++){
		printf("%2d|", i);
		int j = 1;
		for (; j <= col - 2; j++){
			printf(" %-2c|", board[i][j]);
		}
		printf("\n");
		for (j = 1; j <= col - 2; j++){
			printf("%3s", "----");
		}
		printf("---\n");
	}
}
char GetNum(char mine[][COL], int x, int y,char board[][COL],int *c)
{
	int i, j;
	board[x][y]=mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + \
		mine[x][y - 1] + mine[x][y + 1] + \
		mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 7 * '0';
    
	if (board[x][y] == '0')
	{
		i = x - 1;
		for (; i <= x + 1; i++)
		{
			for (j=y-1; j <= y + 1; j++)
			{
				if (board[i][j] == '*'&&i >= 1 && j >= 1 && i <= 10 && j <= 10&&(i!=x||j!=y))
				{
					*c=*c-1;
					GetNum(mine, i, j, board,c);
				}
			}
		}
	}
}

//当棋盘上的未之格子与雷数相同时,扫雷成功
一些问题
1.棋盘大小与显示的棋盘不符
一般输入习惯从1开始,而数组下标从零开始,加上检测周围雷数是检测周围8格,所以我们加棋盘横纵各加一行,方便代码书写和用户输入。
2.为什么要自动展开0,怎么展开
为了游戏体验,不断递归,详情见函数getnum。
运行截图如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述代码如下

//主函数
#include "mine.h"

static void Menu()
{
	printf("##################################\n");
	printf("##     欢迎来到我的扫雷游戏     ##\n");
	printf("##################################\n");
	printf("## 1. Play              2. Exit ##\n");
	printf("##################################\n");
	printf("Please Select=> ");
}

int main()
{
	int quit = 0;
	do{
		int select = 0;
		Menu();
		scanf("%d", &select);
		switch (select){
		case 1:
			Game();
			printf("当前游戏已经结束 ... 再来一把?\n");
			break;
		case 2:
			quit = 1;
			printf("再见 ... 欢迎下次来玩!\n");
			break;
		default:
			printf("选择错误 ... 请重新选择\n");
			break;
		}
	} while (!quit);
	system("pause");
	return 0;
}
//源文件
#ifndef _MINE_H_
#define _MINE_H_

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

#pragma warning(disable:4996)

#define ROW 12
#define COL 12

#define NUM 1

void Game();
void InitBoard(char board[][COL], int row, int col, char elem);
void SetMine(char mine[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
char GetNum(char mine[][COL], int x, int y); //'0', '1', '2'


#endif
//实现函数文件
#include "mine.h"

void InitBoard(char board[][COL], int row, int col, char elem)
{
	int i = 0;
	for (; i < row; i++){
		int j = 0;
		for (; j < col; j++){
			board[i][j] = elem;
		}
	}
}
void SetMine(char mine[][COL], int row, int col)
{
	int n = NUM;
	while (n){
		int x = rand() % (row - 2) + 1;
		int y = rand() % (col - 2) + 1;
		if (mine[x][y] == '1'){
			continue;
		}

		mine[x][y] = '1';
		n--;
	}
}
void ShowBoard(char board[][COL], int row, int col)
{
	int i = 1;
	printf("   ");
	for (; i <= col - 2; i++){
		printf(" %-2d|", i);
	}
	printf("\n");
	for (i = 1; i <= col - 2; i++){
		printf("%3s", "----");
	}
	printf("---\n");

	for (i = 1; i <= row - 2; i++){
		printf("%2d|", i);
		int j = 1;
		for (; j <= col - 2; j++){
			printf(" %-2c|", board[i][j]);
		}
		printf("\n");
		for (j = 1; j <= col - 2; j++){
			printf("%3s", "----");
		}
		printf("---\n");
	}
}
char GetNum(char mine[][COL], int x, int y,char board[][COL],int *c)
{
	int i, j;
	board[x][y]=mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + \
		mine[x][y - 1] + mine[x][y + 1] + \
		mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 7 * '0';
    
	if (board[x][y] == '0')
	{
		i = x - 1;
		for (; i <= x + 1; i++)
		{
			for (j=y-1; j <= y + 1; j++)
			{
				if (board[i][j] == '*'&&i >= 1 && j >= 1 && i <= 10 && j <= 10&&(i!=x||j!=y))
				{
					*c=*c-1;
					GetNum(mine, i, j, board,c);
				}
			}
		}
	}
}

void Game()
{
	srand((unsigned long)time(NULL));

	char board[ROW][COL];
	char mine[ROW][COL];

	InitBoard(board, ROW, COL, '*'); //'*'
	InitBoard(mine, ROW, COL, '0');  //'0'

	SetMine(mine, ROW, COL);
	//ShowBoard(board, ROW, COL);
	//ShowBoard(mine, ROW, COL);

	int count = (ROW - 2)*(COL - 2) - NUM;
	do{
		system("cls");
		int x = 0;
		int y = 0;
		ShowBoard(board, ROW, COL);
		printf("请输入你要扫的位置-> ");
		scanf("%d %d", &x, &y);
		if (x < 1 || x > 10 || y < 1 || y > 10){//[1,10]
			printf("你输入的位置是错误的,请重新输入...\n");
			Sleep(1000);
			continue;
		}
		if (board[x][y] != '*'){
			printf("你输入的位置已经被扫过,请重新输入...\n");
			Sleep(1000);
			continue;
		}
		if (mine[x][y] == '0'){
			count--;
			GetNum(mine, x, y,board,&count);
		}
		else{
			printf("你输入的位置(%d, %d),有雷!\n", x, y);
			break;
		}
	} while (count > 0);

	char *result = NULL;
	if (count > 0){
		result = "##  扫雷结束, 你被炸死了:<   ##\n";
	}
	else{
		result = "##扫雷结束,恭喜你,成功啦:> ##\n";
	}
	printf("###############################\n");
	printf("%s", result);
	printf("###############################\n");
	ShowBoard(mine, ROW, COL);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值