http://www.ironsudoku.comjavascript: for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { ClearTinySquares(i,j); } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value != "") { var tmp_value = grid[i][j].value; for (k=1; k<=9; k++) { for (l=1; l<=9; l++) { if (grid[k][l].value == "") { if ((Math.ceil(i/3) == Math.ceil(k/3) && Math.ceil(j/3) == Math.ceil(l/3)) || i == k || j == l) { grid[k][l]["mini"][tmp_value] = -1; } } } } } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { for (k=1; k<=9; k++) { if (grid[i][j]["mini"][k] != -1) grid[i][j]["mini"][k] = 1; else grid[i][j]["mini"][k] = 0; } } } } PopulatePuzzle(grid); void(0);javascript: for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { ClearTinySquares(i,j); } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value != "") { var tmp_value = grid[i][j].value; for (k=1; k<=9; k++) { for (l=1; l<=9; l++) { if (grid[k][l].value == "") { if (( Math.ceil(i/3) == Math.ceil(k/3) && Math.ceil(j/3) == Math.ceil(l/3) ) || i == k || j == l ) { grid[k][l]["mini"][tmp_value] = -1; } } } } } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { for (k=1; k<=9; k++) { if (grid[i][j]["mini"][k] != -1) grid[i][j]["mini"][k] = 1; } } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { for (k=1; k<=9; k++) { if (grid[i][j]["mini"][k] != 1) grid[i][j]["mini"][k] = -1; } } } } PopulatePuzzle(grid); void(0); Update: (3/14/2007) IE6 problems solved.
Update: (2/15/2007) Added a note about cheating to the end of the article.
NOTE: The IronSudoku Javascript works in the Firefox web browser and IE7.
IronSudoku is a great sudoku site based on Javascript and DHTML. It’s a lot of fun and allows you to “pencil in” little numbers that are possible for a box, usually green for possibles and red for impossibles. Left-click a box and a little grid pops up with “1″ through “9″ for you to choose your answer for that box or hold down shift and click the number to get a green penciled-in number, or alt click to get the red variant.
Enter User Javascript
Javascript is a scripting language that has been a part of web browsers for years and is only getting more popular. Most modern websites make use of at least some Javascript. An interesting side effect of deep browser support is that they will run a Javascript script from the address bar, given that it is preceded with the keyword “javascript:”. It will execute the script commands immediately. Given a script, you can simply copy it to the clipboard and then paste it into your browser’s address field.
It so happens that a Javascript has been floating around to auto-fill the greens in IronSudoku. A tuned version is below. You can simply copy the following script to the clipboard by selecting it with your mouse, pressing Control-C on Windows systems or Command-C on Macs. Then with IronSudoku in another window or tab of your browser, clear the address bar (where it says javascript: var y1=1,y2=9,x1=1,x2=9; for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value=="") { ClearTinySquares(i,j); } } } for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value!="") { var tmp_value=grid[i][j].value; for(k=y1;k<=y2;k++) { for(l=x1;l<=x2;l++) { if(grid[k][l].value == "") { if((Math.ceil(i/3) == Math.ceil(k/3) && Math.ceil(j/3) == Math.ceil(l/3)) || i==k || j==l ) { grid[k][l]["mini"][tmp_value]=-1; } } } } } } } for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value=="") { for(k=1;k<=9;k++) { if(grid[i][j]["mini"][k]!=-1) grid[i][j]["mini"][k]=1; else grid[i][j]["mini"][k]=0; } } } } for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value=="") { var tmp=new Array(); tmp=Array(10); tmp[1]=0; tmp[2]=0; tmp[3]=0; tmp[4]=0; tmp[5]=0; tmp[6]=0; tmp[7]=0; tmp[8]=0; tmp[9]=0; for( k=1;k<=9;k++) { if(grid[i][j]["mini"][k]==1) { for( r=(Math.floor((i-1)/3)*3)+1; r<=Math.ceil(i/3)*3;r++) { for(s=(Math.floor((j-1)/3)*3)+1; s<=Math.ceil(j/3)*3;s++) { if( Math.abs( grid[r][s]["mini"][k] ) == 1 ) { tmp[k]++; } } } } if( tmp[k] == 2 || tmp[k] == 1) { grid[i][j]["mini"][k]=-1; } } delete tmp; } } } PopulatePuzzle(grid); void(0); ) and press Control-V (or Command-V on Macs) to paste the script. Then hit Enter/Return.
javascript: for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { ClearTinySquares(i,j); } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value != "") { var tmp_value = grid[i][j].value; for (k=1; k<=9; k++) { for (l=1; l<=9; l++) { if (grid[k][l].value == "") { if ((Math.ceil(i/3) == Math.ceil(k/3) && Math.ceil(j/3) == Math.ceil(l/3)) || i == k || j == l) { grid[k][l]["mini"][tmp_value] = -1; } } } } } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { for (k=1; k<=9; k++) { if (grid[i][j]["mini"][k] != -1) grid[i][j]["mini"][k] = 1; else grid[i][j]["mini"][k] = 0; } } } } PopulatePuzzle(grid); void(0);
As you can see in the image to the right, it fills in the greens. However, keep in mind that it is fairly dumb. It’s simply pencil-marking those open boxes with possibles based only on actual filled-in numbers in the current 3×3 or in the same row or column. It will ignore possibles in other 3×3s, and end up marking greens where you probably would not. Also, it will mark as green a number that is not only possible but the obvious answer for that box. For example, see the green 4 in the center of the lower-left 3×3. (More on this in a bit.)
It works great and usually saves a ton of time shift-clicking to fill in the obvious greens. “Usually” because sometimes the noise of all the greens gets in the way of actually seeing the numbers. Too much information? Now you have to spend time un-marking the greens that just can’t be.
Christmas Javascript
What does it look like to mix greens for possibles and reds for impossibles? Well, if you squint just right, it looks a little bit like Christmas.
Basically, a modification to the script above to set those not green to red instead of empty. It is not real useful on a clean board, as you can see.
javascript: for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { ClearTinySquares(i,j); } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value != "") { var tmp_value = grid[i][j].value; for (k=1; k<=9; k++) { for (l=1; l<=9; l++) { if (grid[k][l].value == "") { if (( Math.ceil(i/3) == Math.ceil(k/3) && Math.ceil(j/3) == Math.ceil(l/3) ) || i == k || j == l ) { grid[k][l]["mini"][tmp_value] = -1; } } } } } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { for (k=1; k<=9; k++) { if (grid[i][j]["mini"][k] != -1) grid[i][j]["mini"][k] = 1; } } } } for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { for (k=1; k<=9; k++) { if (grid[i][j]["mini"][k] != 1) grid[i][j]["mini"][k] = -1; } } } } PopulatePuzzle(grid); void(0);
Retasking the Reds
Instead of using them to indicate impossibilities, another way to use the little red numbers is to draw your attention to something important. With some additional code we can tell the script to go back and count the possibles for a given number in a 3×3 sub-grid then mark in red those that are of two possible locations for a number. This is another use of reds.
Also, it doesn’t take a genius to see that the above scripts mark as green a possible where it is, in fact, the only possible location. It is then trivial to have our script say, “okay, now I know the solution to that box, let’s just fill it in automatically.” That smells a little like cheating, so let’s use the a red pencil mark for those too. In the accompanying image, the numbers that can occur once or twice in a given 3×3 are marked in red.
So let’s take a look at a script to do just this.
The G+r Auto-fill Script
javascript: var y1=1,y2=9,x1=1,x2=9; for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value=="") { ClearTinySquares(i,j); } } } for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value!="") { var tmp_value=grid[i][j].value; for(k=y1;k<=y2;k++) { for(l=x1;l<=x2;l++) { if(grid[k][l].value == "") { if((Math.ceil(i/3) == Math.ceil(k/3) && Math.ceil(j/3) == Math.ceil(l/3)) || i==k || j==l ) { grid[k][l]["mini"][tmp_value]=-1; } } } } } } } for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value=="") { for(k=1;k<=9;k++) { if(grid[i][j]["mini"][k]!=-1) grid[i][j]["mini"][k]=1; else grid[i][j]["mini"][k]=0; } } } } for(i=y1;i<=y2;i++) { for(j=x1;j<=x2;j++) { if(grid[i][j].value=="") { var tmp=new Array(); tmp=Array(10); tmp[1]=0; tmp[2]=0; tmp[3]=0; tmp[4]=0; tmp[5]=0; tmp[6]=0; tmp[7]=0; tmp[8]=0; tmp[9]=0; for( k=1;k<=9;k++) { if(grid[i][j]["mini"][k]==1) { for( r=(Math.floor((i-1)/3)*3)+1; r<=Math.ceil(i/3)*3;r++) { for(s=(Math.floor((j-1)/3)*3)+1; s<=Math.ceil(j/3)*3;s++) { if( Math.abs( grid[r][s]["mini"][k] ) == 1 ) { tmp[k]++; } } } } if( tmp[k] == 2 || tmp[k] == 1) { grid[i][j]["mini"][k]=-1; } } delete tmp; } } } PopulatePuzzle(grid); void(0);
That’s Why It’s Called Penciling-In
Sometimes you get to a point where you want to erase the greens (and reds) and start penciling-in again. Here’s some Javascript to erase the pencil marks.
javascript: for (i=1; i<=9; i++) { for (j=1; j<=9; j++) { if (grid[i][j].value == "") { ClearTinySquares(i,j); } } }
Another Tip
Remember that the results of the Retasking the Reds script, like the others, change depending on what’s on the board. So, once you run the Javascript, leave it in the Address bar and just hit the Go button to the right of the Address bar and run it again. It adjusts the penciled-in green and red numbers accordingly.
Conclusion
Like crossword solvers aspire to do the hardest puzzles in ink, most will want to try solving sudoku without marks. However, when that breaks down and you want to use the little green and red numbers at IronSudoku, Javascript can help eliminate the rote drudgery of filling them all in.
It can be argued that this wanders close to cheating, all it really does is save time and frustration. The real work is left to the solver.
Update: (2/15/2007) It seems some people insist that an autofill script is cheating. Is it?
On the one hand, these scripts merely speed the player through the tedium of note-taking — shift-click then a number for every possibility you mark as green, alt-click then a number for reds. Since no answers are filled in, auto-fill does not solve anything for you. Since IronSudoku does not keep track of your times, auto-fill is not moving you ahead of anyone. Therefore, auto-fill is not cheating.
On the other hand, anything automated is cheating. Your brain should be enough. You could make a mistake when placing greens but auto-fill will not.
When I first heard of auto-fill scripts, it sounded like cheating to me too. However, the former arguments made sense; it’s merely reducing the time it takes (which is not recorded) to finish the puzzle and does not actually solve anything besides that. In the example to the right, the red sevens in the lower left 3×3 insist that the green sevens to their right be unmarked. Once you run the script, some it takes some time to manually go back and un-mark the greens that are obvious impossibilities. The G+r variant below goes a little further than the green-only script by marking those sevens as red.
But it’s a definitely a fine line. The script stops at marking the lower-left five red when it could have gone ahead and set the box to five and been done with it.
What do you think?
Tags: Free · Online games · Puzzles · Tips

2 responses so far ↓
1 jan // Feb 8, 2007 at 11:58 pm
why don’t the script work in internet explorer 6?
2 IronSudoku IE6 Autofill Solved » Brittlefish // Mar 14, 2007 at 8:51 pm
wondering for some time why on earth the IronSudoku autofill scripts would not run under Internet Explorer 6, but were fine in Firefox and IE7, the cause and a solution
Leave a Comment