当前位置: 首页 > article >正文

扫雷游戏代码分享(c基础)

hi , I am 36.

代码来之不易👍👍👍

创建两个.c    一个.h

1:test.c

#include"game.h"

void game()
{
	//创建数组
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	char temp[ROWS][COLS] = { 0 };
	//初始化数组
	Iniarr(mine, ROWS, COLS, '0');
	Iniarr(show, ROWS, COLS, '*');
	Iniarr(temp, ROWS, COLS, '0');


	//选择模式
	mold(mine, ROW, COL);

	//打印出来
	/*Minebord(mine, ROW, COL);*/
	Showbord(show, ROW, COL);

	//排查
	int flag = 1;
	while (flag)
	{
		/*Minebord(mine, ROW, COL);*/

		flag = Tell__count(mine, show, temp,ROWS, COLS);
		
		Showbord(show, ROW, COL);
		int x = 0;
		//判断胜利
		if (x = Iswin(mine, show, ROW, COL)) break;
		//是否标记
		if (flag) 	
		{
			Isflag(show, ROW, COL);
			Showbord(show, ROW, COL);
		}
		
	}
	if (flag == 0)
	{
		Minebord(mine, ROW, COL);
		printf("你输了\n\n\n");
		Sleep(2000);
	}
	else
	{
		printf("\nCongratulations!!!\a\a\n");
		printf("Congratulations!!!\a\a\n");
		printf("Congratulations!!!\a\a\n");
		printf("Congratulations!!!\a\a\n");
		printf("Congratulations!!!\a\a\n");
		printf("Congratulations!!!\a\a\n");

	}
}


int main()
{

	//设置种子值
	srand((unsigned int)time(NULL));

	Myintub();//介绍
	menu();//菜单
	int n = 0;
	do
	{


		scanf("%d", &n);
		switch (n)
		{
		case 1:game(); printf("请问是否继续?\n"); menu(); break;
		case 0:system("cls"); Myintuo(); break;
		default:system("cls"); printf("该值无效 请重新输入\n"); Sleep(1000); system("cls"); menu(); break;
		}

	} while (n);




	return 0;
}

2: game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2

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




void Myintub();

void Myintuo();

void menu();

void Iniarr(char arr[ROWS][COLS], int rows, int cols, int set);

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

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

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

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

int  Tell__count(char arr[ROWS][COLS], char show[ROWS][COLS], char temp[ROWS][COLS], int rows, int cols);

int  print(char mine[ROWS][COLS], char show[ROWS][COLS], char temp[ROWS][COLS], int rows, int cols,int x, int y);

int Iswin(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

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

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

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

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

3:game.c

#include"game.h"

void Myintub()
{
	printf("hi, I am 36\n");
	printf("let us begine \n");

	Sleep(1500);
	system("cls");
}

void Myintuo()
{
	printf("hi, I am 36.\n");
	printf("bye bye \n");

	Sleep(1500);
}

void menu()
{
	printf("**** 扫雷游戏 ****\n");
	printf("******************\n");
	printf("**** 1  play *****\n");
	printf("**** 0  exit *****\n");
	printf("**** 扫雷游戏 ****\n");
	printf("请选择:>");

}

void Iniarr(char arr[ROWS][COLS], int rows, int cols, int set)
{

	int i = 0;
	int j = 0;

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)

			arr[i][j] = set;
	}

}

void Minebord(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("*** 扫雷游戏 ******\n");
	for (j = 0; j <= row; j++)
	{
		printf("%d ", j);
	}
	printf("\n");

	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);

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

void Showbord(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;

	printf("\n*** 扫雷游戏 ******\n\n");

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

	


	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);

		for (j = 1; j <= col; j++)
			printf("%c ", arr[i][j]);
		printf("\n");
	}

	printf("\n*** 扫雷游戏 ******\n");
}

void mold(char arr[ROWS][COLS], int row, int col)
{


	printf("**** 扫雷游戏 ****\n");
	printf("**** 0  简单 *****\n");
	printf("**** 1  中等 *****\n");
	printf("**** 2  困难 *****\n");
	printf("**** 扫雷游戏 ****\n");
	int n = 0;
	int flag = 0;
	do
	{
		flag = 0;
		printf("请选择难度:>");
		scanf("%d", &n);
		system("cls");
		switch (n)
		{
		case 0:Ini__my__count(arr, ROW, COL, n); break;
		case 1:Ini__my__count(arr, ROW, COL, n); break;
		case 2:Ini__my__count(arr, ROW, COL, n); break;
		default: printf("该值无效 请重新输入\n"); flag = 36; break;
		}
	} while (flag);
}

void Ini__my__count(char arr[ROWS][COLS], int row, int col,int n)
{

	//雷的个数
	int a = 0;
	a = (n + 1) * 10;
	//创建坐标
	int x = 0;
	int y = 0;

	while (a)
	{
		x = rand() % row  + 1;
		y = rand() % col  + 1;
		if (arr[x][y] == '0')
		{
			
			arr[x][y] = '1';
			
			a--;
		}
	}


	//判断
	/*for (x = 0; x < row + 2; x++)
	{
		for (y = 0; y < col + 2; y++)

			printf("%d %d %c\n",x, y, arr[x][y]);
	}*/

}

int Tell__count(char mine[ROWS][COLS], char show[ROWS][COLS], char temp[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	int flag = 0;
	do
	{
		printf("请输入排查坐标:>");
		scanf("%d %d", &x, &y);
		if (x == 36 && y == 36)
		{
			flag = 1;
			Minebord(mine, ROW, COL);
		}
		else if (x < 1 || x>9 || y < 1 || y>9)
		{
			printf("请输入合法值\n");
			flag = 1;

		}
		else if (show[x][y] != '*'&& show[x][y] != '?'&& show[x][y] != '$')
		{
			printf("该坐标已被排查,请重新输入\n");
			flag = 1;

		}
		else
			flag = 0;
		
	} while (flag);
	 return print(mine, show,temp, rows, cols, x, y);

}

int  print(char mine[ROWS][COLS], char show[ROWS][COLS], char temp[ROWS][COLS], int rows, int cols, int x, int y)
{

	//若是雷返回0
	if (mine[x][y] == '1')		
		return 0;

	//不是雷返回1
	//打印个数
	else
	{
		int i = 0;
		int j = 0;
		temp[x][y] = '1';

		//创建计数变量
		int count = 0;

		for (i = -1; i <= 1; i++)
			for (j = -1; j <= 1; j++)
				if (mine[x + i][y + j] == '1')
					count++;
		if (count != 0)
		{
			show[x][y] = count + '0';
			return 1;
		}
		else
		{
			show[x][y] = ' ';
			for (i = -1; i <= 1; i++)
			{
				for (j = -1; j <= 1; j++)
				{
					if (x + i <= 9 && x + i >= 1 && y + j <= 9 && y + j >= 1 && temp[x + i][y + j] == '0')
					{
						print(mine, show, temp, rows, cols, x + i, y + j);

					}

				}
			}
			return 1;

		}
	}


}

int Iswin(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{

	int i = 0;
	int j = 0;
	int a = 0;
	int b = 0;


	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
			if (mine[i][j] == '1')
				a++;

	}

	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
			if (show[i][j] == '*')
				b++;

	}
	if (a == b)
		return 36;
	else return 0;
}

void Isflag(char arr[ROWS][COLS], int row, int col)
{


	int n = 0;
	do
	{
		printf("\n*****************\n");
		printf("请问是否标记操作?\n");
		printf("***0   exit    **\n");
		printf("** 1   $       **\n");
		printf("** 2   ?       **\n");
		printf("** 3   *       **\n");

		printf("请选择:>");
		scanf("%d", &n);
		switch (n)
		{
		case 0:	system("cls");break;
		case 1:Flag1(arr, row, col); break;
		case 2:Flag2(arr, row, col); break;
		case 3:Flag3(arr, row, col); break;
		default:printf("该值无效\n请重新输入:>"); break;
		}

	} while (n);


}

void Flag1(char arr[ROWS][COLS], int row, int col)
{
	printf("请输入坐标:>");
	int x = 0;
	int y = 0;
	
		scanf("%d %d", &x, &y);
		if (arr[x][y] == '*')
		{
			arr[x][y] = '$';
			Showbord(arr, row, col);
		}
}

void Flag2(char arr[ROWS][COLS], int row, int col)
{
	printf("请输入坐标:>");
	int x = 0;
	int y = 0;

	scanf("%d %d", &x, &y);
	if (arr[x][y] == '*')
	{
		arr[x][y] = '?';
		Showbord(arr, row, col);
	}
}

void Flag3(char arr[ROWS][COLS], int row, int col)
{
	printf("请输入坐标:>");
	int x = 0;
	int y = 0;

	scanf("%d %d", &x, &y);
	if (arr[x][y] == '$' || arr[x][y] == '?')
	{
		arr[x][y] = '*';
		Showbord(arr, row, col);

	}

}


http://www.kler.cn/a/393770.html

相关文章:

  • Linux网络——网络初识
  • Docker compose部署portainer
  • Android Studio更新成2024.1.2版本后旧项目Gradle配置问题
  • 群控系统服务端开发模式-应用开发-前端个人信息功能
  • 【操作系统】守护进程
  • apache2配置多站点
  • 观察者模式 vs 不使用观察者模式:商品库存变化的通知
  • Spring框架之责任链模式 (Chain of Responsibility Pattern)
  • GDSC、CTRP数据库学习
  • ApiSmart-QWen2.5 coder vs GPT-4o 那个更强? ApiSmart 测评
  • 使用Java爬虫获取淘宝商品类目API返回值
  • Rust学习(一):初识Rust和Rust环境配置
  • Kafka Eagle 安装教程
  • ue5 蓝图学习(一)结构体的使用
  • 什么是 WPF 中的转换器?如何自定义一个值转换器?
  • 06-form-serialize插件的使用、案例
  • redis实现消息队列的几种方式
  • Swift 类型转换
  • LaTeX之四:如何兼容中文(上手中文简历和中文论文)、在win/mac上安装新字体。
  • session 的工作原理
  • 使用 Python 流式 Websocket 传输 Binance 订单更新 附代码
  • Vue3入门介绍及快速上手
  • Autosar CP 基于CAN的时间同步规范导读
  • DA217应用笔记
  • TypeScript在现代前端开发中的应用
  • C哈的刷题计划之输出数字螺旋矩阵(1)