www.webdeveloper.com
Results 1 to 6 of 6

Thread: small sudoku implementation help

  1. #1
    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. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,946
    Well, it is a pretty picture, but if you want some help you will need to supply the code you created it with.

  3. #3
    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. #4
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,946
    You have questions, but without some code to look at anything I could say would be a pure SWAG.

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

Thread Information

Users Browsing this Thread

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
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14646 seconds
  • Memory Usage 2,890KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (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
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.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 (72):
  • 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_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • 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
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_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
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates