previous

page 32

next
an undo facility

At any point in the game, its state is fully specified by the three sets of arrays: temp_col, main_cols and final_col. So all we need to do is to save these arrays after every move to give us a way of undoing.

So I plan to create an aray like this:

[temp_col, main_cols, final_col]

and push it onto another array, g_saves, after every move.

Then undoing will simply be a matter of working backwards through this array using pop().

But there's a catch. temp_col (for example) is not really the array itself but simply a pointer to it. To cope with this situation, I have written a Copy_array function (it's in the new library) to make a new array from a given array.

save_state() in functions.js
function save_state() {
var arr=[];
arr.push(Copy_array(g_temp_col));
arr.push(Copy_array(g_main_cols));
arr.push(Copy_array(g_final_col));
g_saves.push(Copy_array(arr));
}
index.html
<script> deal(); draw(); save_state(); </script>
undo() in functions.js
function undo() {
if (g_saves.length>1) {
highlight_off();
g_saves.pop(); // discard current state
var arr=g_saves.pop(); // get previous state
g_temp_col=Copy_array(arr[0]);
g_main_cols=Copy_array(arr[1]);
g_final_col=Copy_array(arr[2]);
draw();
save_state();
}
}

Now all you need is an undo button and to add to both move and redeal.

previous
next