Creating The Tiles From C++

This step places the game tiles randomly.

Change the main function and includes in src/main.cpp to the following:

// ...

#include <random> // Added

int main()
{
    auto main_window = MainWindow::create();
    auto old_tiles = main_window->get_memory_tiles();
    std::vector<TileData> new_tiles;
    new_tiles.reserve(old_tiles->row_count() * 2);
    for (int i = 0; i < old_tiles->row_count(); ++i) {
        new_tiles.push_back(*old_tiles->row_data(i));
        new_tiles.push_back(*old_tiles->row_data(i));
    }
    std::default_random_engine rng {};
    std::shuffle(new_tiles.begin(), new_tiles.end(), rng);
    auto tiles_model = std::make_shared<slint::VectorModel<TileData>>(new_tiles);
    main_window->set_memory_tiles(tiles_model);

    main_window->run();
}

The code takes the list of tiles, duplicates it, and shuffles it, accessing the memory_tiles property through the C++ code.

For each top-level property, Slint generates a getter and a setter function. In this case get_memory_tiles and set_memory_tiles. Since memory_tiles is a Slint array, it's represented as a std::shared_ptr<slint::Model>.

You can't change the model generated by Slint, but you can extract the tiles from it and put them in a slint::VectorModel which inherits from Model. VectorModel lets you make changes and you can use it to replace the static generated model.

Running this code opens a window that now shows a 4 by 4 grid of rectangles, which show or hide the icons when a player clicks on them.

There's one last aspect missing now, the rules for the game.