扫雷(C语言)

  • 第一次下子不炸死
  • 坐标周围没雷,可以实现展开还在研究中

game.h

#ifndef __GAME_H__
#define __GAME_H__

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

enum OPTION
{
    EXIT,
    START
};
enum NANDU
{
    PRIMARY = 10
};
#define COL 9
#define ROW 9
#define COLS COL+2
#define ROWS ROW+2

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void PrintBoard(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);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void open_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
#endif//__GAME_H__

test.c

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

    char mine[ROWS][COLS];//保存随机插入的雷
    char show[ROWS][COLS];//为用户显示每次排雷后得到的信息
    InitBoard(mine,ROWS,COLS,'0');
    InitBoard(show,ROWS,COLS,'*');

    SetMine(mine, ROW, COL);//布置雷,给程序员看
    PrintBoard(show, ROW, COL);//将用*隐藏过的雷盘展示给用户
    //PrintBoard(mine, ROW, COL);//程序员查看的雷盘,可以不打开

    FindMine(mine, show, ROW, COL);//排雷,并保证第一次不死
}
void test()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:");
        scanf_s("%d", &input);
        switch (input)
        {
        case START:
            game();
            break;
        case EXIT:
            printf("退出游戏\n");
            break;
        default:
            break;
        }
    } while (input);
}
int main()
{
    test();
    system("pause");
    return 0;
}

game.c

#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = set;
        }
    }
}
void PrintBoard(char board[ROWS][COLS], int row, int col)

{
    int i = 0;
    int j = 0;
    for (i = 0; i <= col; 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 SetMine(char board[ROWS][COLS], int row, int col)//雷用‘1’代替,非雷用‘0’代替
{
    int count = PRIMARY;
    while (count)
    {
        int x = rand() % 9 + 1;
        int y = rand() % 9 + 1;
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
    }
}
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 < col * row - PRIMARY)//判断什么时候雷排完
    {
        printf("请输入排查的坐标:");
        scanf_s("%d%d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)//保证坐标合法
        {
            if (mine[x][y] == '1')
            {
                if (win == 0)//第一次踩到雷
                {
                    mine[x][y] = '0';//将该位置雷拔除
                    char n = GetMineCount(mine,x, y);
                    show[x][y] = n + '0';
                    PrintBoard(show, ROW, COL);
                    int x = rand() % 9 + 1;
                    int y = rand() % 9 + 1;
                    if (mine[x][y] == '0')
                    {
                        mine[x][y] = '1';
                    }
                }
            }
            else
            {
                int count = 0;
                win++;//每进入一次,说明找到了一个非雷坐标
                count = GetMineCount(mine, x, y);
                show[x][y] = count + '0';//将对应的数字转化为字符存入
                PrintBoard(show, ROW, COL);
            }
        }
        else
        {
            printf("坐标非法\n");
        }
    }
    if (win >= col * row - PRIMARY)
    {
        printf("恭喜你获得胜利:\n");
    }
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)//统计该坐标周围雷的数量
{
    //该坐标周围每个元素的值均减去‘0’得到的数字和,即雷的个数(‘0’-‘0’= 0)(‘1’-‘0’= 1)
        return mine[x - 1][y] +
               mine[x - 1][y - 1] +
               mine[x][y - 1] +
               mine[x + 1][y - 1] +
               mine[x + 1][y] +
               mine[x + 1][y + 1] +
               mine[x][y + 1] +
               mine[x - 1][y + 1] - 8 * '0';
}

猜你喜欢

转载自blog.csdn.net/weixin_40995778/article/details/80034870
今日推荐