用C++在大小为10*10的二维数组中设置20个随机值[已关闭]

nqwrtyyt  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(315)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

2小时前关门了。
Improve this question
我想在10*10 2d阵列中生成20个随机障碍(“X”),问题是rand函数有时会重复值,因此它会在阵列中的同一位置设置障碍,因此它不会一直有20个障碍
游戏一开始应该示例化一个10x10的Map,其中包括随机分配的宝石(至少40),除了随机分配的障碍(至少20)
example of map

#include <iostream>
#include <ctime>
//#include <conio.h>

using namespace std;

void printMap(string arr[][10])
{

    for (int r = 0; r < 10; r++)
    {
        for (size_t c = 0; c < 10; c++)
        {
            cout << arr[r][c] << "  ";
        }
        cout << endl;
    }
}

int main()
{

    int x, y;

    x = 10;
    y = 10;

    string a[10][10];

    for (size_t i = 0; i < 10; i++)
    {
        for (size_t j = 0; j < 10; j++)
        {
            a[i][j] = ".";
        }
    }

    // allocate 20 random obstacles
    srand(time(0));
    for (int i = 0; i <= 20; i++)
    {
        a[rand() % 9][rand() % 9] = "X";
    }

    // allocate 40 random gems
    srand(time(0));
    for (int f = 0; f <= 40; f++)
    {
        a[rand() % 9][rand() % 9] = "G";
    }
    

    printMap(a);

    return 0;
}
wpcxdonn

wpcxdonn1#

更新的示例在x=(0,9),y=(0,9)范围内生成20个唯一点

#include <vector>
#include <iostream>
#include <random>
#include <set>
#include <format>

//-----------------------------------------------------------------------------
// a struct containing a position on your map/board
struct point_t
{
    std::size_t x;
    std::size_t y;
w
};

//-----------------------------------------------------------------------------
// operators for set to work

bool operator==(const point_t& lhs, const point_t& rhs)
{
    return (lhs.x == rhs.x) && (lhs.y == rhs.y);
}

bool operator<(const point_t& lhs, const point_t& rhs)
{
    if (lhs.x == rhs.x) return (lhs.y < rhs.y);
    return lhs.x < rhs.x;
}

//-----------------------------------------------------------------------------
// creat an array (vector) of points on the board

auto create_random_points(std::size_t board_size, std::size_t number_of_points)
{
    // initialize random generator only on first call to function (static variables).
    static std::mt19937 generator(std::random_device{}());
    static std::uniform_int_distribution<std::size_t> distribution(0, board_size - 1);

    std::set<point_t> points;
    while (points.size() != number_of_points)
    {
        point_t point{ distribution(generator),distribution(generator) };

        // if point not found in points add it.
        if (points.find(point) == points.end())
        {
            points.insert(point);
        }
    }

    return std::vector<point_t>{ points.begin(), points.end() };
}

//-----------------------------------------------------------------------------
// to print positions
std::ostream& operator<<(std::ostream& os, const std::vector<point_t>& vector)
{
    bool comma{ false };

    for (const auto& point : vector)
    {
        if (comma) os << ", ";
        os << std::format("({0},{1})", point.x, point.y);
        comma = true;
    }

    return os;
}

//-----------------------------------------------------------------------------

int main()
{
    auto vector = create_random_points(10ul, 20ul);
    std::cout << vector << "\n";
    std::cout << vector.size() << "\n";
    return 0;
}

相关问题