云南网站建设模块网络营销做得好的酒店
test.c源文件 - 扫雷游戏测试
game.h头文件 - 扫雷游戏函数的声明
game.c源文件 - 扫雷游戏函数的实现
1.布置雷 -- 存放雷的雷盘 9*9 数组设计成11*11 上下左右方各多一行,保证周围8的范围 雷 - 1 不是雷 - 0
2.排查雷
主题测试源文件代码 :
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"// 雷 - 1 不是雷 - 0
void menu()
{printf("****************************************\n");printf("******* 扫雷游戏 *********\n");printf("******* author:小凡同学 *********\n");printf("******* 1.play *********\n");printf("******* 0.exit *********\n");printf("****************************************\n");
}void game()
{char mine[ROWS][COLS];//存放布置好雷的信息char show[ROWS][COLS];//存放排查出雷的信息//两个数组坐标严格一一对应//初始化雷盘InitBoard(mine, ROWS, COLS,'0');//初始化为 全0InitBoard(show, ROWS, COLS,'*');//初始化为 全*//打印一下雷盘DisplayBoard(mine, ROW, COL);DisplayBoard(show, ROW, COL);//布置雷 使用的雷盘是9*9SetMine(mine, ROW, COL);//DisplayBoard(mine, ROW, COL);//排查雷FindMine(mine,show, ROW, COL);//对mine进行排查,排查结果放入show
}
int main()
{int input;srand((unsigned int)time(NULL));//使用rand()随机生成函数前提 #include<stdlib.h> #include<time.h>//(unsigned int)time(NULL)强制类型转换do {menu();printf("请选择:>");scanf("%d", &input);switch (input){case 1:game();/*printf("扫雷游戏\n");*/break;case 0:printf("退出游戏\n");break;default:printf("选择有误,请重新选择\n");break;}} while (input);return 0;
}
封装声明头文件代码:
#pragma once
#define ROW 9
#define COL 9#define ROWS ROW+2
#define COLS COL+2#define EASY_COUNT 10
//存放头文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>//初始化雷盘
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);//布置雷 使用的雷盘是9*9
void SetMine(char mine[ROWS][COLS], int row, int col);//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
游戏函数功能实现源文件代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h" //自己编写的头文件用引号""引用//初始化雷盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{int i, j;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){board[i][j] = set;}}
}//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{int i = 0;int j = 0;printf("--------------扫雷游戏-------------\n");//打印列号for (i = 0; i <= col; i++){printf("%d ", i);}printf("\n");for (i = 1; i <= row; i++){printf("%d ", i);//打印行号for (j = 1; j <= col; j++){printf("%c ", board[i][j]);}printf("\n");}printf("--------------扫雷游戏-------------\n");
}//布置雷 使用的雷盘是9*9
void SetMine(char mine[ROWS][COLS], int row, int col)
{//布置10个雷int count = EASY_COUNT;while (count){//生成随机的下标 1-9 int x = rand() % row + 1; //使用rand()前需要调用srand()int y = rand() % col + 1;// 雷 - 1 不是雷 - 0 if (mine[x][y] == '0'){mine[x][y] = '1';count--;//布置一个减少一个}}
}
//统计x,y坐标周围有几个雷//static
1.修饰局部变量
//2.修饰全局变量
//3.修饰函数
static int get_mine_count(char mine[ROWS][COLS], int x, int y) //加上static,让该函数只能在自己所在的源文件内部看到,其他源文件不可以用该函数
{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] - 8 * '0';//利用的ASCII码值计算
}//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{//1.输入排查的坐标//2.检查坐标处是不是雷//(1)是雷 - 很遗憾炸死了 - 游戏结束//(2)不是雷 - 统计坐标周围有几个雷 - 存储排查雷的信息到show数组,游戏继续//int x=0,y=0,win=0;while(win <row*col-EASY_COUNT){printf("请输入要排查的坐标:>");scanf("%d %d", &x, &y);//判断坐标的合法性 1-9if (x >= 1 && x <= row && y >= 1 && y <= col){if (mine[x][y] == '1'){printf("很遗憾,你踩雷了!!!\n");DisplayBoard(mine, row, col);break;}else//排雷成功{//不是雷的情况下,统计x,y坐标周围有几个雷int count = get_mine_count(mine, x, y);show[x][y] = count+'0';//获得的是字符对应地ASCII值 - %c 打印出数字字符 数字字符与ASCII对应关系//ASCII码/*0 '0' - 481 '1' - 49*///显示排查出来的信息DisplayBoard(show, row, col);win++;}}else{printf("坐标不合法,请重新输入\n");}}if (win == row * col - EASY_COUNT){printf("恭喜你,排雷成功!!!\n");DisplayBoard(mine, row, col);}}
运行结果: