# Thread: small sudoku implementation help

1. Registered User
Join Date
Apr 2013
Posts
2

## 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.

2. Well, it is a pretty picture, but if you want some help you will need to supply the code you created it with.

3. Registered User
Join Date
Apr 2013
Posts
2
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?

4. You have questions, but without some code to look at anything I could say would be a pure SWAG.

5. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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).
Last edited by 007Julien; 04-16-2013 at 11:53 AM.

6. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
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.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.13442 seconds
• Memory Usage 2,891KB
• Queries Executed 15 (?)
Template Usage (33):
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (6)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (6)postbit
• (6)postbit_onlinestatus
• (6)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• 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
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete