# small sudoku implementation help

• 04-14-2013, 05:43 PM
jackjohnsonbush
small sudoku implementation help
Hi,

I am new to javascript so please be gentle. I am implementing a small 4x4 sudoku (16 cells total). I figured out how to make the tables and how to put buttons inside the cells to allow the user to pick a number. I am stuck on one thing however. How would I clear all the buttons within a cell once a user picks one and just have that number appear in plain text? here's what it looks like so far,

http://i.imgur.com/CLuPSkJ.jpg

For example, after I click on button 1, I want to check if it's valid then clear all the buttons and put "1" into that cell in plain text. Thanks guys.
• 04-15-2013, 08:06 PM
JMRKER
Well, it is a pretty picture, but if you want some help you will need to supply the code you created it with.
• 04-15-2013, 09:46 PM
jackjohnsonbush
Actually after hours of researching and failures, I figured most of my problems out. I have one last thing that just has to do with the algorithm. I know how to check if there's the same number in the row or column but i can't seem to figure out how to check if a number is in the same quadrant.. The table basically is 4x4 and there are 4 quadrants, how would you go about checking if the same number appears in the quadrant already?
• 04-15-2013, 10:07 PM
JMRKER
You have questions, but without some code to look at anything I could say would be a pure SWAG.
• 04-16-2013, 11:37 AM
007Julien
Use the division of two intergers (by 4 and by 2) with quotient and remainder to get the row, column and quadrant of the cells (from 0 to 15) of your grid. Then the code is something like this
Code:

```  var nmb;// the number of a cell   var col = nmb%4; // its column (from 0 to 3)   var row = (nmb-col)/4 //its row (from 0 to 3)   var reg = (row%2)*2 + (col%2) //its quadrant (from 0 to 3)```
But binaries are very efficient for Sudokus. For example, with 4x4 grids :
• each leer cell can be represent by 11110 = (32-1) which means that each value 1, 2, 3, 4 is possible
• each given cell by 00011 (for 1), 00101 (for 2), 01001 (for 3) and 00011 (for 4).

Then a new object Board could be build and update like this (not tested, only a transcription of this script) :

Code:

``` // The board constructor function Board(){         this.cells=new Array();for (var i=0;i<16;i++) this.cells[i]=31; } // A method to place the value v in the cell c (used at first with the given number) Board.prototype.setV=function(c,v){         var k,v=1<<v,w=32-v,x=c%4,y=Math.floor(c/4),r=Math.floor(x/2)*2,s=Math.floor(y/2)*2;         for (k=0;k<4;k++){this.cells[x+k*4]&=w;this.cells[k+y*4]&=w;                 this.cells[r+(k%2)+4*(s+Math.floor(k/2))]&=w;}                 this.cells[c]=v+1;} }```
Then the Board gives immediately for each cell the number (if the bit of range 0 is 1) or possible numbers (the bit of range 0 is null).
• 04-17-2013, 05:24 AM
007Julien
Sorry I make a mistake a leer cell 11110 is 30 instead of 31 !
32-1 is Math.pow(2,5)-1 or (1<<5)-1 = 11111 and 30 is 11110.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.28548 seconds
• Memory Usage 2,364KB
• Queries Executed 11 (?)
Template Usage (20):
• (2)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates