260 var boardDom = document.getElementById("board"); |
260 var boardDom = document.getElementById("board"); |
261 var ui = {}; |
261 var ui = {}; |
262 ui.board = {}; |
262 ui.board = {}; |
263 ui.board.val2048Dom = document.getElementById('2048'); |
263 ui.board.val2048Dom = document.getElementById('2048'); |
264 ui.board.val2048Dom.addEventListener("click", function(event) { |
264 ui.board.val2048Dom.addEventListener("click", function(event) { |
|
265 event.preventDefault(); event.stopPropagation(); |
265 ui.board.update(ui.board.position); |
266 ui.board.update(ui.board.position); |
266 localStorage.val2048 = ui.board.val2048Dom.checked; |
267 localStorage.val2048 = ui.board.val2048Dom.checked; |
267 event.target.blur(); |
268 event.target.blur(); |
268 }); |
269 }, false); |
269 ui.board.val2048 = function(val) { |
270 ui.board.val2048 = function(val) { |
270 if (ui.board.val2048Dom.checked) |
271 if (ui.board.val2048Dom.checked) |
271 return Math.pow(2, val); |
272 return Math.pow(2, val); |
272 return val; |
273 return val; |
273 } |
274 } |
302 tdDom.appendChild(inputDom); |
303 tdDom.appendChild(inputDom); |
303 inputDom.focus(); |
304 inputDom.focus(); |
304 inputDom.select(); |
305 inputDom.select(); |
305 inputDom.addEventListener("blur", function(tdDom, i, j) { |
306 inputDom.addEventListener("blur", function(tdDom, i, j) { |
306 return function(event) { |
307 return function(event) { |
|
308 event.preventDefault(); event.stopPropagation(); |
307 ui.board.handleEdit(tdDom, i, j); |
309 ui.board.handleEdit(tdDom, i, j); |
308 return false; |
|
309 } |
310 } |
310 }(tdDom, i, j)); |
311 }(tdDom, i, j), false); |
311 } else { |
312 } else { |
312 var inputDom = tdDom.firstChild; |
313 var inputDom = tdDom.firstChild; |
313 var val = parseInt(inputDom.value); |
314 var val = parseInt(inputDom.value); |
314 if (0 <= val && val < 15) { |
315 if (0 <= val && val < 15) { |
315 ui.board.position[i][j] = val; |
316 ui.board.position[i][j] = val; |
325 var tdDoms = trDoms[i].querySelectorAll("td"); |
326 var tdDoms = trDoms[i].querySelectorAll("td"); |
326 for (var j = 0; j < 4; j++) { |
327 for (var j = 0; j < 4; j++) { |
327 var tdDom = tdDoms[j]; |
328 var tdDom = tdDoms[j]; |
328 tdDom.addEventListener("click", function(tdDom, i, j) { |
329 tdDom.addEventListener("click", function(tdDom, i, j) { |
329 return function(event) { |
330 return function(event) { |
|
331 event.preventDefault(); event.stopPropagation(); |
330 ui.board.handleEdit(tdDom, i, j); |
332 ui.board.handleEdit(tdDom, i, j); |
331 return false; |
|
332 } |
333 } |
333 }(tdDom, i, j)); |
334 }(tdDom, i, j), false); |
334 tdDom.addEventListener("keyup", function(tdDom, i, j) { |
335 tdDom.addEventListener("keyup", function(tdDom, i, j) { |
335 return function(event) { |
336 return function(event) { |
|
337 event.preventDefault(); event.stopPropagation(); |
336 if (event.keyCode === 13) |
338 if (event.keyCode === 13) |
337 ui.board.handleEdit(tdDom, i, j); |
339 ui.board.handleEdit(tdDom, i, j); |
338 } |
340 } |
339 }(tdDom, i, j)); |
341 }(tdDom, i, j), false); |
340 } |
342 } |
341 } |
343 } |
342 } |
344 } |
343 ui.board.enableEdit(); |
345 ui.board.enableEdit(); |
344 |
346 |
444 ui.board.turn = 0; |
446 ui.board.turn = 0; |
445 ui.board.position = board.create(); |
447 ui.board.position = board.create(); |
446 board.putRandom(ui.board.position); |
448 board.putRandom(ui.board.position); |
447 ui.board.update(ui.board.position); |
449 ui.board.update(ui.board.position); |
448 } |
450 } |
449 document.getElementById("start").addEventListener("click", ui.action.start); |
451 document.getElementById("start").addEventListener("click", ui.action.start, false); |
450 |
452 |
451 for (var i = 0; i < ui.game.dirs.length; i++) { |
453 for (var i = 0; i < ui.game.dirs.length; i++) { |
452 var dir = ui.game.dirs[i]; |
454 var dir = ui.game.dirs[i]; |
453 ui.action[dir] = function(dir) { |
455 ui.action[dir] = function(dir) { |
454 return function() { |
456 return function(event) { |
|
457 event.preventDefault(); event.stopPropagation(); |
455 if (ui.game.checkGameOver()) |
458 if (ui.game.checkGameOver()) |
456 return; |
459 return; |
457 ui.suggestion.clear(); |
460 ui.suggestion.clear(); |
458 var updated = board.move[dir](ui.board.position); |
461 var updated = board.move[dir](ui.board.position); |
459 if (updated) { |
462 if (updated) { |
460 ui.game.finishStep(); |
463 ui.game.finishStep(); |
461 ui.ai.current && ui.ai.current.cleanup(); |
464 ui.ai.current && ui.ai.current.cleanup(); |
462 } |
465 } |
463 } |
466 } |
464 } (dir); |
467 } (dir); |
465 document.getElementById(dir).addEventListener("click", ui.action[dir]); |
468 document.getElementById(dir).addEventListener("click", ui.action[dir], false); |
466 } |
469 } |
467 |
470 |
468 document.body.addEventListener("keydown", function(event) { |
471 document.body.addEventListener("keydown", function(event) { |
469 if (document.activeElement.value) |
472 if (document.activeElement.value) |
470 return; |
473 return; |
471 if (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey) |
474 if (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey) |
472 return; |
475 return; |
473 var key = event.keyCode || event.which; |
476 var key = event.keyCode || event.which; |
474 var keyMoves = {38: "up", 40: "down", 37: "left", 39: "right"}; |
477 var keyMoves = {38: "up", 40: "down", 37: "left", 39: "right"}; |
475 if (key in keyMoves) { |
478 if (key in keyMoves) { |
476 ui.action[keyMoves[key]](); |
479 ui.action[keyMoves[key]](event); |
477 event.preventDefault(); |
480 } |
478 } |
481 }, false); |
479 }); |
|
480 |
482 |
481 document.getElementById("test").addEventListener("click", function() { |
483 document.getElementById("test").addEventListener("click", function() { |
|
484 event.preventDefault(); event.stopPropagation(); |
482 board.move.replaceByBoardJS(ui.brdEngine); |
485 board.move.replaceByBoardJS(ui.brdEngine); |
483 }); |
486 }, false); |
484 |
487 |
485 ui.action.suggest = function() { |
488 ui.action.suggest = function(event) { |
|
489 event.preventDefault(); event.stopPropagation(); |
486 if ( ! ui.ai.current) { |
490 if ( ! ui.ai.current) { |
487 ui.game.setMessage('Select AI!'); |
491 ui.game.setMessage('Select AI!'); |
488 return; |
492 return; |
489 } |
493 } |
490 if ( ! ui.game.beginStep()) |
494 if ( ! ui.game.beginStep()) |
495 ui.ai.current.cleanup(); |
499 ui.ai.current.cleanup(); |
496 if ( ! ui.game.checkMoveValid(move)) |
500 if ( ! ui.game.checkMoveValid(move)) |
497 return; |
501 return; |
498 ui.suggestion.set(move); |
502 ui.suggestion.set(move); |
499 } |
503 } |
500 document.getElementById("suggest").addEventListener("click", ui.action.suggest); |
504 document.getElementById("suggest").addEventListener("click", ui.action.suggest, false); |
501 ui.action.step = function() { |
505 ui.action.step = function() { |
502 if ( ! ui.ai.current) { |
506 if ( ! ui.ai.current) { |
503 ui.game.setMessage('Select AI!'); |
507 ui.game.setMessage('Select AI!'); |
504 return; |
508 return; |
505 } |
509 } |
512 if ( ! ui.game.checkMoveValid(move)) |
516 if ( ! ui.game.checkMoveValid(move)) |
513 return; |
517 return; |
514 board.move[move].call(null, ui.board.position); |
518 board.move[move].call(null, ui.board.position); |
515 ui.game.finishStep(); |
519 ui.game.finishStep(); |
516 } |
520 } |
517 document.getElementById("step").addEventListener("click", ui.action.step); |
521 document.getElementById("step").addEventListener("click", ui.action.step, false); |
518 |
522 |
519 ui.action.finish = function() { |
523 ui.action.finish = function() { |
520 if ( ! ui.ai.current) { |
524 if ( ! ui.ai.current) { |
521 ui.game.setMessage('Select AI!'); |
525 ui.game.setMessage('Select AI!'); |
522 return; |
526 return; |
546 ui.game.finishStep(); |
550 ui.game.finishStep(); |
547 ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
551 ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
548 ui.game.setMessage("Game over!"); |
552 ui.game.setMessage("Game over!"); |
549 ui.ai.current && ui.ai.current.cleanup(); |
553 ui.ai.current && ui.ai.current.cleanup(); |
550 } |
554 } |
551 document.getElementById("finish").addEventListener("click", ui.action.finish); |
555 document.getElementById("finish").addEventListener("click", ui.action.finish, false); |
552 |
556 |
553 //////////////////////////////////////////////////////////////// |
557 //////////////////////////////////////////////////////////////// |
554 // Register AIs. |
558 // Register AIs. |
555 |
559 |
556 ui.ai = {}; |
560 ui.ai = {}; |
603 }; |
607 }; |
604 ui.ai.domList = document.querySelectorAll('div.ai'); |
608 ui.ai.domList = document.querySelectorAll('div.ai'); |
605 for (var i = 0; i < ui.ai.domList.length; i++) { |
609 for (var i = 0; i < ui.ai.domList.length; i++) { |
606 ui.ai.domList[i].querySelectorAll('button.ai')[0].addEventListener("click", function (event) { |
610 ui.ai.domList[i].querySelectorAll('button.ai')[0].addEventListener("click", function (event) { |
607 ui.ai.enable(event.target.parentNode); |
611 ui.ai.enable(event.target.parentNode); |
608 }); |
612 }, false); |
609 } |
613 } |
610 |
614 |
611 ui.ai.moveToTop = function(aiDom) { |
615 ui.ai.moveToTop = function(aiDom) { |
612 for (var i = 0; i < ui.ai.domList.length; i++) { |
616 for (var i = 0; i < ui.ai.domList.length; i++) { |
613 ui.ai.domList[i].classList.remove('enabled'); |
617 ui.ai.domList[i].classList.remove('enabled'); |