-
Notifications
You must be signed in to change notification settings - Fork 5
/
script8.js
627 lines (521 loc) · 19.8 KB
/
script8.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
var game = new Game();
console.log(game.tetramino, JSON.stringify(game.to_shape()))
const Keybind = {'keydown':{}, 'keyup':{}}
var Config = {'das':100, 'arr':0, 'delay':0, 'pressing_left':false, 'pressing_right': false, 'pressing_down': false, 'pressing':{},
'skim_ind':true, 'mdhole_ind':false, 'unqiue_ind':true, 'auto_next_ind':true,
'mode':'1', 'no_of_unreserved_piece':7, 'no_of_piece':7,
'no_of_trial':0, 'no_of_success':0}
var Customized_key = ['ArrowLeft','ArrowRight','ArrowDown','Space','KeyZ','KeyX','KeyA','ShiftLeft','KeyR','KeyP']
var board = document.getElementById('board')
const clone = (items) => items.map(item => Array.isArray(item) ? clone(item) : item);
function clone2d(arr){
var newarr = []
for (var sub_arr of arr){
newarr.push([...sub_arr])
}
return newarr
}
fetch('./learnfromai.json')
.then(results=>results.json())
.then(d=>{jsondata = d;console.log('loading')})
/*
0. sound effect
*/
const sound={
0:new Audio ("sound/1.ogg"),
1:new Audio ("sound/2.ogg"),
2:new Audio ("sound/3.ogg"),
3:new Audio ("sound/4.ogg"),
4:new Audio ("sound/5.ogg"),
5:new Audio ("sound/6.ogg"),
6:new Audio ("sound/7.ogg"),
win:new Audio ("sound/win.ogg"),
lose:new Audio ("sound/lose.ogg")
}
function play_sound(){
if (game.combo >= 0){
console.log('sing', game.combo)
sound[Math.min(6,game.combo)].cloneNode().play()
}
}
/*
1. html related
*/
function load_setting(){
try{
var storage = localStorage.getItem('Customized_key')
if (storage!= null){
Customized_key = JSON.parse(storage);
Config.das = parseInt(localStorage.getItem('das'))
if (! (Config.das>=1 && Config.das<=200)){
Config.das = 100}
Config.arr = parseInt(localStorage.getItem('arr'))
if (! (Config.arr>=0 && Config.arr<=100)){
Config.arr = 0
}
Config.auto_next_ind = localStorage.getItem('auto_next_ind') != 'false'
}
}
catch(err){
localStorage.clear()
console.log('storage corrupted')
}
for (var i=0; i<10; i++){
document.getElementById('input'+(i+1)).value = Customized_key[i]
}
document.getElementById('input11').value = Config.das
document.getElementById('input12').value = Config.arr
document.getElementById('input12.1').checked = Config.auto_next_ind
}
function load_level(modname){
Config.mode = modname
localStorage.setItem('current_level', modname)
play_a_map()
document.getElementById('input13').innerHTML = jsondata[modname]['display_name'];
}
function load_jsondata(){
var passed_level_list = JSON.parse(localStorage.getItem('passed_level'))
if (passed_level_list == null) passed_level_list = []
var text = ''
for (var modname in jsondata){
label = passed_level_list.includes(modname)? "✅": " "
color = passed_level_list.includes(modname)? "green": "red"; console.log(color,typeof(modname))
display_name = jsondata[modname]["display_name"]
text += `<button class="game_button ${color}" onclick='load_level("${modname}"); board.focus()'>${display_name}${label}</button>`
}
document.getElementById('scrollable').innerHTML = text
}
function save_setting(){
console.log('save_setting')
for (var i=0; i<10; i++){
Customized_key[i] = document.getElementById('input'+(i+1)).value
}
Config.das = parseInt(document.getElementById('input11').value)
if (! (Config.das>=1 && Config.das<=200)){
alert('DAS should be between 1 to 200')
Config.das = 100
}
Config.arr = parseInt(document.getElementById('input12').value)
if (! (Config.arr>=0 && Config.arr<=100)){
alert('ARR should be between 0 to 100')
Config.arr = 0
}
Config.auto_next_ind = document.getElementById('input12.1').checked
update_keybind()
localStorage.setItem('auto_next_ind', Config.auto_next_ind)
localStorage.setItem('Customized_key',JSON.stringify(Customized_key))
localStorage.setItem('das',Config.das)
localStorage.setItem('arr',Config.arr)
}
function save_gamemode(){
console.log('save_gamemode')
Config.mode = selection.options[selection.selectedIndex].value
}
/*
2. render
*/
function render(){
var ctx = document.getElementById("board").getContext('2d');
ctx.clearRect(0,0,520,610);
// render background and margin
var offset_x = 110
var offset_y = 5
ctx.fillStyle = 'black';
ctx.fillRect(offset_x,offset_y,300,600);
ctx.strokeStyle = 'grey';
ctx.strokeRect(offset_x,offset_y,300,600);
// render grid
for (var row=0; row<20; row++)
for (var col=0; col<10; col++){
ctx.strokeRect(col*30+offset_x,(19-row)*30+offset_y,30,30);
}
// render shodow
var min_relative_height = 20
for ([col, row] of game.to_shape()){
var ground_height = 0
for (var i=0; i<row; i++)
if (game.board[i][col] != 'N')
ground_height = i+1
min_relative_height = Math.min(min_relative_height, row - ground_height)
}
ctx.fillStyle = 'grey';
for ([col, row] of game.to_shape())
ctx.fillRect(col*30+offset_x,(19-row+min_relative_height)*30+offset_y,30,30);
// render piece
ctx.fillStyle = color_table[game.tetramino]
for (var [col,row] of game.to_shape()){
ctx.fillRect(col*30+offset_x,(19-row)*30+offset_y,30,30)
}
// render board
ctx.strokeStyle = 'grey';
for (var row=0; row<20; row++)
for (var col=0; col<10; col++){
if (game.board[row][col] != 'N'){
ctx.fillStyle = color_table[game.board[row][col]];
ctx.fillRect(col*30+offset_x,(19-row)*30+offset_y,30,30);
}
}
// render hold
var offset_x = 5
var offset_y = 5
ctx.fillStyle = 'black';
ctx.fillRect(offset_x,offset_y,100,100);
ctx.strokeStyle = 'grey';
ctx.strokeRect(offset_x,offset_y,100,100);
if (game.holdmino != ''){
ctx.fillStyle = color_table[game.holdmino]
for (var [col, row] of game.to_shape(game.holdmino)){
var piece_offset = 'IO'.includes(game.holdmino)? 10: 20;
ctx.fillRect((col+1)*20+offset_x+piece_offset,(3-row)*20+offset_y,20,20);
}
}
// render next
var offset_x = 415
var offset_y = 5
ctx.fillStyle = 'black';
ctx.fillRect(offset_x,offset_y,100,410);
ctx.strokeStyle = 'grey';
ctx.strokeRect(offset_x,offset_y,100,410);
for (var piece_idx=1; piece_idx<6; piece_idx++){
ctx.fillStyle = color_table[game.bag[piece_idx]]
for (var [col, row] of game.to_shape(game.bag[piece_idx])){
var piece_offset = 'IO'.includes(game.bag[piece_idx])? 10: 20;
ctx.fillRect((col+1)*20+offset_x+piece_offset,(3-row)*20+(piece_idx-1)*80+offset_y,20,20);
}
}
// render game stat
ctx.fillStyle = 'green';
ctx.font = "bold 20px Arial ";
if (game.line_clear>0 && game.line_clear<4 && game.b2b>=0)ctx.fillText('TSPIN',10,150)
if (game.line_clear>0)ctx.fillText(['','Single','Double','Triple','Quad'][game.line_clear],10,200)
if (game.combo > 0) ctx.fillText(game.combo+' Combo',10,300)
if (game.pc) ctx.fillText('All Clear',10,350)
ctx.fillText('Trial:',420,450)
ctx.fillText(Config.no_of_success+'/'+Config.no_of_trial,420,470)
}
/*
3. keybind
*/
function press_left(first_call = false){
if (first_call && !Config.pressing_left){
Config.timer1 = new Date().getTime();
Config.delay = Config.das;
game.move_left()
render();
Config.pressing_left = true;
Config.pressing_right = false;
setTimeout(press_left, 1)
}
else if (!first_call && Config.pressing_left){
var now = new Date().getTime()
console.log(now - Config.timer1)
if (now - Config.timer1 > Config.delay){
Config.arr===0? game.move_leftmost(): game.move_left()
render();
Config.delay = Config.arr
Config.timer1 = now
}
setTimeout(press_left, 1)
}
}
function release_left(){
Config.pressing_left = false;
}
function press_right(first_call = false){
if (first_call && !Config.pressing_right){
Config.timer2 = new Date().getTime();
Config.delay = Config.das;
game.move_right()
render();
Config.pressing_right = true;
Config.pressing_left = false;
setTimeout(press_right, 1)
}
else if (!first_call && Config.pressing_right){
var now = new Date().getTime()
console.log(now - Config.timer2)
if (now - Config.timer2 > Config.delay){
Config.arr===0? game.move_rightmost(): game.move_right()
render();
Config.delay = Config.arr
Config.timer2 = now
}
setTimeout(press_right, 1)
}
}
function release_right(){
Config.pressing_right = false;
}
function press_down(first_call = false){
if (first_call && !Config.pressing_down){
game.softdrop()
render();
Config.pressing_down = true;
setTimeout(press_down, 1)
}
else if (!first_call && Config.pressing_down){
game.softdrop()
render();
setTimeout(press_down, 1)
}
}
function release_down(){
Config.pressing_down = false;
}
// bind('keydown ArrowRight>', lambda event: (release_right()))
function add_generic_keybind(key,func){
Keybind.keydown[key] = e=>{if (!Config.pressing[key]) func();
render();
Config.pressing[key] = true;
}
Keybind.keyup[key] = e=>{Config.pressing[key] = false}
}
function update_keybind(){
Keybind.keydown = {}
Keybind.keyup = {}
Keybind.keydown[Customized_key[0]] = e=>{press_left(true)}
Keybind.keyup[Customized_key[0]] = e=>{release_left(true)}
Keybind.keydown[Customized_key[1]] = e=>{press_right(true)}
Keybind.keyup[Customized_key[1]] = e=>{release_right(true)}
Keybind.keydown[Customized_key[2]] = e=>{press_down(true)}
Keybind.keyup[Customized_key[2]] = e=>{release_down(true)}
add_generic_keybind(Customized_key[3], ()=> (game.harddrop(), play_sound(), detect_win()))
add_generic_keybind(Customized_key[4], ()=> game.rotate_anticlockwise())
add_generic_keybind(Customized_key[5], ()=> game.rotate_clockwise())
add_generic_keybind(Customized_key[6], ()=> game.rotate_180())
add_generic_keybind(Customized_key[7], ()=> game.hold())
add_generic_keybind(Customized_key[8], ()=> retry())
add_generic_keybind(Customized_key[9], ()=> pcfinder())
}
function set_event_listener(){
document.onkeydown = (e => {
console.log('down',e.code)
var func = Keybind.keydown[e.code];
if (['Space','ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.code) && document.activeElement.tagName != 'INPUT' && document.activeElement.tagName != 'TEXTAREA'){
e.preventDefault()}
if (document.activeElement.className == 'keybind'){
document.activeElement.value = e.code
save_setting()
}
if (document.activeElement.tagName != 'INPUT' && document.activeElement.tagName != 'TEXTAREA' && func != undefined){
board.focus()
func()
}
})
document.onkeyup = (e => {
console.log('up',e.code)
var func = Keybind.keyup[e.code];
if (func != undefined){
func()
}
})
board.onfocus = (e =>{
render()
})
board.onblur = (e =>{
var ctx = document.getElementById("board").getContext('2d');
if (!Config.on_focus){
ctx.font = "bold 40px Arial ";
ctx.fillStyle = 'rgba(234,200,0,0.5)'
ctx.fillText(' OUT OF FOCUS',0,300)
}
})
document.getElementById('input11').oninput = e=>{save_setting()}
document.getElementById('input12').oninput = e=>{save_setting()}
document.getElementById('input12.1').onchange = e=>{save_setting()}
const ua = navigator.userAgent;
if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
document.getElementById('board').onblur = (e=>e.preventDefault())
document.getElementById('tcc').style.display = 'inline-block';
} else if (
/Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(ua)
) {
document.getElementById('board').onblur = (e=>e.preventDefault())
document.getElementById('tcc').style.display = 'inline-block';
} // else document.getElementById("tcc").style.display = 'none';
document.getElementById('tc-dr').addEventListener('touchstart', function (e) {
game.rotate_180()
render()
});
document.getElementById('tc-h').addEventListener('touchstart', function (e) {
game.hold()
render()
});
document.getElementById('tc-hd').addEventListener('touchstart', function (e) {
game.harddrop()
play_sound()
detect_win()
render()
});
document.getElementById('tc-l').addEventListener('touchstart', function (e) {
press_left(true)
render()
});
document.getElementById('tc-l').addEventListener('touchend', function (e) {
release_left(true)
render()
});
document.getElementById('tc-r').addEventListener('touchstart', function (e) {
press_right(true)
render()
});
document.getElementById('tc-r').addEventListener('touchend', function (e) {
release_right(true)
render()
});
document.getElementById('tc-d').addEventListener('touchstart', function (e) {
press_down(true)
render()
});
document.getElementById('tc-d').addEventListener('touchend', function (e) {
release_down(true)
render()
});
document.getElementById('tc-cc').addEventListener('touchstart', function (e) {
game.rotate_anticlockwise()
render()
});
document.getElementById('tc-c').addEventListener('touchstart', function (e) {
game.rotate_clockwise()
render()
});
}
/*
4. map generation
*/
Record={
added_line:[],
board:[],
piece_added:[],
shuffled_queue:['I','O','J','L','S','Z','T'],
finished_map:[
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'],
['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N']],
tsd: 0,
quadL: 0,
pc: 0,
max_combo: 0,
}
function random_choose(arr){
var idx = Math.floor(Math.random() * arr.length)
return arr[idx]
}
function get_shuffled_holdable_queue(queue){
var result = []
var size = queue.length
if (2<= size && size<=7){
var rng = Math.floor(Math.random()*reverse_hold_table[size].length)
var selected_table = reverse_hold_table[size][rng]
for (var pointer of selected_table){
result.push(queue[pointer])}
}
return result
}
// 4.4 shuffle queue and play / restart
function play(){
Record.tsd = Record.quad = Record.pc = Record.max_combo = 0
game = new Game()
game.board = clone(Record.finished_map)
game.bag = Record.piece_added.concat(['G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'])
game.update()
game.holdmino = ''
}
function play_a_map(){
mode = Config.mode
game = new Game()
if (jsondata[mode]['winning_requirement']["max_combo"] > 0)
document.getElementById('winning_requirement1').innerHTML = jsondata[mode]['winning_requirement']["max_combo"]+' Combo'
else
document.getElementById('winning_requirement1').innerHTML = ''
if (jsondata[mode]['winning_requirement']["tsd"] > 0)
document.getElementById('winning_requirement2').innerHTML = jsondata[mode]['winning_requirement']["tsd"]+' Tspin Double'
else
document.getElementById('winning_requirement2').innerHTML = ''
if (jsondata[mode]['winning_requirement']["quad"] > 0)
document.getElementById('winning_requirement3').innerHTML = jsondata[mode]['winning_requirement']["quad"]+' Quad'
else
document.getElementById('winning_requirement3').innerHTML = ''
if (jsondata[mode]['winning_requirement']["pc"] > 0)
document.getElementById('winning_requirement4').innerHTML = jsondata[mode]['winning_requirement']["pc"]+' Perfect Clear'
else
document.getElementById('winning_requirement4').innerHTML = ''
Config.no_of_piece = jsondata[mode]["no_of_piece"]
game.board = clone2d(jsondata[mode]['board']).reverse()
Record.finished_map = clone(game.board)
Record.piece_added = [...jsondata[mode]['queue']]
play()
render()
}
function detect_win(){
if (game.total_piece == 1){
Config.no_of_trial += 1
}
mode = Config.mode
if (game.line_clear == 2 && game.b2b >= 0) Record.tsd++
if (game.line_clear == 4) Record.quad++
if (game.pc) Record.pc ++
if (game.combo > Record.max_combo) Record.max_combo = game.combo
if (game.total_piece == Config.no_of_piece){
if (Record.tsd >= jsondata[mode]["winning_requirement"]['tsd'] &&
Record.quad >= jsondata[mode]["winning_requirement"]['quad'] &&
Record.pc >= jsondata[mode]["winning_requirement"]['pc'] &&
Record.max_combo >= jsondata[mode]["winning_requirement"]['max_combo']){
sound['win'].play()
if (Config.auto_next_ind){
play_a_map()}
Config.no_of_success += 1
var passed_level_list = JSON.parse(localStorage.getItem('passed_level'))
if (passed_level_list == null)
passed_level_list = []
if(!passed_level_list.includes(Config.mode))
localStorage.setItem('passed_level', JSON.stringify(passed_level_list.concat(Config.mode)))
load_jsondata()
}
else{
sound['lose'].play()
retry()
}
}
}
function retry(){
play()
render()
}
function show_ans(){
}
/*
5. start
*/
set_event_listener()
load_setting()
update_keybind()
function initialize(){
load_jsondata()
var current_level = localStorage.getItem('current_level')
if (current_level == null) current_level = "1"
try{
load_level(current_level)
}
catch(err){
load_level('1')
}
}
setTimeout(initialize,1000)