-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.html
126 lines (117 loc) · 3.29 KB
/
random.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<html>
<head>
<title>Rummi random runs</title>
<style type='text/css'>
span.tilecolour-R { background: red; color: white }
span.tilecolour-T { background: aqua; color: black }
span.tilecolour-Y { background: orange; color: black }
span.tilecolour-B { background: black; color: white }
li {
margin: 1ex;
border: 1px solid black;
padding: 2ex 2ex 2ex 1ex;
list-style: none;
width: fit-content;
background: #e0e0e0;
display: inline-block;
}
.cards {
padding: 2ex 2ex 2ex 1ex;
line-height: 2em;
}
span.tile { margin-left: 1ex; padding: 4px }
span.joker { font-weight: bold }
#tosolve span { margin-left: 1ex; padding: 4px }
#tosolve span.joker { font-weight: bold }
#tosolve { margin: 1ex; }
</style>
<script src='./rummi.js'></script>
<script src='./fullcases.js'></script>
<script>
function clear() {
for (let l of ['hands']) {
const e = document.getElementById(l);
while (e.firstChild) e.removeChild(e.firstChild);
}
}
function get_worker() {
if (!window.solve_worker) solve_worker = new Worker('./worker.js');
return solve_worker;
}
function generate() {
clear();
input = [];
const count = document.getElementById('count').value - 0,
size = document.getElementById('size').value - 0;
get_worker();
for (let i = 0; i < count; ++i) {
console.log(i);
const tiles = bag().slice(0, size),
disp = document.getElementById('template').firstElementChild.cloneNode(true),
cards = disp.querySelector('.cards');
input.push({in: tiles, el: disp, num: input.length});
tiles.map(c => prettytile(c, document)).forEach(el => {
el.classList.add('tile');
cards.appendChild(el) ;
cards.append(document.createTextNode(' '));
});
document.getElementById('hands').appendChild(disp);
}
solve_worker.onmessage = handleSolve;
requestSolve(0);
}
function requestSolve(i) {
console.log('request', i, input[i]);
solve_worker.postMessage(input[i].in);
}
function handleSolve(msg) {
const d = msg.data;
let index = 0;
console.log('handler', d);
for (let i = 0; i < input.length; ++i)
if (input[i]) {
index = i;
break;
}
const detail = input[index];
if (d.completed) {
if (d.solution) {
for (let meld of d.solution) {
const li = itemof(meld);
detail.el.querySelector('.solution').appendChild(li);
}
}
detail.el.querySelector('.result').textContent = ((d.succeeded)?'Solved':'Unsolved') +
' in ' + d.elapsed + 'ms';
input[index] = null;
if (++index < input.length)
requestSolve(index);
} else {
detail.el.querySelector('.result').textContent = 'Thinking (' + Math.floor((new Date().getTime() - d.start_time)/1000) + 's)';
}
}
</script>
</head>
<body>
<div>
<p>Colours: R T Y B</p>
<p>Ranks: 1 - 13</p>
<p>Jokers: JS JD JM JC</p>
</div>
<form>
<input id='count'></input>
<input id='size'></input>
<button type='button' onclick='generate()'>Go</button>
</form>
<div id='hands'>
</div>
<div id='template'>
<div class='each'>
<p class='cards'></p>
<p class='result'></p>
<ul class='solution'></ul>
</div>
</div>
<p id='status'></p>
</body>
</html>