357 } |
358 } |
358 } |
359 } |
359 ui.game.checkMoveValid = function(move) { |
360 ui.game.checkMoveValid = function(move) { |
360 var tmpBrd = board.create(); |
361 var tmpBrd = board.create(); |
361 board.copy(board.current, tmpBrd); |
362 board.copy(board.current, tmpBrd); |
362 if (!(move in {"left":0,"right":0,"up":0,"down":0})) { |
363 if (ui.game.dirs.indexOf(move) === -1) { |
363 ui.game.setMessage("AI can't find move!"); |
364 ui.game.setMessage("AI can't find move!"); |
364 return false; |
365 return false; |
365 } |
366 } |
366 if ( ! board.move[move].call(null, tmpBrd)) { |
367 if ( ! board.move[move].call(null, tmpBrd)) { |
367 ui.game.setMessage("AI move "+move+" is ivalid!"); |
368 ui.game.setMessage("AI move "+move+" is ivalid!"); |
383 board.putRandom(board.current); |
384 board.putRandom(board.current); |
384 ui.board.update(board.current); |
385 ui.board.update(board.current); |
385 } |
386 } |
386 document.getElementById("start").addEventListener("click", ui.action.start); |
387 document.getElementById("start").addEventListener("click", ui.action.start); |
387 |
388 |
388 ui.action.up = function() { |
389 for (var i = 0; i < ui.game.dirs.length; i++) { |
389 ui.action.clearSuggestion(); |
390 var dir = ui.game.dirs[i]; |
390 var updated = board.move.up(board.current); |
391 ui.action[dir] = function(dir) { |
391 if (updated) { |
392 return function() { |
392 board.putRandom(board.current); |
393 if (ui.game.checkGameOver()) |
393 ui.board.update(board.current); |
394 return; |
394 ui.score.update(board.current); |
395 ui.action.clearSuggestion(); |
395 ui.ai.current && ui.ai.current.cleanup(); |
396 var updated = board.move[dir](board.current); |
396 } |
397 if (updated) { |
397 } |
398 board.putRandom(board.current); |
398 document.getElementById("up").addEventListener("click", ui.action.up); |
399 ui.board.update(board.current); |
399 ui.action.down = function() { |
400 ui.score.update(board.current); |
400 ui.action.clearSuggestion(); |
401 ui.ai.current && ui.ai.current.cleanup(); |
401 var updated = board.move.down(board.current); |
402 } |
402 if (updated) { |
403 } |
403 board.putRandom(board.current); |
404 } (dir); |
404 ui.board.update(board.current); |
405 document.getElementById(dir).addEventListener("click", ui.action[dir]); |
405 ui.score.update(board.current); |
406 } |
406 ui.ai.current && ui.ai.current.cleanup(); |
|
407 } |
|
408 } |
|
409 document.getElementById("down").addEventListener("click", ui.action.down); |
|
410 ui.action.left = function() { |
|
411 ui.action.clearSuggestion(); |
|
412 var updated = board.move.left(board.current); |
|
413 if (updated) { |
|
414 board.putRandom(board.current); |
|
415 ui.board.update(board.current); |
|
416 ui.score.update(board.current); |
|
417 ui.ai.current && ui.ai.current.cleanup(); |
|
418 } |
|
419 } |
|
420 document.getElementById("left").addEventListener("click", ui.action.left); |
|
421 ui.action.right = function() { |
|
422 ui.action.clearSuggestion(); |
|
423 var updated = board.move.right(board.current); |
|
424 if (updated) { |
|
425 board.putRandom(board.current); |
|
426 ui.board.update(board.current); |
|
427 ui.score.update(board.current); |
|
428 ui.ai.current && ui.ai.current.cleanup(); |
|
429 } |
|
430 } |
|
431 document.getElementById("right").addEventListener("click", ui.action.right); |
|
432 |
407 |
433 document.body.addEventListener("keydown", function(event) { |
408 document.body.addEventListener("keydown", function(event) { |
434 if (document.activeElement.value) |
409 if (document.activeElement.value) |
435 return false; |
410 return false; |
436 var key = event.keyCode || event.which; |
411 var key = event.keyCode || event.which; |