forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·63 lines (52 loc) · 1.68 KB
/
main.js
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
#!/usr/bin/env node
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
import * as slint from "slint-ui";
let ui = slint.loadFile("memory.slint");
let window = new ui.MainWindow();
let initial_tiles = [...window.memory_tiles];
let tiles = initial_tiles.concat(initial_tiles.map((tile) => Object.assign({}, tile)));
for (let i = tiles.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
[tiles[i], tiles[j]] = [tiles[j], tiles[i]];
}
let model = new slint.ArrayModel(tiles);
window.memory_tiles = model;
window.check_if_pair_solved = function () {
let flipped_tiles = [];
tiles.forEach((tile, index) => {
if (tile.image_visible && !tile.solved) {
flipped_tiles.push({
index,
tile
});
}
});
if (flipped_tiles.length == 2) {
let {
tile: tile1,
index: tile1_index
} = flipped_tiles[0];
let {
tile: tile2,
index: tile2_index
} = flipped_tiles[1];
let is_pair_solved = tile1.image.path === tile2.image.path;
if (is_pair_solved) {
tile1.solved = true;
model.setRowData(tile1_index, tile1);
tile2.solved = true;
model.setRowData(tile2_index, tile2);
} else {
window.disable_tiles = true;
setTimeout(() => {
window.disable_tiles = false;
tile1.image_visible = false;
model.setRowData(tile1_index, tile1);
tile2.image_visible = false;
model.setRowData(tile2_index, tile2);
}, 1000);
}
}
};
window.run();