C语言扫雷

本文介绍了一个简单的扫雷游戏的实现过程,包括游戏初始化、雷区设置、玩家交互及胜利条件判断等核心功能。通过C语言实现了游戏逻辑,并详细解释了关键函数的作用。

Game.h

#define ROW 9

#define COL 9

#define ROWS ROW+2

#define COLS COL+2//定义值

#define EASY_COUNT 10

#include<stdlib.h>//srand的头文件

#include<time.h>//time的头文件

#include<stdio.h>

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

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值