zoukankan      html  css  js  c++  java
  • 扫雷游戏:C++生成一个扫雷底板

    首先,要做扫雷需要有一个底板,然后全部覆盖,每点击一次揭开一部分即可。
    首先我们先写一个生成底板的程序,具体做成界面并用鼠标点击我还不会。

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <ctime>
    #include <windows.h>
    using namespace std;
    struct node
    {
        int mx, my;
    };
    int main()
    {
        srand(time(0));
        int n, m, x, y, k;
        printf("Input the size of the map(n <= 100, m <= 100): ");
        scanf("%d%d", &n, &m);
        char map[400][400];
        printf("Input the numbers of mines: ");
        scanf("%d", &k);
        node *mine = new node[k + 1];
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= m; j++)
            {
                map[i][j] = '0';
            }
        }
        for (int i = 0; i < k; i++)
        {
            int tx, ty;
            tx = rand() % (n - 1);
            ty = rand() % (m - 1);
            mine[i].mx = tx;
            mine[i].my = ty;
        }
        for (int i = 0; i < k; i++)
        {
            map[mine[i].mx + 1][mine[i].my + 1] = '#';
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (map[i][j] == '#')
                {
                    for (int tx = -1; tx <= 1; tx++)
                    {
                        for (int ty = -1; ty <= 1; ty++)
                        {
                            if (map[i + tx][j + ty] != '#')
                            {
                                map[i + tx][j + ty] = ((map[i + tx][j + ty] - '0') + 1) + '0';
                            }
                        }
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                /*if (map[i][j] == '0')
                {
                    cout << "  ";
                    continue;
                }这里可以打开注释*/
                cout << map[i][j] << " ";
            }
            cout << endl;
        }
        return 0;
    }
    

    这个程序,竟然用了四重循环,但是地图一般不会超过100x100,循环也就循环个30000次左右,所以这种算法时间复杂度是足够的。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    结语:我不知道怎么做成窗体,需要用Qt吗?那我就不用C++了,太麻烦了。等我哪天把这个写成Python然后做个简单点的窗体吧。

    或者大佬们帮我做做

  • 相关阅读:
    DB2 字段操作
    解析二维码
    Eclipse tomcat 内存溢出
    hereim_美句_1
    js自定义函数默认参数的写法
    PHP和JS判断访问客户端的是PC还是移动设备
    lampp服务器配置httpd-vhosts.conf文件,设置多域名
    价值7000万的商业模式,羊毛出在狗身上,猪来买单
    确保 PHP 应用程序的安全
    美国淘金的故事
  • 原文地址:https://www.cnblogs.com/coding365/p/14650400.html
Copyright © 2011-2022 走看看