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

embedded/2024/11/15 4:47:56/

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;}elseflag = 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)
{//若是雷返回0if (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.ppmy.cn/embedded/137675.html

相关文章

Linux命令学习,git命令

Linux系统&#xff0c;Git是一个强大的版本管理系统&#xff0c;允许用户跟踪代码的更改、管理项目历史以及与他人协作。 Linux Git命令&#xff1a; 初始化仓库:当前目录创建一个Git仓库&#xff0c;生成.git隐藏目录存储版本历史和其他Git相关的元数据。 git init 克隆仓库…

SDL打开YUV视频

文章目录 问题1&#xff1a;如何控制帧率&#xff1f;问题2&#xff1a;如何触发退出事件&#xff1f;问题3&#xff1a;如何实时调整视频窗口的大小问题4&#xff1a;YUV如何一次读取一帧的数据&#xff1f; 问题1&#xff1a;如何控制帧率&#xff1f; 单独用一个子线程给主线…

GitHub个人主页美化

效果展示 展示为静态效果&#xff0c;动态效果请查看我的GitHub页面 创建GitHub仓库 创建与GitHub用户名相同的仓库&#xff0c;当仓库名与用户名相同时&#xff0c;此仓库会被视作特殊仓库&#xff0c;其README.md&#xff08;自述文件&#xff09;会展示在GitHub个人主页…

[Docker#3] LXC | 详解安装docker | docker的架构与生态

目录 1.LXC容器操作 安装LXC LXC容器操作步骤 2.理论 LXC 是什么&#xff1f; Docker 是什么 Docker 和虚拟机的区别 Docker 和 JVM 虚拟化的区别 Docker 版本 ⭕Docker 官方网站&#xff08;建议收藏&#xff09; Docker 架构 生活案例 Docker 生态 Docker 解决…

深度学习笔记(一)更新中

一、监督学习和无监督学习 什么是监督学习&#xff1f; 就是我们给学习算法一个数据集。这个数据集由“正确答案”组成&#xff0c;然后运用学习算法&#xff0c;算出更多的正确答案。 监督学习包括&#xff1a;回归、分类 什么是回归&#xff1f; 回归这个词的意思是&#…

Go 加密算法工具方法

常用的工具方法&#xff1a; 1.将字节数组转成16进制字符串&#xff1a; []byte -> string 2.将16进制字符串串转成字节数组&#xff1a; hex string -> []byte 3.16进制字符串大端和小端颠倒 4.字节数组大端和小端颠倒 5.哈希函数 package mainimport ("fmt&…

单网页图库应用Single File PHP Gallery

什么是 Single File PHP Gallery &#xff1f; Single File PHP Gallery 是一个基于单个 PHP 文件的网页图库。您只需将该脚本复制到任何包含图像的目录&#xff0c;即可创建图库。子目录将作为子图库。图像和目录的缩略图会自动生成。使用 Single File PHP Gallery 无需任何配…

正向代理服务器

1 概念 1.1 正向代理概念 正向代理是一个位于客户端和目标服务器之间的代理服务器&#xff08;中间服务器&#xff09;。为了从目标服务器取得内容&#xff0c;客户端向代理服务器发送一个请求&#xff0c;并且指定目标服务器&#xff0c;之后代理向目标服务器转发请求&#…