C程序,简单模仿扫雷游戏,10行 10列
2017-10-19 23:32
197 查看
要求:
1>第一次下子,不炸死。2>坐标周围没雷,可以实现展开。
实现
1.用一个二维字符数组mine[ROWS] [COLS]来存储雷,现在我们用字符1来表示有雷,字符0表示无雷。再用一个二维字符数组show[ROWS][COLS]将所有的元素初始化为*,并打印作为展现给玩家的。同时用show数组来表示对应的mine数组中周围雷即字符0的个数。对于边缘的格子无法计算雷的个数,因此只需再增加2行2列即可,即ROWS=COLS=12 。2.第一次下子,不炸死
在设置雷前先让玩家输入一个坐标(x,y),再设置雷,然后再计算周围雷的个数,即相当于第一次无效。
3.坐标周围没雷,可以实现展开。
可用递归函数实现。
4.此游戏还存在一个问题,若确定一个坐标不是雷,则可重复输入此坐标直到玩家胜利。因为计数器自增的条件是只要满足x,y对应的位置为非雷。此时只需增加一个循环即可。
while (show[x][y]!='*') { printf("输入非法,请重新输入:"); scanf("%d%d", &x, &y); printf("\n"); }
1 game.h
game.h
#define _CRT_SECURE_NO_WARNINGS #ifndef __GAME_H__ #define __GAME_H__ #include <stdlib.h> #include <stdio.h> #include <time.h> #define ROW 10 #define COL 10 #define ROWS ROW+2 #define COLS COL+2 #define COUNT 10 void InitBoard(char arr[ROWS][COLS], int row, int col, char set); void Display(char arr[ROWS][COLS], int row, int col); void Setmine(char arr[ROWS][COLS], int row, int col); void blank(char mine[ROWS][COLS], char show[ROWS][COLS] ,int x, int y); #endif //__GAME_H__
2 test.c
test.c
#include "game.h" int GetMineCount(char mine[ROWS][COLS], int x, int y)//求mine[x][y]周围8个元素中雷的总个数 { return mine[x-1][y]+mine[x-1][y-1]+mine[x][y-1]+mine[x+1][y-1]+mine[x+1][y]+mine[x+1][y+1]+mine[x][y+1]+mine[x-1][y+1]-7*'0'; } void game() { int ret = 0; int count = 0; int x = 0; int y = 0; char show[ROWS][COLS]; char mine[ROWS][COLS]; InitBoard(mine, ROWS, COLS, '0');//初始化数组,全置为字符0 InitBoard(show, ROWS, COLS, '*');//初始化数组 Display(show, ROWS, COLS);//打印数组,注意打印的行和列都是序号1到10的数组元素 Display(mine, ROWS, c030 COLS);//打印雷的具体分布,便于调试 flag: printf("请输入坐标(x,y)"); scanf("%d%d", &x,&y); if (x>=1&&x<=10&&y>=1&&y<=10)//对输入的合法性进行检验 { Setmine(mine, ROWS, COLS);//随机设置雷的位置 show[x][y] = GetMineCount(mine, x, y);//将(x,y)处*赋值为雷的个数 blank(mine, show, x, y);//实现展开的函数,递归函数 } else { printf("输入有误\n"); goto flag; } Display(show, ROWS, COLS); Display(mine, ROWS, COLS); while (count<ROW*COL-COUNT) { printf("请输入坐标(x,y)"); scanf("%d%d", &x,&y); while (show[x][y]!='*') { printf("输入非法,请重新输入:"); scanf("%d%d", &x, &y); printf("\n"); } if (x>=1&&x<=10&&y>=1&&y<=10) { if (mine[x][y] == '0') { blank(mine, show, x, y); Display(show, ROWS, COLS); Display(mine, ROWS, COLS); count++; } else { printf("踩雷了,失败\n"); Display(mine, ROWS, COLS); return ; } } else { printf("输入有误\n"); } } printf("恭喜,扫雷成功!\n"); } void menu() { printf("********************************\n"); printf("*********1.play 0.exit********\n"); printf("********************************\n"); } void test() { int input = 0; srand((unsigned) time(NULL)); do { menu(); printf("请选择:"); scanf("%d", &input); switch(input) { case 0: break; case 1: game(); break; default : printf("输入非法,请重新输入\n"); } }while(input);//可以不断玩游戏 } int main() { test(); return 0; }
3 game.c//函数具体实现
game.c
#include "game.h" void InitBoard(char arr[ROWS][COLS], int row, int col, char set)//初始化数组函数 { int i = 0; int j = 0; for (i=0; i<ROWS ; i++) { for (j=0; j<COLS; j++) { arr[i][j] = set; } } } void Display(char arr[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf(" "); for (i=1; i<=row-2; i++) { printf("%d ", i); } printf("\n"); for (i=1; i<=row-2; i++) { printf("%2d ", i); for (j=1; j<=col-2; j++) { printf("%c ",arr[i][j]); } printf("\n"); } } void Setmine(char arr[ROWS][COLS], int row, int col) { int count = 0; int i = 0; int j = 0; while (count<COUNT) { i = rand()%10+1; j = rand()%10+1; if (arr[i][j]=='0') { arr[i][j] = '1'; count++; } } } void blank(char mine[ROWS][COLS], char show[ROWS][COLS] ,int x, int y)//递归调用 { if (mine[x][y]=='0') { show[x][y] = GetMineCount(mine, x, y); if (show[x][y] == '0') { show[x][y] = ' '; if (show[x - 1][y - 1] == '*') { blank(mine, show, x - 1, y - 1); } if (show[x - 1][y] == '*') { blank(mine, show, x - 1, y); } if (show[x - 1][y + 1] == '*') { blank(mine, show, x - 1, y + 1); } if (show[x][y + 1] == '*') { blank(mine, show, x, y + 1); } if (show[x + 1][y + 1] == '*') { blank(mine, show, x + 1, y + 1); } if (show[x + 1][y] == '*') { blank(mine, show, x + 1, y); } if (show[x + 1][y - 1] == '*') { blank(mine, show, x + 1, y - 1); } if (show[x][y - 1] == '*') { blank(mine, show, x, y - 1); } } } }
)
此图未运行完全。
相关文章推荐
- VS中模仿WPF模板创建最简单的WPF程序
- 简单模仿mfc程序
- winSDK C++ 模仿基础对话框的简单的基础程序
- 模仿MFC实现简单画图程序
- 模仿别人的简单贪吃蛇程序
- 一个简单程序快速入门JDBC
- 用Qt Creator编写一个简单的窗口程序
- NET简单的一个画图程序 使用简单 自己可以相关自己的内容进行配置就可以使用了
- 一个简单的Servlet程序
- Wolf从零学编程-用Python打造简单加密程序(二)
- 最简单的条件判断程序
- 简单的时间片轮转多道程序内核代码浅析(一)
- 怎样编写一个简单的hibernate程序
- 怎样将程序猿写出来的程序打包成安装包(最简单的)
- 用flask开发博客(1)——第一个简单程序
- 最简单的基于FFMPEG的Helloworld程序
- 简单的Web登录程序,jsp+mysql
- windbg 基础命令实战 - 简单程序破解
- WCF创建简单程序
- 简单程序3-判断三角形