I think we should do the tech talk about the back step function here.
Sorry, I don't get it where the 'much work' of a back step function should be. As far as I can see, it takes 5 steps to introduce this functionality:Jeff Widderich wrote:Insider information
Ein rueckwaerts Funktion ist sehr viel arbeit .
- The button itself calling the function back_step(). No big deal, only HTML.
- The function back_step() This is quite easy: get the necessary information out of *somewhere*, change the array at one cell and repaint this cell if needed. I assume, the repainting could do the function set_square() which is allready in place.
- Some hook at the onchange event of every Cell. It's allready there: function Sudoku1(). So all you need to do is to store the current value of the changed cell *somewhere*. This should be one or two additional lines of code at function Sudoku1().
- We need some sort of init step for *somewhere*, as the hook stores the current value of each cell and not the old value. Unfortunately the task of 'loading a board' was mixed up with the task of 'deciding which board to load'. Threrefore you have two setup functions load_board() and load_easy() where the init step must be implemented. This might be the right time to do a little cleanup:
- rewrite load_board(initString) to load only the board itself
- create a load_current(print_version) to get the *.asp file and call the new function load_board(mySudoku)
- and rewrite the load_easy(print_version) also to get only the *.asp file and call the new function load_board(mySudoku)
- Last but not least, the famous *somewhere* must be defined. As allways, the more time you spend in design of your data, the easier it will be to handle it. After some minutes to think about the problem, I would sugest the following solution:
- One list (which is a stack, lifo-principle) storing all the cell names which are changed. Lets call it cellStack.
- Second, a hash (associative list) with keys representing the cell name (e.g. 28) and the value being a list (again implemented as a stack) of values. Lets call it cell_valueStack to remember that we refer to it via the cell names and we will get a value which is itself a list of values. So you end up with 81 lists (one for each cell) of old values bundled together to one hash.
Init step inside load_board(), load_easy()
Code: Select all
var cellStack=new Array();
var cell_valueStack=new Object();
// inside the double loop
var cellName=''+j+i;
var cell_valueStack[cellName]=new Array();
cell_valueStack[cellName].push(g[j][i]);
Code: Select all
// assuming that you have the variables x and y and afield.value
var cellName=''+y+x;
cellStack.push(cellName);
// note that we store the current value (after user has changed it), not the old one!
cell_valueStack[cellName].push(afield.value); // or use mask[y][x] if you prefer
Code: Select all
// check if there are any steps to go back
if(cellStack.length==0){
alert('Es gibt keine Änderungen zum zurücknehmen.');
return;
}
// get the last cell which was changed and remove it from stack
var cellToChange=cellStack.pop();
//As we have stored allwasy the current value,
//we now remove the last value of the stack
cell_valueStack[cellToChange].pop();
//now get the value we want to restore
var last=cell_valueStack[cellToChange].length-1;
var valueToRestore=cell_valueStack[cellToChange][last];
// now you have all you need, and you can do the fancy convert_* things to set
// mask[y][x] and g[y][x] and finally call set_square()
Cheers,
Str8tFan