www.webdeveloper.com
Results 1 to 7 of 7

Thread: How do i store data with a Two Dimensional Array? Please help!!

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    How do i store data with a Two Dimensional Array? Please help!!

    Basically im trying to create a sudoku page, and the user inputs the numbers they want to into empty table cells and i want them to be able to save what they have done by clicking on a button. Ive got a two dimensional array but im not sure if it works because i dont know how to link the array with the button :/ im only just starting javscript at uni so im just a beginner. please help!



    var current_cell = null; // the currently selected cell
    var saved = {}; // Object for saving the current game

    function initialize() {
    var col, row;
    // Work through all the cells in the table and set
    // onclick event handlers and classNames for the empty
    // ones.
    for (row = 0; row <=8; row++) {
    for (col=0; col <= 8; col++) {
    var cell = document.getElementById('cell_' + col + '_' + row);
    if (!parseInt(cell.innerHTML)) {
    // cell is empty
    cell.onclick = selectCell;
    cell.className = 'tofill';
    }
    }
    }
    document.onkeypress = keyPress;
    save();

    }

    var data = new array(9)
    for (i=0; i <9 ; i++)
    data[i] = new array(9)


    data[0][0]: cell_0_0
    data[1][0]: cell_1_0
    data[2][0]: cell_2_0
    data[3][0]: cell_3_0
    data[4][0]: cell_4_0
    data[5][0]: cell_5_0
    data[6][0]: cell_6_0
    data[7][0]: cell_7_0
    data[8][0]: cell_8_0

    data[0][1]: cell_0_1
    data[1][1]: cell_1_1
    data[2][1]: cell_2_1
    data[3][1]: cell_3_1
    data[4][1]: cell_4_1
    data[5][1]: cell_5_1
    data[6][1]: cell_6_1
    data[7][1]: cell_7_1
    data[8][1]: cell_8_1

    data[0][2]: cell_0_2
    data[1][2]: cell_1_2
    data[2][2]: cell_2_2
    data[3][2]: cell_3_2
    data[4][2]: cell_4_2
    data[5][2]: cell_5_2
    data[6][2]: cell_6_2
    data[7][2]: cell_7_2
    data[8][2]: cell_8_2

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,931

    Lightbulb

    I don't see why you would want to attack a two dimension array if you are a beginner.
    Here is a way that a single dimension array could be used (a lot easier to understand).

    The script does NOT play the game or even set-up a valid game.
    All it does is show how a single array can be displayed in two dimensions.

    BTW: You should enclose you scripts between [ code] and [ /code] tags (without the spaces)
    to make it easier to read, copy, test and debug your attempts.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    var board = [
      1,2,3, 1,2,3, 1,2,3,
      4,5,6, 4,5,6, 4,5,6,
      7,8,9, 7,8,9, 7,8,9,
    
      1,2,3, 1,2,3, 1,2,3,
      4,5,6, 4,5,6, 4,5,6,
      7,8,9, 7,8,9, 7,8,9,
    
      1,2,3, 1,2,3, 1,2,3,
      4,5,6, 4,5,6, 4,5,6,
      7,8,9, 7,8,9, 7,8,9
    ];
    function drawBoard() {
      var tmp;
      for (r=0; r<9; r++) {
        for (c=0; c<9; c++) {
          tmp = 'r'+r+'c'+c;  // alert(tmp);
          document.getElementById(tmp).innerHTML = board[(r*9)+c];
        }
      }
    }
    window.onload = function() { drawBoard(); }
    </script>
    
    <style type="text/css">
    #gameBoard div { width:2em; height:2em; float:left; text-align:center; font-size:1.5em; border:1px solid red; }
    </style>
    </head>
    <body>
    <h1>Sudoku</h1>
    
    <div id="gameBoard">
     <div id="r0c0"></div> <div id="r0c1"></div> <div id="r0c2"></div>
     <div id="r0c3"></div> <div id="r0c4"></div> <div id="r0c5"></div>
     <div id="r0c6"></div> <div id="r0c7"></div> <div id="r0c8"></div><br style="clear:both">
     <div id="r1c0"></div> <div id="r1c1"></div> <div id="r1c2"></div>
     <div id="r1c3"></div> <div id="r1c4"></div> <div id="r1c5"></div>
     <div id="r1c6"></div> <div id="r1c7"></div> <div id="r1c8"></div><br style="clear:both">
     <div id="r2c0"></div> <div id="r2c1"></div> <div id="r2c2"></div>
     <div id="r2c3"></div> <div id="r2c4"></div> <div id="r2c5"></div>
     <div id="r2c6"></div> <div id="r2c7"></div> <div id="r2c8"></div><br style="clear:both">
    
     <div id="r3c0"></div> <div id="r3c1"></div> <div id="r3c2"></div>
     <div id="r3c3"></div> <div id="r3c4"></div> <div id="r3c5"></div>
     <div id="r3c6"></div> <div id="r3c7"></div> <div id="r3c8"></div><br style="clear:both">
     <div id="r4c0"></div> <div id="r4c1"></div> <div id="r4c2"></div>
     <div id="r4c3"></div> <div id="r4c4"></div> <div id="r4c5"></div>
     <div id="r4c6"></div> <div id="r4c7"></div> <div id="r4c8"></div><br style="clear:both">
     <div id="r5c0"></div> <div id="r5c1"></div> <div id="r5c2"></div>
     <div id="r5c3"></div> <div id="r5c4"></div> <div id="r5c5"></div>
     <div id="r5c6"></div> <div id="r5c7"></div> <div id="r5c8"></div><br style="clear:both">
    
     <div id="r6c0"></div> <div id="r6c1"></div> <div id="r6c2"></div>
     <div id="r6c3"></div> <div id="r6c4"></div> <div id="r6c5"></div>
     <div id="r6c6"></div> <div id="r6c7"></div> <div id="r6c8"></div><br style="clear:both">
     <div id="r7c0"></div> <div id="r7c1"></div> <div id="r7c2"></div>
     <div id="r7c3"></div> <div id="r7c4"></div> <div id="r7c5"></div>
     <div id="r7c6"></div> <div id="r7c7"></div> <div id="r7c8"></div><br style="clear:both">
     <div id="r8c0"></div> <div id="r8c1"></div> <div id="r8c2"></div>
     <div id="r8c3"></div> <div id="r8c4"></div> <div id="r8c5"></div>
     <div id="r8c6"></div> <div id="r8c7"></div> <div id="r8c8"></div><br style="clear:both">
    </div>
    
    </body>
    </html>

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,290
    The simpler form to describe a Sudoku is probably a string like this :
    '000000012000035000000600070700000300000400800100000000000120000080000040050000600'
    Where 0 represent the lear cases...

    Paste this string in this Sudoku page to see an example of Sudoku game...

  4. #4
    Join Date
    Oct 2012
    Posts
    2
    Thanks guys, much appreciated unfortunately i was told i have to use two dimensional arrays by my lecturer and that is why i have been struggling because it is quite confusing. I didnt need to draw the table as it had already been drawn in html i only had to create a save button so what the user enters into the cells would be saved.. but i cant find an exact way to do that :/

  5. #5
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,931
    Quote Originally Posted by bash112 View Post
    Thanks guys, much appreciated unfortunately i was told i have to use two dimensional arrays by my lecturer and that is why i have been struggling because it is quite confusing. I didnt need to draw the table as it had already been drawn in html i only had to create a save button so what the user enters into the cells would be saved.. but i cant find an exact way to do that :/
    There is not a way to save the user results using JS only.
    The closest you can come is to use a cookie or if your browser supports it, local storage.

    Your original post was incomplete, so I used the two dimensions (r and c) to write to it on the screen.
    You can use a similar logic to save the results to an array rather than the individual <div> elements.
    But again, it will be temporary storage only, ie; until you exit the page.

  6. #6
    Join Date
    Dec 2010
    Location
    Romania
    Posts
    28

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,290
    A computer memory is an unidimensional array with addresses and values. Then it is always better to work with keys (from 0 to 80 for Sudokus) to divide this keys by 9 to find the quotient (which give the row from 0 to 8) and the rest (key%9 which give the column).

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.26382 seconds
  • Memory Usage 2,911KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (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
  • (1)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)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 (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_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