扫雷——1.第一步不炸死2.坐标周围没雷,可以实现展开

game.h
#ifndef __GAME_H__
#define __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 COUNT 10
void Initboard(char board[ROWS][COLS], int row, int col, char ret);
void Displayboard(char board[ROWS][COLS], int row, int col);
void Bombboard(char board[ROWS][COLS], int row, int col);
int  Countbomb(char show[ROWS][COLS], int x, int y);
char first_step(char board[ROWS][COLS], int x, int y, int row, int col);
#endif
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[ROWS][COLS], int row, int col, char ret)//初始化数组
{
    int i = 0, j = 0;
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            board[i][j] = ret;
        }
    }

}
void Displayboard(char board[ROWS][COLS], int row, int col)//打印雷盘界面
{
    int i = 0, j = 0;
    for (i = 0; i <= row; i++)
    {
        printf("%d ", i);
    }
    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 Bombboard(char board[ROWS][COLS], int row, int col)//设置雷
{
    int count = COUNT;
    while (count)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }

    }
}
void first_step(char board[ROWS][COLS], int x, int y, int row, int col)//第一步不睬雷
{
    if (board[x][y] == '1')
    {
        board[x][y] = '0';
        int count = 1;
        while (count)
        {
            int x1 = rand() % row + 1;
            int y1 = rand() % col + 1;
            if (board[x][y] == '0'&&x1 != x&&y1 != y)
            {
                board[x1][y1] = '1';
                count--;
            }
        }
    }
    return board[x][y];
}
int  Countbomb(char board[ROWS][COLS], int x, int y)//统计周围雷数
{
    return board[x][y] = board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] +
        board[x][y - 1] + board[x][y + 1] +
        board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';

}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
    int x = 0, y = 0,i=0;
    char board[ROWS][COLS] = {'0'};
    char show[ROWS][COLS] = { '0' };
    Initboard(board, ROW, COL,'0');
    Initboard(show, ROW, COL,'*');

    Bombboard(board, ROW, COL);
    //Displayboard(board, ROW, COL);//显示雷的分布点
    Displayboard(show, ROW, COL);
    while (i<ROW*COL - COUNT)
    {
        int  ret = 0;
        printf("请选择:\n");
        scanf("%d%d", &x, &y);
        if (x >= 1 && x <= ROW&&y >= 1 && y <= COL)
        {
            if (board[x][y] == '1'&&i == 0)
            {
                board[x][y]=first_step(board, x, y, ROW, COL);
                Displayboard(board, ROW, COL);
                ret = Countbomb(board, x, y);
                show[x][y] = ret + '0';
                Displayboard(show, ROW, COL);
                i++;
            }
            else
            {
                if (board[x][y] == '0')
                {
                    ret = Countbomb(board, x, y);
                    show[x][y] = ret + '0';

                    if (ret == 0)
                    {
                        show[x - 1][y - 1] = ' ';
                        show[x - 1][y] = ' ';
                        show[x - 1][y + 1] = ' ';
                        show[x][y - 1] = ' ';
                        show[x][y + 1] = ' ';
                        show[x + 1][y - 1] = ' ';
                        show[x + 1][y] = ' ';
                        show[x + 1][y + 1] = ' ';
                    }

                    Displayboard(show, ROW, COL);
                    i=i+8;
                }

                else
                {
                    printf("很遗憾,你被炸死了\n");
                    break;
                }
            }

        }
        else
        {
            printf("输入坐标有误\n");
        }

    }
    if (i==ROW*COL - COUNT)
        printf("恭喜你,排雷成功!\n");

}
void menu()
{
    printf("*********************\n");
    printf("***1.开始   0.退出***\n");
    printf("*********************\n");
}
int main()
{
    menu();
    int input = 0;
    srand((unsigned)time(NULL));
    do
    {
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        }
    } while (1);
    system("pause");
    return 0;
}

整个代码写的很糙,自己磨了好几天才把结果磨正确,所以代码不是很好,欢迎各位大神提意见~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值