当前位置:首页>编程日记>正文

C语言-扫雷游戏

头文件

#ifndef __MINE_H__
#define __MINE_H__#define LINE 10
#define LIST 10
#define ROWS 6
#define COWS 6int game(char UserBoard[LINE+2][LIST+2], char PlayerBoard[LINE][LIST]);
void PrintBoard(char Playerboard[LINE][LIST]);
void MineLay(char UserBoard[LINE + 2][LIST + 2]);
void PrintUser(char UserBoard[LINE + 2][LIST + 2]);
int MineClear(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST]);
void Blast(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST]);
int Counter(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST], int x, int y);#endif   //mine.h

函数文件

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include"mine.h"void MineLay(char UserBoard[LINE + 2][LIST + 2])
{int x = 0, y = 0,i = 0,j = 0;char m = '0';printf("**************************
");printf("******* a: 十个雷 ********
");printf("******* b: 二十雷 ********
");printf("**************************
");printf("请选择难度>:");fflush(stdin);scanf("%c",&m);printf("
");if ('a' == m){j = 10;}elsej = 20;while (i < j){x = rand() % 10 + 1;y = rand() % 10 + 1;if ('0' == UserBoard[x][y]){UserBoard[x][y] = '1';i++;}}}void Blast(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST])//全局输了
{int i = 0, j = 0;for (i = 1; i <=LINE; i++){for (j = 1; j <= LIST; j++){if ('1' == UserBoard[i][j]){PlayerBoard[i - 1][j - 1] = '#';}if ('*' == PlayerBoard[i - 1][j - 1])    //{                                       //PlayerBoard[i - 1][j - 1] = ' ';      //}                                          //}}
}void PrintBoard(char Playerboard[LINE][LIST])
{int i = 0, j = 0;printf("  0 1 2 3 4 5 6 7 8 9 10
");printf("------------------------
");for (i = 0; i < LINE; i++){printf("%2d| ",i+1);for (j = 0; j < LIST; j++){printf("%c ",Playerboard[i][j]);}printf("
");}printf("
");
}int MineClear(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST])
{int x = 0, y = 0,num = 0,win = 0,i =0,j = 0,m = 0,n = 0,p = 0,q = 0,a = 0,b = 0;while (1){printf("请输入扫雷坐标>:");scanf("%d%d", &x, &y);a = x;b = y;i = x;j = y;m = x;n = y;p = x;q = y;if ((x > 0 && x <= 10) && (y > 0 && y <= 10)){if ('1' == UserBoard[x][y]){PlayerBoard[x-1][y-1] = '#';Blast(UserBoard, PlayerBoard);PrintBoard(PlayerBoard);printf("啊!!!!你被炸死啦!!!!
");return 0;}if ('0' == UserBoard[x][y]){for (a = x; a >= 1; a--,b = y)//向上向左{if (0 != Counter(UserBoard, PlayerBoard, a, b))break;for (b = y; b >= 1; b--)   自加自减和for循环的初值一定要注意哦,我一开始的代码是	for (a = x; a >= 1 && '0' == UserBoard[a][b]; a--)for(; b >= 1 && '0' == UserBoard[a][b]; b--)此时{                                                        //此时,当a--时,b的值已经是上次的0了,所以下面的每次都不执行,因为判断条件是b>=1if (0 != Counter(UserBoard, PlayerBoard, a, b))break;}}for (i = x; i >= 1; i--,j = y)//向上向右数{if (0 != Counter(UserBoard, PlayerBoard, i, j))break;for (j = y; j <= LIST; j++)     //for循环里面尽量不要既执行又判断{if (0 != Counter(UserBoard, PlayerBoard, i, j))break;}}for (m = x; m <= LINE; m++,n = y)//向下向左{if (0 != Counter(UserBoard, PlayerBoard, m, n))break;for (n = y; n >= 1; n--){if (0 != Counter(UserBoard, PlayerBoard, m, n))break;}				}for (p = x; p <= LINE; p++,q = y)//向下向右{if (0 != Counter(UserBoard, PlayerBoard, p, q))break;for (q = y; q <= 4; q++){if (0 != Counter(UserBoard, PlayerBoard, p, q))break;}				}PrintBoard(PlayerBoard);//PrintUser(UserBoard);  win = 0;                 //一开始的时候这个地方没有写win = 0;这样就出现了一个问题,就是,上次输入一个坐标循环的时候win已经有大于0的值,导致循环完的最终结果不能小于3for (x = 0; x < LINE; x++){for (y = 0; y < LIST;y++)if ('*' == PlayerBoard[x][y]){win++;	}}if (win <= 3){for (x = 0; x < LINE; x++){for (y = 0; y < LIST; y++)if ('*' == PlayerBoard[x][y]){PlayerBoard[x][y] = '#';}}printf("扫雷成功,你赢了!!!
");return 0;}}}}
}int Counter(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST], int x, int y)
{int num = 0;num = (UserBoard[x - 1][y - 1] - '0') +(UserBoard[x - 1][y] - '0') +(UserBoard[x - 1][y + 1] - '0') +(UserBoard[x][y - 1] - '0') +(UserBoard[x][y + 1] - '0') +(UserBoard[x + 1][y - 1] - '0') +(UserBoard[x + 1][y] - '0') +(UserBoard[x + 1][y + 1] - '0');if (0 == num){PlayerBoard[x - 1][y - 1] = '0';}else{PlayerBoard[x - 1][y - 1] = num + '0';}return num;
}int game(char UserBoard[LINE+2][LIST+2], char PlayerBoard[LINE][LIST])
{/*int x = 0, y = 0;int i = 0, j = 0;*///PrintBoard(PlayerBoard);MineLay(UserBoard);PrintBoard(PlayerBoard);//PrintUser(UserBoard);                           ///return  MineClear(UserBoard, PlayerBoard);return 0;
}void PrintUser(char UserBoard[LINE + 2][LIST + 2])
{int i = 0, j = 0;printf("   0 1 2 3 4 5 6 7 8 9 10
");for (i = 0; i < LINE + 2; i++){printf("%2d ", i );for (j = 0; j < LIST + 2; j++){printf("%c ", UserBoard[i][j]);}printf("
");}printf("
");
}


测试函数文件

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include"mine.h"
//1.第一次扫雷,扫一大片2.玩家选择难度3.插标记void menu()
{printf("***********************
");printf("******  1.play  *******
");printf("******  0.exit  *******
");printf("***********************
");printf("请选择相应的数字>:");
}int main()
{char PlayerBoard[LINE][LIST];char UserBoard[LINE + 2][LIST + 2];int input = 1, a = 0;srand((unsigned int)time(NULL));while (input){memset(PlayerBoard, '*', sizeof(char)*LINE*LIST);memset(UserBoard, '0', sizeof(char)*(LINE+2)*(LIST+2));menu();scanf("%d", &input);printf("
");switch (input){case 1:a = game(UserBoard, PlayerBoard);break;case 0:break;    //此处break的作用是结束switchdefault:printf("你的输入不符合要求,请重新输入>:"); break;}if (0 == input){break;      //此处break的作用是跳出while循环,即结束游戏}}printf("游戏结束,欢迎再次使用!!!
");system("pause");return 0;
}




http://www.coolblog.cn/news/927d0b545ea68b01.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • 2019-9
  • jquery 使用小技巧
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • linux批量创建用户和密码
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • js常用阻止冒泡事件
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • 【设计模式】 模式PK:策略模式VS状态模式
  • JS实现-页面数据无限加载
  • CSS小技巧——CSS滚动条美化
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里巴巴分布式服务框架 Dubbo
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 两张超级大表join优化
  • Arm芯片的新革命在缓缓上演
  • 第九天函数
  • Linux软件安装-----apache安装
  • HDU 5988 最小费用流
  • 《看透springmvc源码分析与实践》读书笔记一
  • nagios自写插件—check_file
  • python3 错误 Max retries exceeded with url 解决方法
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • 通过Spark进行ALS离线和Stream实时推荐
  • 行为模式之Template Method模式