kaka.farm

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/kaka.farm
Log | Files | Refs | README

sokoban-web.js (1510B)


      1 'use strict';
      2 
      3 
      4 (function() {
      5 	function load_game() {
      6 		return JSON.parse(window.localStorage.getItem('game'));
      7 	};
      8 
      9 
     10 	function save_game(game) {
     11 		window.localStorage.setItem('game', JSON.stringify(game));
     12 	};
     13 
     14 
     15 	function main() {
     16 		let game = load_game() || sokoban.new_game();
     17 
     18 		let text_element = document.getElementById('text');
     19 		text_element.innerText = sokoban.game_to_text(game);
     20 
     21 		[
     22 			['up', sokoban.action_step_up],
     23 			['down', sokoban.action_step_down],
     24 			['left', sokoban.action_step_left],
     25 			['right', sokoban.action_step_right],
     26 			['next_level', sokoban.action_next_level],
     27 			['restart_level', sokoban.action_restart_level],
     28 			['undo', sokoban.action_undo],
     29 			['save', save_game],
     30 		].forEach(([element_id, action_function])=>{
     31 			let button = document.getElementById(element_id);
     32 			console.debug(button)
     33 			function click_handler() {
     34 				console.debug('click: ' + element_id);
     35 				action_function(game);
     36 				text_element.innerText = sokoban.game_to_text(game);
     37 			};
     38 			button.addEventListener('click', click_handler);
     39 		});
     40 
     41 		[
     42 			['new', sokoban.new_game],
     43 			['load', load_game],
     44 		].forEach(([element_id, action_function])=>{
     45 			let button = document.getElementById(element_id);
     46 			console.debug(button)
     47 			function click_handler() {
     48 				console.debug('click: ' + element_id);
     49 				game = action_function();
     50 				text_element.innerText = sokoban.game_to_text(game);
     51 			};
     52 			button.addEventListener('click', click_handler);
     53 		});
     54 	};
     55 
     56 	window.addEventListener('load', main);
     57 })();