kaka.farm

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

commit 1c39b8c976789a3ed5488f5ce8c35fa4cdd85c53
parent 48f7fbbdee3c05b88a2daf34d78c661dcac97fee
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Sun, 12 Jul 2020 14:40:40 +0300

Add save game, load game, and new game functions.

Diffstat:
Mhtml/sokoban.html | 37+++++++++++++++++++++++++++++++++----
Mhtml/sokoban.js | 7+++----
2 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/html/sokoban.html b/html/sokoban.html @@ -17,8 +17,19 @@ window.kakalog_activated = true; (function() { + function load_game() { + return JSON.parse(window.localStorage.getItem('game')); + }; + + + function save_game(game) { + window.localStorage.setItem('game', JSON.stringify(game)); + }; + + function main() { - let game = sokoban.load_game(); + let game = load_game() || sokoban.new_game(); + let text_element = document.getElementById('text'); text_element.innerText = sokoban.game_to_text(game); @@ -30,6 +41,7 @@ window.kakalog_activated = true; ['next_level', sokoban.action_next_level], ['restart_level', sokoban.action_restart_level], ['undo', sokoban.action_undo], + ['save', save_game], ].forEach(([element_id, action_function])=>{ let button = document.getElementById(element_id); console.debug(button) @@ -40,6 +52,20 @@ window.kakalog_activated = true; }; button.addEventListener('click', click_handler); }); + + [ + ['new', sokoban.new_game], + ['load', load_game], + ].forEach(([element_id, action_function])=>{ + let button = document.getElementById(element_id); + console.debug(button) + function click_handler() { + console.debug('click: ' + element_id); + game = action_function(); + text_element.innerText = sokoban.game_to_text(game); + }; + button.addEventListener('click', click_handler); + }); }; window.addEventListener('load', main); @@ -52,9 +78,12 @@ window.kakalog_activated = true; <button id="down" type="button">↓</button> <button id="left" type="button">←</button> <button id="right" type="button">→</button> - <button id="next_level" type="button">c</button> - <button id="restart_level" type="button">r</button> - <button id="undo" type="button">u</button> + <button id="next_level" type="button">continue</button> + <button id="restart_level" type="button">restart</button> + <button id="undo" type="button">undo</button> + <button id="save" type="button">save game</button> + <button id="load" type="button">load game</button> + <button id="new" type="button">new game</button> <pre id="log"></pre> </body> </html> diff --git a/html/sokoban.js b/html/sokoban.js @@ -252,14 +252,13 @@ let sokoban = (function() { }; - function load_game() { - let game = { + function new_game() { + return { current_level_number: 0, level_state: read_level_text(levels[0]), undo_stack: [], number_of_steps: 0, }; - return game; }; @@ -272,7 +271,7 @@ let sokoban = (function() { action_step_up, action_undo, game_to_text, - load_game, + new_game, }; })();