C语言实现扫雷

一.大致框架

代码层面:分为test.c用来测试游戏逻辑game.c用来实现函数game.h用来进行函数的声明,头文件包括,宏的定义

游戏层面:分为一个简易菜单游戏主体的实现

二.设计思路

我将用两个数组实现扫雷,一个用来显示,一个用来设置炸弹

三.实现流程

(1)制作简易菜单

void menu()
{
    printf("**************************************\n");
    printf("**************************************\n");
    printf("****************1.play****************\n");
    printf("****************0.exit****************\n");
    printf("**************************************\n");
    printf("**************************************\n");
}
void game()
{
    char mine[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };
    InitBoard(mine, ROWS, COLS,'0');
    InitBoard(show, ROWS, COLS,'*');
    DisplayBoard(show, ROW, COL);
    SetMine(mine, ROW, COL);
    FindMine(mine, show, ROW, COL);
}
int main()
{
    int input = 0;
    srand((unsigned)time(NULL));
    do
    {
        menu();
        printf("请选择\n");
        scanf("%d", &input);
        switch(input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("输入无效,请重新输入\n");
            break;
        }
    } while (input);
    return 0;
}

(2)进行游戏主体的实现

1.先在game.h进行头文件的包括、宏的定义和函数的声明

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);

因为炸弹的设置需要生成随机数,所以这里包括了stdlib.h和time.h

因为当没点到炸弹时,扫雷会提示周围雷的个数,所以我们需要扩大数组以防止数组越界访问,所以分别设置ROWS,COLS用来扩大数组,而COL,ROW是实际用到行列数量

地雷的数量也用宏定义,这里设置简单模式为10个地雷

函数的声明分为初始化数组,数组显示,设置地雷,找雷四部分

2.然后再实现函数

先是初始化两个数组,一个是显示数组,展现给玩家看,一个是埋雷数组,用来判断该坐标是否有雷

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

设置一个set变量可以使一个函数初始化两个数组,在这里我想将埋雷数组初始化为0,用1表示雷,所以我就可以将‘0’传给该函数,将显示数组初始化为‘*’,将‘*’传给该数组

然后实现数组的显示

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    for (j = 0; j <= 9; j++)
    {
        printf("%d ", j);
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf("%d ", i);
        for (j = 1; j <= col; j++)
        {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    } 
}

因为玩家需要输入坐标,所以我们在每行每列都加上序号

其次是设置地雷

void SetMine(char mine[ROWS][COLS], int row, int col)
{
    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--;
        }
    }
}

利用随机数生成地雷坐标,因为地雷坐标在1~9,所以%9+1,这个表达式的范围也是1~9,

注意不要再同一个地方重复设置雷,用if语句控制循环

最后是找雷

int GetMineCount(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'; 
}
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    int win = 0;
    while (win<row*col-EASY_COUNT)
    {
        printf("请输入要排查的坐标:>\n");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (show[x][y] == '*')
            {
                if (mine[x][y] == '1')
                {
                    printf("很遗憾你被炸死了\n");
                    DisplayBoard(mine, ROW, COL);
                    break;
                }
                else
                {
                    int count = GetMineCount(mine, x, y);
                    show[x][y] = count + '0';
                    DisplayBoard(show, ROW, COL);
                    win++;
                }
            }
            else
            {
                printf("改坐标已被排查过\n");
            }
        }
        else
        {
            printf("输入坐标非法请重新输入\n");
        }
    }
    if (win == row * col - EASY_COUNT)
    {
        printf("恭喜你排雷成功\n");
        DisplayBoard(mine, ROW, COL);
    }
}

扫雷是一个循环过程,先写一个while循环,设置win变量,每次扫雷成功时win就++,当win等于col*row-EASY_COUNT时候则说明所有非雷位置均排查,游戏胜利,退出循环,玩家可能会输入规定以外的坐标,在这个情况发生时,提醒玩家输入错误,当输入正确坐标时,判断该坐标是否为‘*’,若真则未被检测过,再判断是否为雷,若为雷,显示埋雷数组,让玩家死的明白,若不为雷,则显示该位置的周围有几个雷,因此我们再写一个GetMineCount函数,来计算该位置周围雷的个数,因为我们这里储存的是字符一,所以周围八个位置的和再减去八个字符零,就为周围八个位置字符一的个数,再另该位置赋值为count+‘0’,就可以用字符的方式显示出该位置周围雷的个数了

四.总代码

game.h

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS
#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;
    for (j = 0; j <= 9; j++)
    {
        printf("%d ", j);
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf("%d ", i);
        for (j = 1; j <= col; j++)
        {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    } 
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
    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--;
        }
    }
}
int GetMineCount(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'; 
}
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    int win = 0;
    while (win<row*col-EASY_COUNT)
    {
        printf("请输入要排查的坐标:>\n");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (show[x][y] == '*')
            {
                if (mine[x][y] == '1')
                {
                    printf("很遗憾你被炸死了\n");
                    DisplayBoard(mine, ROW, COL);
                    break;
                }
                else
                {
                    int count = GetMineCount(mine, x, y);
                    show[x][y] = count + '0';
                    DisplayBoard(show, ROW, COL);
                    win++;
                }
            }
            else
            {
                printf("改坐标已被排查过\n");
            }
        }
        else
        {
            printf("输入坐标非法请重新输入\n");
        }
    }
    if (win == row * col - EASY_COUNT)
    {
        printf("恭喜你排雷成功\n");
        DisplayBoard(mine, ROW, COL);
    }
}

test.c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
    printf("**************************************\n");
    printf("**************************************\n");
    printf("****************1.play****************\n");
    printf("****************0.exit****************\n");
    printf("**************************************\n");
    printf("**************************************\n");
}
void game()
{
    char mine[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };
    InitBoard(mine, ROWS, COLS,'0');
    InitBoard(show, ROWS, COLS,'*');
    DisplayBoard(show, ROW, COL);
    SetMine(mine, ROW, COL);
    FindMine(mine, show, ROW, COL);
}
int main()
{
    int input = 0;
    srand((unsigned)time(NULL));
    do
    {
        menu();
        printf("请选择\n");
        scanf("%d", &input);
        switch(input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("输入无效,请重新输入\n");
            break;
        }
    } while (input);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值