栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言扫雷

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言扫雷

Game.h

#define ROW 9

#define COL 9

#define ROWS ROW+2

#define COLS COL+2//定义值

#define EASY_COUNT 10

#include//srand的头文件

#include//time的头文件

#include

void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);

//  DisplayBoard(mine,ROW,COL);

//DisplayBoard(show, ROW, COL);

//共用IintBoard

void DisplayBoard(char board[ROWS][COLS], int row, int col);//数组传过来的是ROWS,COLS

//同IintBoard

void SetMine(char board[ROWS][COLS], int row, int col);

//查找雷

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

Test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu()

{

    printf("************************************n");

    printf("*******1.开始游戏。0.退出游戏*******n");

    printf("************************************n");

}

void game()

{

    //雷的信息存储

    //1.布置好雷的信息

    char mine[ROWS][COLS] = { 0 };

    //2.排查出的雷的信息

    char show[ROWS][COLS] = { 0 };

    //初始化

    InitBoard(mine,ROWS,COLS,'0');

    InitBoard(show,ROWS,COLS,'*');

    //打印棋盘

    //DisplayBoard(mine,ROW,COL);

    DisplayBoard(show,ROW,COL);

    //布置雷

    SetMine(mine,ROW,COL);

    //扫雷,再次打印棋盘

    //DisplayBoard(mine, ROW, COL);//查看雷的位置

    FindMine(mine,show,ROW,COL);

}

int main()

{

    int input = 0;

    srand((unsigned int)time(NULL));//(unsigned int)强制类型转换

    do

    {

        menu();

        printf("请选择:>");

        scanf("%d", &input);

        switch (input)

        {

        case 1:

             game();

             break;

        case 0:

             printf("退出游戏");

             break;

        default:

             printf("选择错误,请重新选择:>");

             break;//此处的break不是终止循环,只是跳出switch

        }

    } while (input);

    return 0;

}

Game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)

{

    int i = 0;

    int j = 0;

    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");

}

void SetMine(char mine[ROWS][COLS], int row, int col)

{

    //布置10个雷

    int count = EASY_COUNT;

    while (count)

    {

        //生产随机的下标

        int x = rand() % row + 1;

        int y = rand() % col + 1;

        if (mine[x][y] == '0')

        {

             mine[x][y] = '1';

             count--;

        }

    }

}

//static

//1. 修饰局部变量

//2. 修饰全局变量

//3. 修饰函数

static int get_mine_count(char mine[ROWS][COLS], int x, int y)

{

    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';

}

//'3'-'0'=3

//'1'-'0'=1

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)

{

    //1. 输入排查的坐标

    //2. 检查坐标处是不是雷

       // (1) 是雷   - 很遗憾炸死了 - 游戏结束

       // (2) 不是雷  - 统计坐标周围有几个雷 - 存储排查雷的信息到show数组,游戏继续

    int x = 0;

    int y = 0;

    int win = 0;

    while (win < row * col - EASY_COUNT)

    {

        printf("请输入要排查的坐标:>");

        scanf("%d%d", &x, &y);//x--(1,9)  y--(1,9)

        //判断坐标的合法性

        if (x >= 1 && x <= row && y >= 1 && y <= col)

        {

             if (mine[x][y] == '1')//'1'字符

             {

                 printf("很遗憾,你被炸死了n");

                 DisplayBoard(mine, row, col);

                 break;

             }

             else

             {

                 //不是雷情况下,统计x,y坐标周围有几个雷

                 int count = get_mine_count(mine, x, y);

                 show[x][y] = count + '0';

                 //显示排查出的信息

                 DisplayBoard(show, row, col);

                 win++;

             }

        }

        else

        {

             printf("坐标不合法,请重新输入n");

        }

    }

    if (win == row * col - EASY_COUNT)

    {

        printf("恭喜你,排雷成功n");

        DisplayBoard(mine, row, col);

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号