-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmoetris.js
More file actions
429 lines (428 loc) · 14.1 KB
/
moetris.js
File metadata and controls
429 lines (428 loc) · 14.1 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Generated by LiveScript 1.2.0
(function(){
var split$ = ''.split, replace$ = ''.replace;
$(function(){
var corpus;
corpus = "moetris.txt";
if (/^#[htpb]/.exec(location.hash)) {
corpus = "moetris." + location.hash.substr(1, 1) + ".txt";
}
return $.get(corpus, null, function(ALL){
var matchCache, keys, keyMap, i$, len$, idx, keyCode, score, ice, fire, time, w, h, tap, cs, select, IsTouchDevice, o, draw, resumeFalling, doGravity, doit, blacklist;
window.ALL = ALL;
matchCache = {};
keys = [122, 120, 99, 118, 98, 110, 97, 115, 100, 102, 103, 104, 113, 119, 101, 114, 116, 121, 49, 50, 51, 52, 53, 54, 90, 88, 67, 86, 66, 78, 65, 83, 68, 70, 71, 72, 81, 87, 69, 82, 84, 89, 33, 64, 35, 36, 37, 94];
keyMap = {};
for (i$ = 0, len$ = keys.length; i$ < len$; ++i$) {
idx = i$;
keyCode = keys[i$];
keyMap[keyCode] = {
'key': String.fromCharCode(keyCode),
'x': ~~(idx % 6),
'y': ~~(idx / 6)
};
}
score = 0;
ice = fire = time = 0;
w = 75;
h = 57;
$('big').remove();
tap = IsTouchDevice ? 'touchstart' : 'click';
$(document).on('keypress', function(arg$){
var which, pos;
which = arg$.which;
switch (which) {
case 105:
return $('.ice.button').trigger(tap);
case 111:
return $('.fire.button').trigger(tap);
case 112:
return $('.time.button').trigger(tap);
case 32:
return $('#wrap').trigger(tap);
default:
if (~keys.indexOf(which)) {
pos = keyMap[which];
return select($(".char.col-" + pos.x).eq(pos.y));
}
}
});
cs = '';
select = function(it){
var c, idx;
if ($('body').hasClass('finished')) {
return;
}
c = it.find('big').text();
if (it.hasClass('active')) {
if (~(idx = cs.lastIndexOf(c))) {
it.removeClass("active red green");
draw(cs.substring(0, idx) + cs.substring(idx + 1));
}
return;
}
it.addClass('active');
cs += c;
return draw(cs);
};
IsTouchDevice = 'ontouchstart' in window || in$('onmsgesturechange', window);
if (IsTouchDevice) {
$('body').addClass('touch');
$('body').on('touchstart', '.char', function(){
return select($(this));
});
} else {
$('body').on(tap, '.char', function(){
return select($(this));
});
}
$('.ice.button').on(tap, function(){
if ($('body').hasClass('frozen')) {
return;
}
if (ice <= 0) {
return;
}
$('#ice').text(--ice);
if (!ice) {
$('.ice.button').addClass('disabled');
}
$('body').addClass('frozen');
return $('.falling').stop();
});
$('.fire.button').on(tap, function(){
var i$, c, xs;
if (fire <= 0) {
return;
}
$('#fire').text(--fire);
if (!fire) {
$('.fire.button').addClass('disabled');
}
for (i$ = 0; i$ <= 5; ++i$) {
c = i$;
xs = $(".col-" + c + ":not(.falling)").get();
xs.sort(fn$);
$(xs[0]).detach().trigger('detached').remove();
}
return doGravity();
function fn$(a, b){
return $(b).css('top') - $(a).css('top');
}
});
$('.time.button').on(tap, function(){
if (time <= 0) {
return;
}
$('#time').text(--time);
if (!time) {
$('.time.button').addClass('disabled');
}
if ($('body').hasClass('paused')) {
return;
}
$('body').addClass('paused');
$('.falling').stop();
return $('#special').fadeOut({
duration: 10000,
easting: 'linear',
complete: function(){
$('#special').show();
$('body').removeClass('paused');
return resumeFalling();
}
});
});
o = function(it){
return (split$.call(it, '')).sort().join('');
};
draw = function(it){
var ref$, that;
cs = it;
$('#msg').text(cs);
$('#wrap').addClass('input');
if (!cs.length) {
return $('#wrap').removeClass("active red green");
}
$('#wrap').addClass('active');
if (location.hash === '#p') {
cs = o(cs);
}
if ((ref$ = location.hash) === '#p' || ref$ === '#b') {
if (that = ALL.match(RegExp('("' + cs + '":\\S+)'))) {
$('#msg').text(that[1]);
$('.active').removeClass('red').addClass('green');
return;
}
} else if (~ALL.indexOf("\"" + cs + "\"")) {
return $('.active').removeClass('red').addClass('green');
}
if (that = /[*?]/.exec(cs) && !/^[*?]+$/.test(cs) && ALL.match(RegExp('"(' + cs.replace(/?/g, '[^"]').replace(/*/g, '(?:[^"]+)?') + ')"(:\\S+)?'))) {
$('#msg').text(that[1] + ((ref$ = that[2]) != null ? ref$ : ''));
$('.active').removeClass('red').addClass('green');
return;
}
return $('.active').removeClass('green').addClass('red');
};
$('#top').css({
left: '5px',
width: 6 * w + "px",
top: '5px'
});
$('#proto').css({
left: '5px',
width: 10 + 6 * w + "px",
height: h + "px",
top: 20 + 9 * h
});
$('#special').css({
left: '5px',
width: 10 + 6 * w + "px",
height: h + "px",
top: 25
});
$('#msg').show();
$('.icon.loading').remove();
$('#wrap').removeClass('red').addClass('green').on(tap, function(){
if (!$('body').hasClass('started')) {
$('body').addClass('started');
$(this).removeClass('green');
$('#msg').text('');
$('#help').remove();
doit();
return;
}
if ($(this).hasClass('red')) {
$('.active').removeClass('active').removeClass('red');
return draw('');
}
if ($(this).hasClass('green')) { //成功消掉時
var tower = $('#tower').html(); tower = tower.substr(1, tower.length-2);
$('#tower').html('[' + $(this).text().split('space') + tower +']'); // 加入字塔中
score += $(this).text().length;
$('#score').text(score);
$(this).removeClass('active').removeClass('green');
$('#ice').text(ice += $(".active .tint").length);
$('#fire').text(fire += $(".active .fire").length);
$('#time').text(time += $(".active .time").length);
if (ice) {
$('.ice.button').removeClass('disabled');
}
if (fire) {
$('.fire.button').removeClass('disabled');
}
if (time) {
$('.time.button').removeClass('disabled');
}
$('.active').detach().trigger('detached').remove();
doGravity();
draw('');
} else {
$('.falling').finish();
}
if ($('body').hasClass('frozen')) {
$('body').removeClass('frozen');
return resumeFalling();
}
});
resumeFalling = function(){
var $x;
if ($('body').hasClass('frozen')) {
return;
}
if ($('body').hasClass('paused')) {
return;
}
$x = $('.falling');
if (!$x.length) {
return doit();
}
return $x.animate({
top: $x.data('top')
}, $x.data('speed'), 'linear', function(){
$('.falling').removeClass('falling');
doGravity();
return doit();
});
};
doGravity = function(){
var i$, c, lresult$, xs, below, j$, len$, x, top, results$ = [];
for (i$ = 0; i$ <= 5; ++i$) {
c = i$;
lresult$ = [];
xs = $(".col-" + c + ":not(.falling)").get();
xs.sort(fn$);
below = 0;
for (j$ = 0, len$ = xs.length; j$ < len$; ++j$) {
x = xs[j$];
below++;
top = 72 + (8 - below) * h;
if (top === $(x).css('top')) {
continue;
}
lresult$.push($(x).animate({
top: top
}, 50, 'linear'));
}
results$.push(lresult$);
}
return results$;
function fn$(a, b){
return $(b).css('top') - $(a).css('top');
}
};
doit = function(){
var min, i$, c, cnt, col, next, special, $x, below, $access, ref$, top, speed;
min = Infinity;
for (i$ = 0; i$ <= 5; ++i$) {
c = i$;
cnt = $(".col-" + c).length;
if (min <= cnt) {
continue;
}
min = cnt;
col = c;
}
next = pick($('big').text());
if (Math.random() < 0.1) {
special = ['fire', 'tint', 'time'][Math.floor(Math.random() * 3)];
}
$x = $('<div/>', {
'class': "ui char button large col-" + col
}).append($('<big/>').text(next).addClass(next === '?'
? 'qq'
: next === '*' ? 'aa' : 'ww')).append($('<i/>', {
'class': "icon " + special
}));
$x.css({
display: 'inline-block',
position: 'absolute',
left: col * w + 10
});
$x.appendTo('body');
below = $(".col-" + col).length;
$access = $('<div/>', {
'class': 'access'
}).text((ref$ = keyMap[keys[col + (below - 1) * 6]]) != null ? ref$.key : void 8);
$x.append($access);
$x.on('detached', function(){
var $chars;
$chars = $(".col-" + col);
return $(".col-" + col + " > .access").each(function(row, element){
var ref$;
return $(element).text((ref$ = keyMap[keys[col + row * 6]]) != null ? ref$.key : void 8);
});
});
if (below > 8) {
$('body').addClass('finished');
$('.button').off(tap).off('touchstart');
$('#msg').text('再玩一次').prepend($('<i/>', {
'class': "icon repeat"
}));
$('#wrap').addClass('secondary').on(tap, function(){
return location.reload();
});
return;
}
$x.addClass('falling');
top = 72 + (8 - below) * h;
speed = (9 - below) * (100 > (ref$ = 500 - score) ? 100 : ref$);
return $x.data({
top: top,
speed: speed
}).animate({
top: top
}, speed, 'linear', function(){
$('.falling').removeClass('falling');
doGravity();
return doit();
});
};
blacklist = {
"": true
};
function pick(cur){
var seen, scores, i$, ref$, len$, c, results, j$, len1$, r, score, cands, res$, ref1$, s, c2, picks, cand;
cur == null && (cur = '');
if (!(/[^*?]/.exec(cur) || location.hash !== '#b')) {
return "ㄅㄆㄇ"[Math.floor(Math.random() * 3)];
}
if (!(/[^*?]/.exec(cur) || location.hash !== '#p')) {
return "一丁之"[Math.floor(Math.random() * 3)];
}
if (!/[^*?]/.test(cur)) {
return "萌的不一我是人有了大國來生在子們中上他時小地出以學可自這會成家到為天心年然要得說過個著能下動發臺麼車那行經去好開現就作後多方如事公看也長面力起裡高用業你因而分市於道外沒無同法前水電民對兒日之文當教新意情所實工全定美理頭本明氣進樣都主間老想重體山物知手相回性果政只此代和活親化加影什身己灣機部常見其正世"[Math.floor(Math.random() * 150)];
}
if (Math.random() < 0.05) {
return "*";
}
if (Math.random() < 0.2) {
return "?";
}
seen = {};
scores = [];
for (i$ = 0, len$ = (ref$ = replace$.call(cur, /[*?]/g, '')).length; i$ < len$; ++i$) {
c = ref$[i$];
results = matchCache[c] || (matchCache[c] = ALL.match(RegExp('[^"]*' + c + '[^"]*', 'g')));
for (j$ = 0, len1$ = results.length; j$ < len1$; ++j$) {
r = results[j$];
if (r.length <= 8 && !blacklist[r]) {
seen[r] == null && (seen[r] = 0);
score = ++seen[r];
if (r.length > score + 1) {
scores[score] == null && (scores[score] = "");
scores[score] += r + "\n";
} else {
blacklist[r] = true;
}
}
}
}
cands = [];
while (!((cands != null && cands.length) || !scores.length)) {
res$ = [];
for (i$ = 0, len$ = (ref$ = split$.call((ref1$ = scores.pop()) != null ? ref1$ : '', "\n")).length; i$ < len$; ++i$) {
s = ref$[i$];
if (!blacklist[s]) {
res$.push(s);
}
}
cands = res$;
}
while (cands.length <= 5 && scores.length) {
c2 = [];
while (!((c2 != null && c2.length) || !scores.length)) {
res$ = [];
for (i$ = 0, len$ = (ref$ = split$.call((ref1$ = scores.pop()) != null ? ref1$ : '', "\n")).length; i$ < len$; ++i$) {
s = ref$[i$];
if (!blacklist[s]) {
res$.push(s);
}
}
c2 = res$;
}
cands = cands.concat(c2);
}
picks = "";
for (i$ = 0, len$ = cands.length; i$ < len$; ++i$) {
cand = cands[i$];
for (j$ = 0, len1$ = (ref$ = replace$.call(cur, /[*?]/g, '')).length; j$ < len1$; ++j$) {
c = ref$[j$];
cand = replace$.call(cand, RegExp(c + ''), '');
}
picks += cand;
}
if (!picks) {
return pick();
}
return picks[Math.floor(Math.random() * picks.length)];
}
return pick;
}, 'text');
});
function in$(x, xs){
var i = -1, l = xs.length >>> 0;
while (++i < l) if (x === xs[i]) return true;
return false;
}
}).call(this);