扫雷

扫雷游戏是windous中的常见简单游戏,用c语言和c++均可以实现,以下用c语言来实现
核心思想:
要有两个棋盘(一个用来设置雷区,一个让玩家扫雷游戏)
初始化扫雷界面,对雷位置进行处理
在扫雷中对坐标位置判断是否有雷

以下游戏还可继续拓展

为了使程序逻辑清楚,方便阅读;
test.c中用来测试游戏,game.c中是游戏实现,game.h中包含头文件及函数的声明

test.c

#define _CRT_SECURE_NO_WARNINGS_1
#include"game.h"
        void menu()
    {
            printf("********************\n");
            printf("****  1.play  ******\n");
            printf("****  0.exit  ******\n");
            printf("********************\n");
        }

    void play_game()
    {
        char mine[ROWS][COLS];
        char show[ROWS][COLS];
        init_game(mine, show);
        display(show);
        sweep(mine, show);
    }

    void test()
    {
        int input = 0;
        do
        {
            menu();
            printf("请输入指令>:");
            scanf_s("%d", &input);
            switch (input)
            {
            case PLAY:
                play_game();
                break;
            case EXIT:
                break;
            }
        } while (input);
    }

    int main()
    {
        test();
        system("pause");
    }

game.c

#define _CRT_SECURE_NO_WARNINGS_1
#include"game.h"

    void init_game(char mine[ROWS][COLS], char show[ROWS][COLS]) //初始化扫雷界面
    {
        memset(mine, '0', ROWS*COLS*sizeof(char));
        memset(show, '*', ROWS*COLS*sizeof(char));
        set_mine(mine);
    }

    int get_mine_num(int x, int y)   //随机产生雷的位置
    {
        return rand() % (y - x + 1) + x;
    }

    void set_mine(char mine[ROWS][COLS])   //设置雷区
    {
        int count = COUNT;
        int x = 0;
        int y = 0;
        srand((unsigned)time(NULL));
        while (count)
        {
            x = get_mine_num(1, 9);
            y = get_mine_num(1, 9);
            if (mine[x][y] == '0')
            {
                mine[x][y] = '1';
                count--;
            }
        }
    }

    void display(char show[ROWS][COLS])  //打印出界面
    {
        int i = 0;
        int j = 0;
        printf(" ");
        for (i = 1; i < COLS - 1; i++)
        {
            printf(" %d ", i);
        }
        printf("\n");
        for (i = 1; i < ROWS - 1; i++)
        {
            printf("%d", i);
            for (j = 1; j < COLS - 1; j++)
            {
                printf(" %c ", show[i][j]);
            }
            printf("\n");
        }
    }

    void sweep(char mine[ROWS][COLS], char show[ROWS][COLS])//开始扫雷
    {
        int count = 0;
        int x = 0;
        int y = 0;
        while (count != ((ROWS - 2)*(COLS - 2) - COUNT))
        {
            printf("请输入坐标>:");
            scanf_s("%d%d", &x, &y);
            if (mine[x][y] == '1')
            {
                printf("踩雷了!\n");
                return;
            }
            else
            {
                int ret = get_mine(mine, x, y);
                show[x][y] = ret + '0';
                display(show);
                count++;
            }
        }
        printf("恭喜你赢了!\n");
        display(mine);
    }

    int get_mine(char mine[ROWS][COLS], int x, int y)   //附近雷的个数
    {
        int count = 0;
        if (mine[x - 1][y - 1] == '1') count++; //坐标位置的上3个
        if (mine[x - 1][y] == '1') count++;
        if (mine[x - 1][y + 1] == '1') count++;

        if (mine[x][y - 1] == '1') count++;  //坐标位置的左右
        if (mine[x][y + 1] == '1') count++;

        if (mine[x + 1][y - 1] == '1') count++; //坐标位置的下3个
        if (mine[x + 1][y] == '1') count++;
        if (mine[x + 1][y + 1] == '1') count++;

        return count;
    }

game.h

#define _CRT_SECURE_NO_WARNINGS_1
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>



    enum oppition
    {
        EXIT,
        PLAY
    };

#define ROWS 11
#define COLS 11
#define COUNT 30

    void init_game(char mine[ROWS][COLS], char show[ROWS][COLS]);   //初始化扫雷界面
    void set_mine(char mine[ROWS][COLS]);    //设置雷区
    void get_num(int x, int y);    //随机产生雷的位置
    void display(char show[ROWS][COLS]);   //打印出界面
    void sweep(char mine[ROWS][COLS], char show[ROWS][COLS]);  //开始扫雷
    int get_mine(char mine[ROWS][COLS], int x, int y);   //附近雷的个数

#endif

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值