JSFiddle - React, Tailwind, and code Playground
by slawe
HTML
<h1>Sudoku</h1>
<div id="container"></div>
<div id="menu" class="sudoku-menu">
<button id="solve">Solve</button>
<button id="validate">Validate Board</button>
<button id="reset">Reset</button>
</div>
CSS
body {
margin: 0;
padding: 0;
}
button, h1 {
margin-left: auto;
margin-right: auto;
text-align: center;
}
.sudoku-menu {
width: 50%;
text-align: center;
margin-left: auto;
margin-right: auto;
}
.sudoku-menu button {
font-size: 24px;
margin: 10px;
padding: 5px;
}
.sudoku-container {
border: 5px solid #666;
margin-left: auto;
margin-right: auto;
}
.sudoku-container td {
margin: 0;
}
.sudoku-container input {
width: 40px;
height: 40px;
text-align: center;
font-size: 20px;
padding: 0;
border: 1px #ccc solid;
background: transparent;
}
.sudoku-container .sudoku-input-error {
background: #ffc9d7;
}
.sudoku-container.valid-matrix {
border: 5px solid #7edb37;
}
.sudoku-section-one {
background: #ccc;
}
JavaScript
/**
* A Javascript implementation of a Sudoku game, including a
* backtracking algorithm solver. For example usage see the
* attached index.html demo.
*
* @author Moriel Schottlender
*/
var Sudoku = ( function ( $ ){
var _instance, _game,
/**
* Default configuration options. These can be overriden
* when loading a game instance.
* @property {Object}
*/
defaultConfig = {
// If set to true, the game will validate the numbers
// as the player inserts them. If it is set to false,
// validation will only happen at the end.
'validate_on_insert': true,
// If set to true, the system will display the elapsed
// time it took for the solver to finish its operation.
'show_solver_timer': true,
// If set to true, the recursive solver will count the
// number of recursions and backtracks it performed and
// display them in the console.
'show_recursion_counter': true,
// If set to true, the solver will test a shuffled array
// of possible numbers in each empty input box.
// Otherwise, the possible numbers are ordered, which
// means the solver will likely give the same result
// when operating in the same game conditions.
'solver_shuffle_numbers': true
},
paused = false,
counter = 0;
/**
* Initialize the singleton
* @param {Object} config Configuration options
* @returns {Object} Singleton methods
*/
function init( config ) {
conf = $.extend( {}, defaultConfig, config );
_game = new Game( conf );
/** Public methods **/
return {
/**
* Return a visual representation of the board
* @returns {jQuery} Game table
*/
getGameBoard: function() {
return _game.buildGUI();
},
/**
* Reset the game board.
*/
reset: function() {
_game.resetGame();
},
/**
* Call for a validation of the game board.
* @returns {Boolean} Whether the board is valid
*/
validate: function() {
var isValid;
isValid = _game.validateMatrix();
$(...