0%

C语言实现扫雷

文章时效性提示

本文发布于 516 天前,部分信息可能已经改变,请注意甄别。

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include "game.h"
void menu()
{
    printf("***************************\n");
    printf("***   1.Play   0.Exit   ***\n");
    printf("***************************\n");
}

void game()
{
    //1、布置雷的信息
    char mine[ROWS][COLS] = {0};
    //2、排雷
    char show[ROWS][COLS] = {0};
    //3、初始化
    Init_Board(mine,ROWS,COLS,'0');
    Init_Board(show,ROWS,COLS,'*');
    //打印棋盘
    DisplayBoard(show,ROW,COL);
    //布置雷
    SetMine(mine,ROW,COL);
    //DisplayBoard(mine,ROW,COL);
    //扫雷
    FindMine(mine,show,ROW,COL);
}

void test()
{
    int input = 0;
    srand((unsigned)time(NULL));
    do {
        menu();
        printf("请选择:->");
        scanf("%d",&input);
        switch (input)
       {
            case 1:
                game();
                break;
            case 0:
                printf("退出游戏\n");
                break;
            default:
                printf("输入错误,请重新输入\n");
                break;
        }
    } while (input);
}

int main()
{
    test();
    return 0;
}

game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10

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

void Init_Board(char board[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(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);

game.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "game.h"
void Init_Board(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;//初始化成0和*
        }
    }
}

void DisplayBoard(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)
{
    int count = EASY_COUNT;
    while (count) {
        //布置雷
        int x = rand()%row+1;//生成一个1-9之间的数字
        int y = rand()%col+1;
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
    }
}

//'1'-'0' = 1,字符1-字符0就是数字1(根据ASCII码)
int get_mine_count(char** mine[ROWS][COLS],int x,int y)
{
    return mine[x-1][y-1] +
    mine[x][y-1] +
    mine[x+1][y-1] +
    mine[x-1][y] +
    mine[x+1][y] +
    mine[x-1][y+1] +
    mine[x][y+1] +
    mine[x+1][y+1] - 8*'0' ;
}

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 < row*col-EASY_COUNT)
    {
        printf("请输入坐标->:");
        scanf("%d%d",&x,&y);
        if (x>=1 && x<=row && y>=1 && y<=col)
        {
            //坐标合法
            //踩雷了
            if (mine[x][y] == '1')
            {
                printf("踩雷了\n");
                DisplayBoard(mine, row, col);
                break;
            }
            else
            {
                //没踩雷,计算x,y坐标周围有几个雷
                int count = get_mine_count(mine,x,y);
                show[x][y] = count +'0';
                DisplayBoard(show, row, col);
                win++;
            }
        }
        else
        {
            printf("坐标非法\n");
        }
    }
    if (win == row*col-EASY_COUNT)
    {
        printf("赢了\n");
        DisplayBoard(mine, row, col);
    }
}