C语言实现扫雷

C语言实现扫雷

//头文件game.h

#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 10
#define COLS 10
#define DEFAULT_COUNT 10
void display(char arr[ROWS + 2][COLS + 2], int rows, int cols);
void set_sweep(char arr[ROWS + 2][COLS + 2], int rows, int cols);
int get_sweep_count(char arr[ROWS + 2][COLS + 2],int x, int y);
void move_sweep(char arr[ROWS + 2][COLS + 2], int i, int j);
#endif//__GAME_H__

//函数实现文件game.c

#include<stdio.h>
#include"game.h"
void display(char arr[ROWS + 2][COLS + 2], int rows, int cols)
{
    int i;
    int j;
    printf("   ");
    for (i = 1; i <= rows; i++)
    {
        printf("%d ", i);
    }
    printf("\n");
    for (i = 1; i <= rows; i++)
    {
        printf("%2d ", i);
        for (j = 1; j <= cols; j++)
        {
            printf("%c ",arr[i][j]);
        }
        printf("\n");
    }
}

void set_sweep(char arr[ROWS + 2][COLS + 2], int rows, int cols)
{
    int count = DEFAULT_COUNT;
    while (count)
    {
        int x = rand()%10+1;
        int y = rand()%10+1;
        if (arr[x][y] == '0')
        {
            arr[x][y] = '1';
            count--;
        }
    }
} 
int get_sweep_count(char arr[ROWS + 2][COLS + 2],int x, int y)
{ 
    int z;
    z = (arr[x - 1][y - 1] - '0') + (arr[x][y - 1] - '0') + (arr[x + 1][y - 1] - '0') + (arr[x + 1][y] - '0') + (arr[x + 1][y + 1] - '0') + (arr[x][y + 1] - '0') + (arr[x - 1][y + 1] - '0') + (arr[x - 1][y] - '0');
    return (z);

}

void move_sweep(char sweep[ROWS + 2][COLS + 2], int i, int j)
{
    int k = 0; 
    k++;
    if (k == 1 && sweep[i][j] == '1')
    {
        while (sweep[i][j] == '1')
        {
            int x = rand() % 10 + 1;
            int y = rand() % 10 + 1; 
            if (sweep != '1')
            {
                sweep[x][y] = sweep[i][j];
                sweep[i][j] = '0';
            }
        }
    }
}

//测试文件 test.c

#include<stdio.h>
#include"game.h"
#include<string.h>
#include<time.h>
#include<stdlib.h>
void menu()//菜单函数,输出可供选择的菜单
{
    printf("**************************\n");
    printf("***** 1.PLAY  0.EXIT *****\n");
    printf("**************************\n");
}
enum Option
{
    EXIT,
    PLAY
};

void game()
{
    char sweep[ROWS + 2][COLS + 2] = { 0 };
    char  show[ROWS + 2][COLS + 2] = { 0 };
    int x = 0;
    int y = 0;
    int win = 0;

    srand((unsigned int)time(NULL));
    memset(sweep, '0', sizeof(char)*(ROWS + 2)*(COLS + 2));
    memset(show, '*', sizeof(char)*(ROWS + 2)*(COLS + 2));
    display(show, ROWS, COLS);
    set_sweep(sweep, ROWS, COLS);
    //display(sweep, ROWS, COLS);
    while (win < (ROWS*COLS - DEFAULT_COUNT))
    {
        printf("请输入坐标:");
        int x;
        int y;
        int i = 0;
        int j = 0;
        int tmp = 0;
        int count1 = 1;
        scanf_s("%d%d", &x, &y);
        if (win == 0 && sweep[x][y] == '1')
        {
            if (sweep[x][y] == '1')
            {
                int i = rand() % 10 + 1;
                int j = rand() % 10 + 1;
                if (sweep != '1')
                {
                    tmp = sweep[i][j];
                    sweep[i][j] = sweep[x][y];
                    sweep[x][y] = tmp;
                    if (sweep[x][y] != '1')
                    {
                        int ret = 0;
                        ret = get_sweep_count(sweep, x, y);
                        show[x][y] = ret + '0';
                        win++;
                        if (ret == 0)
                        {
                            if (sweep[x - 1][y - 1] != '1')
                            {
                                ret = get_sweep_count(sweep, x - 1, y - 1);
                                show[x - 1][y - 1] = ret + '0';
                            }
                            if (sweep[x][y - 1] != '1')
                            {
                                ret = get_sweep_count(sweep, x, y - 1);
                                show[x][y - 1] = ret + '0';
                            }
                            if (sweep[x + 1][y - 1] != '1')
                            {
                                ret = get_sweep_count(sweep, x + 1, y - 1);
                                show[x + 1][y - 1] = ret + '0';
                            }
                            if (sweep[x + 1][y] != '1')
                            {
                                ret = get_sweep_count(sweep, x + 1, y);
                                show[x + 1][y] = ret + '0';
                            }
                            if (sweep[x + 1][y + 1] != '1')
                            {
                                ret = get_sweep_count(sweep, x + 1, y + 1);
                                show[x + 1][y + 1] = ret + '0';
                            }
                            if (sweep[x][y + 1] != '1')
                            {
                                ret = get_sweep_count(sweep, x, y + 1);
                                show[x][y + 1] = ret + '0';
                            }
                            if (sweep[x - 1][y + 1] != '1')
                            {
                                ret = get_sweep_count(sweep, x - 1, y + 1);
                                show[x - 1][y + 1] = ret + '0';
                            }
                            if (sweep[x - 1][y] != '1')
                            {
                                ret = get_sweep_count(sweep, x - 1, y);
                                show[x - 1][y] = ret + '0';
                            }
                        }
                    }
                }
            }
        }
        else if (sweep[x][y] == '1')
        {
            printf("被炸死\n");
            break;
        }
        else if (sweep[x][y] != '1')
        {
            int ret = 0;
            win++;
            ret = get_sweep_count(sweep, x, y);
            /*printf("%d", ret);*/
            show[x][y] = ret + '0';
            if (ret == 0)
            {
                if (sweep[x - 1][y - 1] != '1')
                {
                    ret = get_sweep_count(sweep, x - 1, y - 1);
                    show[x - 1][y - 1] = ret + '0';
                }
                if (sweep[x][y - 1] != '1')
                {
                    ret = get_sweep_count(sweep, x, y - 1);
                    show[x][y - 1] = ret + '0';
                }
                if (sweep[x + 1][y - 1] != '1')
                {
                    ret = get_sweep_count(sweep, x + 1, y - 1);
                    show[x + 1][y - 1] = ret + '0';
                }
                if (sweep[x + 1][y] != '1')
                {
                    ret = get_sweep_count(sweep, x + 1, y);
                    show[x + 1][y] = ret + '0';
                }
                if (sweep[x + 1][y + 1] != '1')
                {
                    ret = get_sweep_count(sweep, x + 1, y + 1);
                    show[x + 1][y + 1] = ret + '0';
                }
                if (sweep[x][y + 1] != '1')
                {
                    ret = get_sweep_count(sweep, x, y + 1);
                    show[x][y + 1] = ret + '0';
                }
                if (sweep[x - 1][y + 1] != '1')
                {
                    ret = get_sweep_count(sweep, x - 1, y + 1);
                    show[x - 1][y + 1] = ret + '0';
                }
                if (sweep[x - 1][y] != '1')
                {
                    ret = get_sweep_count(sweep, x - 1, y);
                    show[x - 1][y] = ret + '0';
                }
            }
        }
        display(show, ROWS, COLS);
        if (win >= (ROWS*COLS - DEFAULT_COUNT))
        {
            printf("排雷成功\n");
        }
    }
}
int main()
{
    int input = 0;
    do
    {
        menu();
        printf("请选择:");
        scanf_s("%d", &input);
        switch (input)
        {
        case PLAY:
            game();
            break;
        case EXIT:
            break;
        default:
            printf("请重新选择:");
            break;
        }
    } while (input);
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值