Created
August 12, 2011 18:52
-
-
Save lholden/1142696 to your computer and use it in GitHub Desktop.
Reconfigure Jame's Knight Game to have the ability to manage state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var save; | |
var freshBoard; | |
var oldSetupGame = this.setupGame; | |
this.setupGame = function() { | |
oldSetupGame(); | |
save = freshBoard = saveState(); | |
} | |
var saveState = function() { | |
return { | |
visited: $$('.visited'), | |
current: $('knight').parentNode | |
} | |
} | |
var restoreState = function(state) { | |
if (!state) return; | |
var knight = $('knight'); | |
$$('.visited').each(function(cell){ cell.removeClassName('visited'); cell.addClassName('unvisited')}) | |
state['visited'].each(function(cell) { cell.addClassName('visited') }); | |
state['current'].appendChild(knight); | |
LEGAL_MOVES.each(function(item) { $(item).stopObserving('click'); }); | |
setHandlerForLegalMoves(knight.parentNode) | |
} | |
$('board').insert('<input type="button" value="Re-Generate" id="regenerate" />'); | |
$('board').insert('<input type="button" value="Restart" id="restart" />'); | |
$('board').insert('<input type="button" value="Load" id="load" />'); | |
$('board').insert('<input type="button" value="Save" id="save" />'); | |
$('regenerate').observe('click', setupGame); | |
$('restart').observe('click', function() {restoreState(freshBoard)}); | |
$('save').observe('click', function() {save = saveState()}); | |
$('load').observe('click', function() {restoreState(save)}); | |
setupGame(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment