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
    FL
    Posts
    7,434

    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,270
    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
    FL
    Posts
    7,434
    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
    27

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    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



Recent Articles