• 【【C语言康复训练-4】】


    C语言康复训练-4

    head.h

    #pragma once
    #define ROWS 11
    #define COLS 11
    #define ROW 9//为什么会在头文件中定义两个 因为1到9是我们想要实现的标准单元
    #define COL 9 //但是对于我们幕后调控者,对边角上并不能和其他一样方便操作,所以我们向外拓展了一圈
    #define number 10
    void menu();
    void game();
    void initboard(char board[ROWS][COLS], int rows, int cols, char ret);
    void displayboard(char board[ROWS][COLS], int row, int col);
    void mailei(char board[ROWS][COLS], int row, int col);
    void pailei(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
    int tongji(char board[ROWS][COLS], int row, int col);
    void jifa(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
    void biaoji(char show[ROWS][COLS], int row, int col);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    head.c

    #define _CRT_SECURE_NO_WARNINGS  1
    #include
    #include
    #include
    #include"head.h"
    
    
    int main()
    {
    	int input = 0;
    	srand((unsigned int)time(NULL));
    	do
    	{
    		menu();
    		printf("请开始你的选择:\n");
    		scanf("%d", &input);
    		switch (input)
    		{
    		case 1:  printf("游戏开始:\n");game(); break;
    		case 0: printf("爱玩玩,不玩滚\n"); break;
    		default: printf("输入非法,请重试\n"); break;
    		}
    	} while (input);
    	return 0;
    }
    
    • 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

    project.c

    #define _CRT_SECURE_NO_WARNINGS  1
    #include
    #include
    #include
    #include"head.h"
    
    void menu()
    {
    	printf("*************************\n");
    	printf("*************************\n");
    	printf("*******1.开始游戏********\n");
    	printf("*******0.退出游戏********\n");
    	printf("*************************\n");
    	printf("*************************\n");
    }
    
    
    void game()
    {
        //进行任何游戏首先第一步是建立一个表格
        char mine[ROWS][COLS] = { 0 };//ROWS和COLS代表行和列因为在此处一直用上我们在头文件中定义
        //这样以后对于行列的修改会更加便利
        char show[ROWS][COLS] = { 0 };//创建一个显示模块,
        //在实现扫雷过程中,我们将埋雷与排雷置于两张表格
        initboard(mine, ROWS, COLS, '0');//初始化埋雷表格,置入字符‘0’
        initboard(show, ROWS, COLS, '*');
        //初始化结束,对于玩家来说到这里什么也没有,那么我先写一个显示模块
       // displayboard(mine, ROW, COL);//地雷模块我们一般显示只是为了确认正确性
       // displayboard(show, ROW, COL);//显示模块是显示在屏幕上的我们把自己增加的外围去掉用ROW
        //当初始化完成之后,我们需要真正开始游戏
        mailei(mine, ROW, COL);//埋雷
       // displayboard(mine, ROW, COL);
        displayboard(show, ROW, COL);//display只是为了调试时候方便 最后哪个不好看,自己删去即可
        //雷埋好了,也显示出来了我们需要去排雷
        pailei(mine, show, ROW, COL);
    }
    
    //初始化
    void initboard(char board[ROWS][COLS], int rows, int cols, char ret)
    {
        int i = 0;
        int j = 0;//设定两个数开始循环
        for (i = 0; i < rows; i++)
        {
            for (j = 0; j < cols; j++)
            {
                board[i][j] = ret;//为什么要设置ret 因为我们在初始化显示模块与地雷模块时赋予了不同的值
                //为了使两个模块在一次初始化中就完成操作
            }
        }
    
    }
    //显示模块
    void displayboard(char board[ROWS][COLS], int row, int col)
    {
        //依次打印值喽
        int i = 0;
        int j = 0;
        for (i = 0; i <= row; i++)//第一行打印 数字序列 1到9
        {
            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 mailei(char board[ROWS][COLS], int row, int col)
    {
        //我们需要电脑生成随机数。随机生成地雷
        int x = 0;
        int y = 0;
        int count = number;//头文件定义雷的数量是10个
        while (count)//怎么保证有指定数量的雷呢,这里用count--
        {
            x = rand() % row + 1;//指的是1-9
            y = rand() % col + 1;//rand模块我们需要去game函数中定义
            if (board[x][y] == '0')
            {
                board[x][y] = '1';
                count--;
            }
        }
    }
    //排雷模块
    void pailei(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
    {
        int x = 0;
        int y = 0;
        int win = 0;
        char ch = 0;
        while (win < row * col - number)
        {
            printf("请输入雷区坐标:\n");//让用户输入坐标
            scanf("%d%d", &x, &y);
            //输入坐标我们首先判断是不是无效坐标
            if (x >= 1 && y >= 1 && x <= row && y <= col)
            {
                if (mine[x][y] == '1')
                {
                    printf("你输了\n");
                    displayboard(mine, ROW, COL);
                    break;
                }
                else
                {
                    //此处不是地雷,我们需要激发周围的对象,就像原版扫雷一样,炸开直至看到是地雷的序号
                    //需要我们去递归进行操作
                    jifa(mine, show, row, col, x, y);
                    displayboard(show, row, col);
                   
                        printf("是否标记:Y,不需要标记:N\n");
                    while ((ch = getchar()) != '\n');//剔除掉我们最喜欢摁的回车
                    scanf("%c", &ch);
                    switch (ch)
                    {
                    case 'Y':
                        biaoji(show, row, col);
                        break;
                    default:
                        break;
                    }
                }
            }
            else
            {
                printf("字符非法,请重新输入:");
            }
        }
        if (win >= row * col - number)
        {
            printf("扫雷成功");
        }
        else
        {
            printf("扫雷失败\n");
        }
    }
    //统计模块
    
    
    
    int tongji(char mine[ROWS][COLS], int x, int y)
    {
        //因为我们是字符0,地雷为字符1 ,根据ASICII码值我们得出结论‘1’-‘0’=1
        return  mine[x - 1][y] +
            mine[x - 1][y - 1] +
            mine[x - 1][y + 1] +
            mine[x][y - 1] +
            mine[x - 1][y + 1] +
            mine[x + 1][y - 1] +
            mine[x + 1][y] +
            mine[x + 1][y + 1] - 8 * '0';
    }
    //激发模块
    void jifa(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
    {
        //激发面对的是如果
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            int z = tongji(mine, x, y);
            if (z == 0)
            {
                //把附近没有地雷的位置变成字符 “空格”
                show[x][y] = ' ';
                int i = 0;
                //向四周共8个位置递归调用
                for (i = x - 1; i <= x + 1; i++)//3行
                {
                    int j = 0;
                    for (j = y - 1; j <= y + 1; j++)//3列
                    {
                        if (show[i][j] == '*')
                        {
                            jifa(mine, show, row, col, i, j);
                        }
                    }
                }
            }
            else
            {
                show[x][y] = z + '0';
            }
        }
    }
    //标记模块
    void biaoji(char show[ROWS][COLS], int row, int col)
    {
        int x = 0;
        int y = 0;
        while (1)
        {
            printf("请输入要标记的坐标:");
            scanf("%d%d", &x, &y);
            if (x >= 1 && x <= row && y >= 1 && y <= col)
            {
                if (show[x][y] == '*')
                {
                    show[x][y] = '!';
                    break;
                }
                else
                {
                    printf("输入非法,请重新输入:\n");
                }
            }
            else
            {
                printf("输入非法,请重新输入:\n");
            }
        }
        displayboard(show, row, col);
    }
    
    • 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
  • 相关阅读:
    工程流体力学复习
    CMMI是什么? 看完这篇你就懂了
    VMware虚拟网络编辑器配置
    Linux系统 (二)- 指令学习2
    go语言学习-基本概念与流程控制
    day03-前端配置、跨域问题、后端数据库迁移、后台主页功能、后台管理
    Vue基础二之全局API、实例属性和全局配置,以及组件进阶(mixins)的详细教程(案列实现,详细图解,附源码)
    [git] git diff
    Android选项卡TabHost
    java毕业设计在线航班订票系统Mybatis+系统+数据库+调试部署
  • 原文地址:https://blog.csdn.net/weixin_50965981/article/details/132781438