334 speedDom.innerHTML = '' + speed; |
337 speedDom.innerHTML = '' + speed; |
335 turnDom.innerHTML = '' + turn; |
338 turnDom.innerHTML = '' + turn; |
336 } |
339 } |
337 |
340 |
338 //////////////////////////////////////////////////////////////// |
341 //////////////////////////////////////////////////////////////// |
|
342 // Suggestion UI. |
|
343 |
|
344 ui.suggestion = {}; |
|
345 ui.suggestion.btns = document.querySelectorAll('div.move-control button'); |
|
346 ui.suggestion.clear = function() { |
|
347 for (var i = 0; i < ui.suggestion.btns.length; i++) |
|
348 ui.suggestion.btns[i].classList.remove('suggestion'); |
|
349 } |
|
350 ui.suggestion.set = function(dir) { |
|
351 document.getElementById(dir).classList.add('suggestion'); |
|
352 } |
|
353 |
|
354 //////////////////////////////////////////////////////////////// |
339 // Game flow. |
355 // Game flow. |
340 |
356 |
341 ui.game = {}; |
357 ui.game = {}; |
342 ui.game.dirs = ["up", "right", "down", "left"]; |
358 ui.game.dirs = ["up", "right", "down", "left"]; |
343 |
359 |
369 return false; |
385 return false; |
370 } |
386 } |
371 return true; |
387 return true; |
372 } |
388 } |
373 |
389 |
|
390 ui.game.beginStep = function() { |
|
391 ui.suggestion.clear(); |
|
392 ui.game.clearMessage(); |
|
393 if (ui.game.checkGameOver()) |
|
394 return false; |
|
395 return true; |
|
396 } |
|
397 ui.game.finishStep = function() { |
|
398 board.putRandom(board.current); |
|
399 ui.board.update(board.current); |
|
400 ui.score.update(board.current); |
|
401 localStorage.savedBoard = board.current; |
|
402 } |
|
403 |
374 //////////////////////////////////////////////////////////////// |
404 //////////////////////////////////////////////////////////////// |
375 // Actions. |
405 // Actions. |
376 |
406 |
377 ui.action = {}; |
407 ui.action = {}; |
378 |
408 |
379 ui.action.start = function() { |
409 ui.action.start = function() { |
380 ui.score.clear(); |
410 ui.score.clear(); |
|
411 ui.suggestion.clear(); |
381 ui.game.clearMessage(); |
412 ui.game.clearMessage(); |
382 ui.action.clearSuggestion(); |
|
383 board.current = board.create(); |
413 board.current = board.create(); |
384 board.putRandom(board.current); |
414 board.putRandom(board.current); |
385 ui.board.update(board.current); |
415 ui.board.update(board.current); |
386 } |
416 } |
387 document.getElementById("start").addEventListener("click", ui.action.start); |
417 document.getElementById("start").addEventListener("click", ui.action.start); |
390 var dir = ui.game.dirs[i]; |
420 var dir = ui.game.dirs[i]; |
391 ui.action[dir] = function(dir) { |
421 ui.action[dir] = function(dir) { |
392 return function() { |
422 return function() { |
393 if (ui.game.checkGameOver()) |
423 if (ui.game.checkGameOver()) |
394 return; |
424 return; |
395 ui.action.clearSuggestion(); |
425 ui.suggestion.clear(); |
396 var updated = board.move[dir](board.current); |
426 var updated = board.move[dir](board.current); |
397 if (updated) { |
427 if (updated) { |
398 board.putRandom(board.current); |
428 ui.game.finishStep(); |
399 ui.board.update(board.current); |
|
400 ui.score.update(board.current); |
|
401 ui.ai.current && ui.ai.current.cleanup(); |
429 ui.ai.current && ui.ai.current.cleanup(); |
402 } |
430 } |
403 } |
431 } |
404 } (dir); |
432 } (dir); |
405 document.getElementById(dir).addEventListener("click", ui.action[dir]); |
433 document.getElementById(dir).addEventListener("click", ui.action[dir]); |
421 |
449 |
422 document.getElementById("test").addEventListener("click", function() { |
450 document.getElementById("test").addEventListener("click", function() { |
423 board.move.replaceByBoardJS(ui.brdEngine); |
451 board.move.replaceByBoardJS(ui.brdEngine); |
424 }); |
452 }); |
425 |
453 |
426 ui.action.clearSuggestion = function() { |
|
427 var btns = document.querySelectorAll('div.move-control button'); |
|
428 for (var i = 0; i < btns.length; i++) |
|
429 btns[i].classList.remove('suggestion'); |
|
430 } |
|
431 ui.action.suggest = function() { |
454 ui.action.suggest = function() { |
432 ui.action.clearSuggestion(); |
455 if ( ! ui.ai.current) { |
433 ui.game.clearMessage(); |
456 ui.game.setMessage('Select AI!'); |
434 if (ui.game.checkGameOver()) |
457 return; |
|
458 } |
|
459 if ( ! ui.game.beginStep()) |
435 return; |
460 return; |
436 var tmpBrd = board.create(); |
461 var tmpBrd = board.create(); |
437 board.copy(board.current, tmpBrd); |
462 board.copy(board.current, tmpBrd); |
438 var move = ui.ai.current.analyse(tmpBrd); |
463 var move = ui.ai.current.analyse(tmpBrd); |
439 ui.ai.current.cleanup(); |
464 ui.ai.current.cleanup(); |
440 if ( ! ui.game.checkMoveValid(move)) |
465 if ( ! ui.game.checkMoveValid(move)) |
441 return; |
466 return; |
442 document.getElementById(move).classList.add('suggestion'); |
467 ui.suggestion.set(move); |
443 } |
468 } |
444 document.getElementById("suggest").addEventListener("click", ui.action.suggest); |
469 document.getElementById("suggest").addEventListener("click", ui.action.suggest); |
445 ui.action.step = function() { |
470 ui.action.step = function() { |
446 ui.game.clearMessage(); |
471 if ( ! ui.ai.current) { |
447 ui.action.clearSuggestion(); |
472 ui.game.setMessage('Select AI!'); |
448 if (ui.game.checkGameOver()) |
473 return; |
|
474 } |
|
475 if ( ! ui.game.beginStep()) |
449 return; |
476 return; |
450 var tmpBrd = board.create(); |
477 var tmpBrd = board.create(); |
451 board.copy(board.current, tmpBrd); |
478 board.copy(board.current, tmpBrd); |
452 var move = ui.ai.current.analyse(tmpBrd); |
479 var move = ui.ai.current.analyse(tmpBrd); |
453 ui.ai.current.cleanup(); |
480 ui.ai.current.cleanup(); |
454 if ( ! ui.game.checkMoveValid(move)) |
481 if ( ! ui.game.checkMoveValid(move)) |
455 return; |
482 return; |
456 board.move[move].call(null, board.current); |
483 board.move[move].call(null, board.current); |
457 board.putRandom(board.current); |
484 ui.game.finishStep(); |
458 ui.board.update(board.current); |
|
459 ui.score.update(board.current); |
|
460 } |
485 } |
461 document.getElementById("step").addEventListener("click", ui.action.step); |
486 document.getElementById("step").addEventListener("click", ui.action.step); |
462 |
487 |
463 ui.action.finish = function() { |
488 ui.action.finish = function() { |
464 ui.game.clearMessage(); |
489 if ( ! ui.ai.current) { |
465 ui.action.clearSuggestion(); |
490 ui.game.setMessage('Select AI!'); |
|
491 return; |
|
492 } |
|
493 ui.game.beginStep(); |
466 var step = 0; |
494 var step = 0; |
467 var tsFrom = new Date().getTime(); |
495 var tsFrom = new Date().getTime(); |
468 while (!board.gameOver(board.current)) { |
496 while (!board.gameOver(board.current)) { |
469 var tmpBrd = board.create(); |
497 var tmpBrd = board.create(); |
470 board.copy(board.current, tmpBrd); |
498 board.copy(board.current, tmpBrd); |
475 } |
503 } |
476 var updated = board.move[move].call(null, board.current); |
504 var updated = board.move[move].call(null, board.current); |
477 if (updated) { |
505 if (updated) { |
478 board.putRandom(board.current); |
506 board.putRandom(board.current); |
479 } else { |
507 } else { |
480 ui.board.update(board.current); |
508 ui.game.finishStep(); |
481 ui.score.update(board.current); |
|
482 ui.game.setMessage("Wrong move!"); |
509 ui.game.setMessage("Wrong move!"); |
483 return; |
510 return; |
484 } |
511 } |
485 step++; |
512 step++; |
486 } |
513 } |
487 var tsTo = new Date().getTime(); |
514 var tsTo = new Date().getTime(); |
488 ui.board.update(board.current); |
515 ui.game.finishStep(); |
489 ui.score.update(board.current); |
|
490 ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
516 ui.score.speed(step*1000.0/(tsTo-tsFrom), step); |
491 ui.game.setMessage("Game over!"); |
517 ui.game.setMessage("Game over!"); |
492 ui.ai.current && ui.ai.current.cleanup(); |
518 ui.ai.current && ui.ai.current.cleanup(); |
493 } |
519 } |
494 document.getElementById("finish").addEventListener("click", ui.action.finish); |
520 document.getElementById("finish").addEventListener("click", ui.action.finish); |