服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现扫雷代码

C语言实现扫雷代码

2022-12-16 14:41杯浅 C/C++

这篇文章主要为大家详细介绍了C语言实现扫雷代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现扫雷的具体代码,供大家参考,具体内容如下

前言

扫雷实现的基本规划:

1、玩家可以自由选择进入和退出游戏
2、玩家通过输入坐标来排雷
3、排查雷给出提示
4、玩家可以任意标记雷区
5、排雷同时展开非雷区

下面为效果图:

C语言实现扫雷代码

一、主函数(test.c):

通过do while嵌套switch对游戏菜单进行控制:

  • 玩游戏输入1:进入case1开始游戏。
  • 输入2:进入case2退出游戏 。
  • 输入错误:进入default,进入下一次循环重新输入。
  • 菜单给出标记操作提示。
?
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
#include "game.h"
void menu()//游戏菜单
{
    printf("**********************************\n");
    printf("*******      扫雷游戏     ********\n");
    printf("*******       1.play      ********\n");
    printf("*******       2.exit      ********\n");
    printf("*******tip1:坐标输入 0 0  ********\n");
    printf("*******    进行标记操作   ********\n");
    printf("*******tip2:坐标输入 0 1  ********\n");
    printf("*******    取消标记操作   ********\n");
    printf("**********************************\n");
}
void game()
{
    //定义两个棋盘,mine里面雷图,'*'为雷,' '为非雷
    //show里面-为未排雷区域,数字为周围有几颗雷
    //空格区域表示安全区域,周围没有雷
    char mine[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };
    //初始化棋盘
    init_board(mine, ROWS, COLS, ' ');
    init_board(show, ROWS, COLS, '-');
    //布置雷
    set_mine(mine, ROW, COL);
    Display_board(show, ROW, COL);//打印棋盘
    //排查雷
    find_mine(mine, show, ROW, COL);
}
int main()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("开始游戏\n");
            game();
            break;
        case 2:
            printf("游戏结束\n");
            break;
        default:
            printf("输入错误,请重新输入\n");
        }
    } while (input != 2);
    return 0;
}

二、头文件及定义(game.h):

行数、列数、雷数可随时更改

?
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define ROW 9 //定义行数为9
#define COL 9 //定义列数为9
#define EASY_COUNT 10 //简单定义一下雷数
 
#define ROWS ROW+2
#define COLS COL+2
 
 
//初始化棋盘
void init_board(char arr[ROWS][COLS], int rows, int cols, char set);
 
//打印棋盘
void Display_board(char arr[ROWS][COLS], int row, int col);
 
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);
 
//排查雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
 
//标记坐标
void sign_show(char show[ROWS][COLS], int row, int col);
 
//删除标记坐标
void del_sign_show(char show[ROWS][COLS], int row, int col);

三、game.c(游戏代码实现全过程):

基本逻辑就是两个棋盘:

1、mine棋盘为设雷棋盘,用来设置雷。
2、show棋盘给玩家看,打印信息给玩家。

?
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "game.h"
 
//初始化棋盘
void init_board(char arr[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++)
        {
            arr[i][j] = set;
        }
    }
}
 
//打印棋盘
void Display_board(char arr[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i <= col; i++)
    {
        printf(" %d ", i);
        if (i < col)
        {
            printf("|");
        }
    }
    printf("\n");
    for (i = 0; i <= col; i++)
    {
        printf("---");
        if (i < col)
        {
            printf("|");
        }
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf(" %d ", i);
        printf("|");
        for (j = 1; j <= col; j++)
        {
            printf(" %c ", arr[i][j]);
            if (j < col)
            {
                printf("|");
            }
        }
        printf("\n");
        if (i < row)
        {
            for (j = 0; j <= col; j++)
            {
                printf("---");
                if (j < col)
                {
                    printf("|");
                }
            }
        }
        printf("\n");
    }
    printf("=======================================\n");
}
 
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
    int count = EASY_COUNT;
    int x = 0;
    int y = 0;
    while (count)
    {
        x = rand() % row + 1;
        y = rand() % col + 1;
        if (mine[x][y] == ' ')
        {
            mine[x][y] = '*';
            count--;
        }
    }
}
 
//排查雷
static int get_mine_count(char mine[ROWS][COLS], int x, int y)//查输入坐标周围雷数
{
    int i = 0;
    int j = 0;
    int count = 0;
    for (i = x - 1; i <= x + 1; i++)
    {
        for (j = y - 1; j <= y + 1; j++)
        {
            if (mine[i][j] == '*')
            {
                count++;
            }
        }
    }
    return count;
}
void spread_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//展开非雷区
{
    int i = 0;
    int j = 0;
    for (i = x - 1; i <= x + 1; i++)
    {
        for (j = y - 1; j <= y + 1; j++)
        {
            if (i >= 1 && i <= ROW && j >= 1 && j <= COL)
            {
                if (get_mine_count(mine, i, j) == 0 && show[i][j] == '-')
                {
                    show[i][j] = ' ';
                    spread_mine(mine, show, i, j);
                }
                else if(get_mine_count(mine, i, j) != 0 && show[i][j] == '-')
                {
                    int count = get_mine_count(mine, i, j);
                    show[i][j] = count + '0';
                }
            }
        }
    }
}
int is_win(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    int count = 0;
    for (i = 1; i <= row; i++)
    {
        for (j = 1; j <= col; j++)
        {
            if (mine[i][j] != '*' && show[i][j] == '-')
            {
                return 0;
            }
        }
    }
    return 1;
}
void sign_show(char show[ROWS][COLS], int row, int col)//标记坐标
{
    int x = 0;
    int y = 0;
    printf("请选择需要标记的坐标:>");
    while (1)
    {
        scanf("%d %d", &x, &y);
        if (show[x][y] == '-')
        {
            show[x][y] = '#';
            break;
        }
        else
        {
            printf("输入错误,请重新输入");
        }
    }
}
void del_sign_show(char show[ROWS][COLS], int row, int col)//删除标记坐标
{
    int x = 0;
    int y = 0;
    printf("请选择需要删除标记的坐标:>");
    while (1)
    {
        scanf("%d %d", &x, &y);
        if (show[x][y] == '#')
        {
            show[x][y] = '-';
            break;
        }
        else
        {
            printf("输入错误,请重新输入");
        }
    }
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("请输入排雷的坐标:>");
        scanf("%d %d", &x, &y);
        if (show[x][y] != '-')
        {
            printf("此坐标已扫过,请重新输入\n");
            continue;
        }
        else if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (mine[x][y] == '*')
            {
                printf("很遗憾,你被炸死了\n");
                Display_board(mine, ROW, COL);
                break;
            }
            else
            {
                int count = get_mine_count(mine, x, y);
                if (count + '0' == '0')
                {
                    show[x][y] = ' ';
                    spread_mine(mine, show, x, y);//展开非雷区
                }
                else
                {
                    show[x][y] = count + '0';
                }
                Display_board(show, ROW, COL);
            }
        }
        else if (x == 0 && y == 0)//输入坐标为两个0时进行标记操作
        {
            sign_show(show, row, col);//标记坐标
            Display_board(show, ROW, COL);
        }
        else if (x == 0 && y == 1)//输入坐标为两个0时进行标记操作
        {
            del_sign_show(show, row, col);//删除标记
            Display_board(show, ROW, COL);
        }
        else
        {
            printf("输入坐标超出雷区范围,请重新输入\n");
        }
        if (is_win(mine,show,row,col)==1)
        {
            printf("恭喜你,扫雷成功\n");
            Display_board(mine, ROW, COL);
        }
    }
}

总结

以上就是自写C语言扫雷的代码全部内容了:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_61661271/article/details/124639560

延伸 · 阅读

精彩推荐
  • C/C++c++ qsort 与sort 对结构体排序实例代码

    c++ qsort 与sort 对结构体排序实例代码

    这篇文章主要介绍了c++ qsort 与sort 对结构体排序实例代码,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下...

    RioTian5602021-10-07
  • C/C++C语言开发实现井字棋及电脑落子优化示例详解

    C语言开发实现井字棋及电脑落子优化示例详解

    以前上课经常和同桌玩起井字棋,那么我们就当我们回忆童年,现在也用C语言来实现井字棋,本次代码相对于初阶的井字棋,在电脑下棋代码部分做了优化...

    RookieStriver8812022-02-27
  • C/C++C语言 栈与数组的实现详解

    C语言 栈与数组的实现详解

    栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个...

    m0_520126564972022-11-09
  • C/C++Qt QMessageBox类使用教程

    Qt QMessageBox类使用教程

    QMessageBox类提供一个模态对话框,用于通知用户或询问用户一个问题并接收答案。这篇文章主要介绍了QMessageBox的一些常用用法,需要的小伙伴快来学习一下...

    newname6972022-03-08
  • C/C++C语言结构体使用之链表

    C语言结构体使用之链表

    这篇文章主要介绍了C语言结构体使用之链表,下面文章主要围绕结构体的概念和用法、结构体数组和结构体指针、包含结构体的结构体、结构体搭建链表方...

    Embedded learner9952022-10-13
  • C/C++C++实现LeetCode(13.罗马数字转化成整数)

    C++实现LeetCode(13.罗马数字转化成整数)

    这篇文章主要介绍了C++实现LeetCode(13.罗马数字转化成整数),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考...

    Grandyang9232021-11-24
  • C/C++C++流程控制中用于跳转的return和goto语句学习教程

    C++流程控制中用于跳转的return和goto语句学习教程

    这篇文章主要介绍了C++流程控制中用于跳转的return和goto语句学习教程,是C++入门学习中的基础知识,需要的朋友可以参考下...

    C++教程网10652021-03-22
  • C/C++浅谈do {...} while (0) 在宏定义中的作用

    浅谈do {...} while (0) 在宏定义中的作用

    下面小编就为大家带来一篇浅谈do {...} while (0) 在宏定义中的作用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C语言教程网4552021-04-25