c语言实现的扫雷游戏

game.h

#ifndef __GAME_H__
#define __GAME_H__

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


#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10


void init_board(char arr[ROWS][COLS], int row, int col, char set);
void display_board(char arr[ROWS][COLS], int row, int col);
void set_mine(char arr[ROWS][COLS], int row, int col);
void clear_mine(char arr[ROWS][COLS], char arr2[ROWS][COLS], int row, int col);
void extend_board(char arr[ROWS][COLS], char arr2[ROWS][COLS], int x, int y);



#endif//__GAME_H__

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char arr[ROWS][COLS], int row, int col,char set)
{
    int i = 0;
    for (i = 0; i < row; i++)
    {
        int j = 0;
        for (j = 0; j < col; j++)
        {
            arr[i][j] = set;
        }
    }
}

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


void set_mine(char arr[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    int i = 0;
    int tmp = EASY;
    while (tmp)
    {
        x = (rand() % 9) + 1;
        y = (rand() % 9) + 1;
        if (arr[x][y] != '1')
        {
            arr[x][y] = '1';
            tmp--;
        }
    }
}

void clear_mine(char arr[ROWS][COLS], char arr2[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    int x1 = 0;
    int y1 = 0;
    char sum = 0;
    int count = (row - 2)*(col - 2) - EASY;//不是雷的个数
    int count2 = 0;//记录第几次输入坐标
    while (count)
    {
        printf("请输入坐标:");
        scanf("%d%d", &x, &y);
        count2++;
        while (count2 == 1)
        {
            while (arr[x][y] == '1')
            {
                arr[x][y] = '0';
                while (1)
                {
                    x1 = (rand() % 9) + 1;
                    y1 = (rand() % 9) + 1;
                    if (arr[x1][y1] != '1')
                    {
                    arr[x1][y1] = '1';
                    break;
                    }
                }
            }
            //display_board(arr, ROWS, COLS);
            break;
        }
        if (arr[x][y] == '1')
        {
            printf("很遗憾,你被炸死了!\n");
            break;
        }
        else
        {
            //统计旁边有几个雷
            sum = ((arr[x - 1][y] + arr[x - 1][y - 1] + arr[x][y - 1] + \
                + arr[x + 1][y - 1] + arr[x + 1][y] + arr[x + 1][y + 1] + \
                + arr[x][y + 1] + arr[x - 1][y + 1]) - 8 * '0')+'0';
            arr2[x][y] = sum;
            //display_board(arr2, ROWS, COLS);
            count--;
            extend_board(arr,arr2, x, y);//展开
        }
    }
    if (count == 0)
        printf("恭喜你,排雷成功!");
}
void extend_board(char arr[ROWS][COLS], char arr2[ROWS][COLS], int x, int y)
{
    int sum = 0;
    sum = (arr[x - 1][y] + arr[x - 1][y - 1] + arr[x][y - 1] + \
        + arr[x + 1][y - 1] + arr[x + 1][y] + arr[x + 1][y + 1] + \
        + arr[x][y + 1] + arr[x - 1][y + 1]) - 8 * '0';
    if (sum == 0)
    {
        if (arr2[x - 1][y] == '*')
            arr2[x - 1][y] = ' ';
        if (arr2[x - 1][y - 1] == '*')
            arr2[x - 1][y - 1] = ' ';
        if (arr2[x][y - 1] == '*')
            arr2[x][y - 1] = ' ';
        if (arr2[x + 1][y - 1] == '*')
            arr2[x + 1][y - 1] = ' ';
        if (arr2[x + 1][y] == '*')
            arr2[x + 1][y] = ' ';
        if (arr2[x + 1][y + 1] == '*')
            arr2[x + 1][y + 1] = ' ';
        if (arr2[x][y + 1] == '*')
            arr2[x][y + 1] = ' ';
        if (arr2[x - 1][y + 1] == '*')
            arr2[x - 1][y + 1] = ' ';
    }
    display_board(arr2, ROWS, COLS);
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
    char mine[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };
    init_board(mine, ROWS, COLS,'0');
    init_board(show, ROWS, COLS, '*');
    //display_board(mine, ROWS, COLS);
    //display_board(show, ROWS, COLS);
    srand((unsigned int)time(NULL));
    set_mine(mine, ROWS, COLS);
    display_board(mine, ROWS, COLS);
    clear_mine(mine, show, ROWS, COLS);
}
void menu()
{
    printf("************************\n");
    printf("***1.play      0.exit***\n");
    printf("************************\n");
}
int main()
{
    int input = 0;
    do
    {
        menu();
        scanf("%d", &input);
        switch (input)
        {
        case 0:
            printf("退出游戏\n");
            break;
        case 1:
            //printf("开始游戏\n");
            game();
            break;
        default:
            break;
        }
    } while (input);
    system("pause");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值