From 9695aebf39ced9d74806599d4e04740d7d4c994c Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Thu, 11 Jul 2013 23:03:06 +0200 Subject: [PATCH] Initial commit --- cellMask.php | 38 +++++++++++ index.html | 63 +++++++++++++++++++ js/SudokuBoard.class.js | 86 +++++++++++++++++++++++++ js/SudokuCell.class.js | 59 +++++++++++++++++ js/SudokuChecks.class.js | 93 +++++++++++++++++++++++++++ js/SudokuSolver.class.js | 132 +++++++++++++++++++++++++++++++++++++++ js/firebugx.js | 8 +++ 7 files changed, 479 insertions(+) create mode 100644 cellMask.php create mode 100644 index.html create mode 100644 js/SudokuBoard.class.js create mode 100644 js/SudokuCell.class.js create mode 100644 js/SudokuChecks.class.js create mode 100644 js/SudokuSolver.class.js create mode 100644 js/firebugx.js diff --git a/cellMask.php b/cellMask.php new file mode 100644 index 0000000..fc28dae --- /dev/null +++ b/cellMask.php @@ -0,0 +1,38 @@ + 512 possible combinations (-9 for the single ones) + +$i = imagecreate( $image_size, $image_size ); +$transp = imagecolorallocatealpha( $i, 0xff, 0xff, 0xff, 0x7f ); + +imagefill( $i, 1, 1, $transp ); + +if ( !isset( $_GET['changed'] ) ) { + $boxcolor = imagecolorallocate( $i, 0xdd, 0xdd, 0xdd ); +} else { + $boxcolor = imagecolorallocate( $i, 0xff, 0x88, 0x88 ); +} +$boxsize = ($image_size+1) / $dim; + +for ( $y=0; $y<$dim; $y++ ) { + for ( $x=0; $x<$dim; $x++ ) { + $m = 1 << ( $y*$dim + $x ); + if ( ( $m & $mask ) == $m ) { + imagefilledrectangle( $i, $x*$boxsize, $y*$boxsize, ($x+1)*$boxsize-2, ($y+1)*$boxsize-2, $boxcolor ); + } + } +} + +header( 'Content-Type: image/png' ); +imagepng( $i ); +imagedestroy( $i ); + +?> \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..766479f --- /dev/null +++ b/index.html @@ -0,0 +1,63 @@ + + + + + + + + + + + + +
+ + diff --git a/js/SudokuBoard.class.js b/js/SudokuBoard.class.js new file mode 100644 index 0000000..de9f897 --- /dev/null +++ b/js/SudokuBoard.class.js @@ -0,0 +1,86 @@ +function SudokuBoard( dim ) { + this.dim = Number( dim ); + this.dim2 = this.dim * this.dim; + this.BASE_SET = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + this.set = this.BASE_SET.substr(0, this.dim2); + this.board = new Array( this.dim2 ); + this.checkDiags = false; + this.changed = false; + + for (var r=0; r 0 ) cssclass = ' class="' + cssclass + '"'; + background = ''; + value = this.cellAt( r, c ).getValue(); + if ( value == '.' ) { + background = ' background="cellMask.php?dim=' + this.dim + '&mask=' + this.cellAt(r,c).getMask(); + if ( this.cellAt( r, c ).hasChanged() ) background += '&changed=1'; + background += '"'; + value = ''; + } + html += '' + value + ''; + } + html += ''; + } + html += ''; + var body = document.getElementsByTagName( 'body' ); + body[0].innerHTML += html; + } +} \ No newline at end of file diff --git a/js/SudokuCell.class.js b/js/SudokuCell.class.js new file mode 100644 index 0000000..15377c0 --- /dev/null +++ b/js/SudokuCell.class.js @@ -0,0 +1,59 @@ +function SudokuCell( initVal, boardObj ) { + this.boardObj = boardObj; + this.changed = false; + this.value = 0; + this.set = ( 1 << this.boardObj.dim2 ) - 1; // all + + this.setMask = function( newSet ) { + if ( newSet != this.set ) { + this.set = newSet; + this.changed = true; + this.boardObj.changed = true; + } + } + + this.setValue = function( newValue ) { + if ( newValue == this.value ) return false; + setidx = this.boardObj.set.indexOf( newValue ); + if ( setidx != -1 ) { + this.value = newValue; + this.setMask( 1 << setidx ); + this.changed = true; + this.boardObj.changed = true; + return true; + } else if ( newValue == -1 ) { + this.value = 0; + this.setMask( ( 1 << this.boardObj.dim2 ) - 1 ); // all + this.changed = true; + this.boardObj.changed = true; + } + return false; + } + + this.setValue( initVal ); // init cell + + this.getValue = function() { + if ( this.value == 0 ) return '.'; + return this.value; + } + + this.getMask = function() { + return this.set; + } + + this.getUnknownsCount = function() { + var result = 0; + for (var n=0; n 0 ) console.info( 'Have %d matches.', n ); + if ( n == 2 ) { + for ( var k in cells ) { + if ( ( cells[k].getValue() == '.' ) && ( cells[k].getMask() == p ) ) { + cells[k].setMask( p ); + } else if ( cells[k].getValue() == '.' ) { + cells[k].setMask( cells[k].getMask() & ~p ); + } + } + } + } + } + console.groupEnd(); + } +}; \ No newline at end of file diff --git a/js/SudokuSolver.class.js b/js/SudokuSolver.class.js new file mode 100644 index 0000000..0614ea6 --- /dev/null +++ b/js/SudokuSolver.class.js @@ -0,0 +1,132 @@ +var SudokuSolver = { + + 'uniqueAll' : function( board ) { + for ( var r=0; r'; + board.print(); + } + i++; + console.timeEnd( 'Checking board.' ); + } while ( board.hasChanged() && i<100); + } + +}; \ No newline at end of file diff --git a/js/firebugx.js b/js/firebugx.js new file mode 100644 index 0000000..5216bbc --- /dev/null +++ b/js/firebugx.js @@ -0,0 +1,8 @@ +if ( ! console in window ) { + var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", + "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"]; + + window.console = {}; + for (var i = 0; i < names.length; ++i) + window.console[names[i]] = function() {} +}