c语言扫雷游戏

简单的c语言扫雷游戏,希望能对大家学习c语言提供帮助,这个简单的扫雷游戏会提高大家对函数和循环使用和理解,希望大家喜欢,谢谢。

我们需要三个文件来编写这个游戏,game.h主要是存放我们的库函数,game.c主要是帮助我们声明函数让我们能够更加之直观的看到我们所需要的函数,test.c就是我们运行游戏时的页面。

1头文件:game.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define EASY_COUNT 10

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

//初始化棋盘
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 board[ROWS][COLS], int row, int col);

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

源文件:game.c

#include "game.h"
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    int i = 0;
    for (; i < rows; i++)
    {
        int j = 0;
        for (; j < cols; j++)
        {
            board[i][j] = set;//初始化棋盘
        }
    }
}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    printf("--扫雷游戏开始--\n");
    //打印列号
    for (i = 0; i <=col; i++)
    {
        printf("%2d", i);
    }
    printf("\n");
    for (i=1; i <=row; i++)
    {
        int j = 1;
        printf("%2d", i);//打印行号
        for (j=1; j <=col; j++)
        {
            printf("%2c", board[i][j]);
        }
        printf("\n");
    }
}
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
    int count = EASY_COUNT;
    while (count)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;

        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
    }
}
//统计雷
static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
     return (mine[x - 1][y] 
        + mine[x - 1][y - 1] +      //‘0’-48
        mine[x - 1][y + 1] +        //‘1’-49
        mine[x][y - 1] +            //‘2’-50
        mine[x + 1][y - 1] +        //由此计算出雷的个数
        mine[x + 1][y] +
        mine[x + 1][y + 1] + 
        mine[x][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)//9*9-10=71(71个不是雷)
    {
        printf("请输入要排查雷的坐标:");
        scanf_s("%d%d", &x, &y);
        //判断坐标的有效性
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            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");
        }
    }
    if (win == row * col - EASY_COUNT)
    {
        printf("恭喜你,排雷成功\n");
        DisplayBoard(mine, ROW, COL);
    }
}

源文件:test.c

#include "game.h"

void menu()
{
    printf("******************\n");
    printf("***** 1.play *****\n");
    printf("***** 2.exit *****\n");
    printf("******************\n");
}

void game()
{
    //完成扫雷游戏
    char mine[ROWS][COLS];//数组全部初始化为'0',存放布置好的雷

    //show数组中存放排查出的雷的信息
    char show[ROWS][COLS];//数组全部初始化为'*',存放排查出来的雷
    //初始化棋盘
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');
    
    //打印棋盘
    //DisplayBoard(mine, ROW,COL);
    DisplayBoard(show, ROW, COL);

    //布置雷
    // 在9*9的棋盘上布置10个雷
    SetMine(mine,ROW,COL);

    //排查雷
    FindMine(mine,show,ROW,COL);
}
int main()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:\n");
        scanf_s("%d", &input);
        switch(input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("请重新选择\n");
            break;
        }
    }while (input);
    return 0;
}
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值