1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: MIT |
3 | |
4 | #include "memory.h" |
5 | #include <random> |
6 | |
7 | int main() |
8 | { |
9 | auto main_window = MainWindow::create(); |
10 | auto old_tiles = main_window->get_memory_tiles(); |
11 | std::vector<TileData> new_tiles; |
12 | new_tiles.reserve(old_tiles->row_count() * 2); |
13 | for (int i = 0; i < old_tiles->row_count(); ++i) { |
14 | new_tiles.push_back(*old_tiles->row_data(i)); |
15 | new_tiles.push_back(*old_tiles->row_data(i)); |
16 | } |
17 | std::default_random_engine rng {}; |
18 | std::shuffle(std::begin(new_tiles), std::end(new_tiles), rng); |
19 | auto tiles_model = std::make_shared<slint::VectorModel<TileData>>(new_tiles); |
20 | main_window->set_memory_tiles(tiles_model); |
21 | |
22 | main_window->on_check_if_pair_solved( |
23 | [main_window_weak = slint::ComponentWeakHandle(main_window)] { |
24 | auto main_window = *main_window_weak.lock(); |
25 | auto tiles_model = main_window->get_memory_tiles(); |
26 | int first_visible_index = -1; |
27 | TileData first_visible_tile; |
28 | for (int i = 0; i < tiles_model->row_count(); ++i) { |
29 | auto tile = *tiles_model->row_data(i); |
30 | if (!tile.image_visible || tile.solved) |
31 | continue; |
32 | if (first_visible_index == -1) { |
33 | first_visible_index = i; |
34 | first_visible_tile = tile; |
35 | continue; |
36 | } |
37 | bool is_pair_solved = tile == first_visible_tile; |
38 | if (is_pair_solved) { |
39 | first_visible_tile.solved = true; |
40 | tiles_model->set_row_data(first_visible_index, first_visible_tile); |
41 | tile.solved = true; |
42 | tiles_model->set_row_data(i, tile); |
43 | return; |
44 | } |
45 | main_window->set_disable_tiles(true); |
46 | |
47 | slint::Timer::single_shot(std::chrono::seconds(1), [=]() mutable { |
48 | main_window->set_disable_tiles(false); |
49 | first_visible_tile.image_visible = false; |
50 | tiles_model->set_row_data(first_visible_index, first_visible_tile); |
51 | tile.image_visible = false; |
52 | tiles_model->set_row_data(i, tile); |
53 | }); |
54 | } |
55 | }); |
56 | |
57 | main_window->run(); |
58 | } |
59 | |