www.webdeveloper.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32

Thread: Sudoku Game Help

  1. #16
    Join Date
    Mar 2013
    Posts
    61
    Been a while I know, but I could do with some help. I'm not sure if this is possible within js but I want to stick arrays within an arrays, retrive a specific array and display it. This current code returns puzzleHolder as undefined. Any idea why?

    Code:
    var difficulty = new Array(Easy, Normal, Hard);
    
    var puzzle_1 = "400537091900100200850000004509080003700020405040000700108409000020050060000003000";
    Easy = puzzle_1.split("");
    var puzzle_2 = "240057360000008009378000200060041702080000005000005010130000000050003026000812000";
    Medium = puzzle_2.split("");
    var puzzle_3 = "500127008000000020039000000040070600006000002800030009005700000000400580170090300";
    Hard = puzzle_3.split("");
    
    function getDiff(choice)
    {
    	for( var q = 0; q < difficulty.length; q++)
    	{
    		var temp_string = difficulty[q].toString();
    		//alert(temp_string);
    		if( choice == temp_string )
    		{
    			//alert(temp_string);
    			puzzleHolder = difficulty[temp_string];
    			//alert(puzzleHolder);
    			
    			return puzzleHolder;
    		}
    	}	
    }
    I think after this it's a very simple process of displaying the sample puzzles.

  2. #17
    Join Date
    Dec 2012
    Posts
    100
    You're defining your difficulty array before you define you're easy/medium/hard arrays, so you're adding a bunch of undefined variables.

  3. #18
    Join Date
    Mar 2013
    Posts
    61
    thanks for the quick repsonce. I've tried shifiting the difficulty array below and it still returns it as undefined.

  4. #19
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    It's too possible to define javascript Arrays like this http://mamisab.chez-alice.fr/sudoku/Su17Epb.js with sudokus. Then there is no need of Ajax to load sudokus...

  5. #20
    Join Date
    Dec 2012
    Posts
    100
    Quote Originally Posted by KarneeKarnay View Post
    thanks for the quick repsonce. I've tried shifiting the difficulty array below and it still returns it as undefined.
    You're also passing "Normal" into the difficulty array, I think that's supposed to be "Medium"

  6. #21
    Join Date
    Mar 2013
    Posts
    61
    I think I solved it but now something werid has happened. My For loop has stopped working.

    Code:
    for( var q = 0; q < difficulty.length; q++)
    	{
    		alert(difficulty[q]);
    		if(choice == q)
    		{
    			var temp_string = difficulty[q].toString();
    			//alert(temp_string);
    		}
    		puzzleHolder = temp_string;	
    		//alert(puzzleHolder);
    		return puzzleHolder;
    		
    	}
    q doesn't increment. difficulty.length is = 3. Yet q stays at 0. I've no idea why.

  7. #22
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by 007Julien View Post
    It's too possible to define javascript Arrays like this http://mamisab.chez-alice.fr/sudoku/Su17Epb.js with sudokus. Then there is no need of Ajax to load sudokus...
    Thanks. I'll implement it when I can.

    Quote Originally Posted by iBeZi View Post
    You're also passing "Normal" into the difficulty array, I think that's supposed to be "Medium"
    You're right. Edited.

  8. #23
    Join Date
    Dec 2012
    Posts
    100
    It's because of the return statement, once the function has returned something it stops being executed, so the loop ends after its first iteration.

  9. #24
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by iBeZi View Post
    It's because of the return statement, once the function has returned something it stops being executed, so the loop ends after its first iteration.

    Thanks. Edited and it works. Next problem. A very strange one.

    Code:
    function hint(solution)
    { 
    	if(solution == null)
    	{
    		alert("Please choose a game");
    	}
            else
            {
                  alert("YAY!!!");
            }
    }
    
    //HTML
    
    <button onclick="hint(solution)">Hint</button>
    The problem is it doesn't return anything. I checked that when the button is clicked, apparently it doesn't think this is a function. Here's the error:

    Code:
    Uncaught TypeError: object is not a function Sudoku.html:162
    onclick

  10. #25
    Join Date
    Mar 2013
    Posts
    61
    Solved it. For some reason java doesn't like the name hint for some reason.

  11. #26
    Join Date
    Mar 2013
    Posts
    61
    Urrrrrrrrrrrr...Just encountered a big bug. Basicially the location of the cells in the grid have just revealed a big problem.

    Here is a standard puzzle line.

    Code:
    564127938718359426239684715941278653356941872827536149485763291693412587172895364

    How this works is that the first 9 numbers, make up the content of the first row across all 3 squares. Like the below.

    http://i46.tinypic.com/28s4z07.png

    My grid however generates it like this...

    http://i45.tinypic.com/15doah.png

    You could already imagine the porblems. I have decided to creat id's for evey cell in a row. Row 1, Row 2, yadda yadda. Below is my attempt to rectify this.

    Code:
    function debuger()
    {
    	row_1 = document.getElementById("row_1");
    	row_2 = document.getElementById("row_2");
    	row_3 = document.getElementById("row_3");
    	row_4 = document.getElementById("row_4");
    	row_5 = document.getElementById("row_5");
    	row_6 = document.getElementById("row_6");
    	row_7 = document.getElementById("row_7");
    	row_8 = document.getElementById("row_8");
    	row_9 = document.getElementById("row_9");
    	
    	for(var i = 0; i < puzzle_3_sol.length; i++)
    	{
    		if( i >! 9)
    		{
    			row_1[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 18)
    		{
    			row_2[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 27)
    		{
    			row_3[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 36)
    		{
    			row_4[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 45)
    		{
    			row_5[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 54)
    		{
    			row_6[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 63)
    		{
    			row_7[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 72)
    		{
    			row_8[i].innerHTML=puzzle_3_sol[i];	
    		}
    		if( i >! 81)
    		{
    			row_9[i].innerHTML=puzzle_3_sol[i];	
    		}
    		else
    		{
    			alert("herp");
    		}
    	}
    
    }
    The probelm I'm having is the if statments don't work. I think I could fix this myself except that this is a very bad way to do it. Any suggestions on other ways of doing this? eg. every 9 objects in the array move onto the next row.

  12. #27
    Join Date
    Jan 2010
    Posts
    77
    Hi bud

    Here is one possible solution to your problem, what I have done is grabbed the table and made an array of the table rows then looped through the rows and looped through the cells.
    Code:
    <html>
      <head>
        <script type="text/javascript">
          function populateTable(puzzle_sol){
            puzzle_sol = puzzle_sol.split("");
            var tblRows=document.getElementById("tbl1").getElementsByTagName("tr"); // Make array of rows
            var n=0;
            for(var r=0;r<9;r++){  // Loop through rows
               for(var i=0;i<9;i++){  // Loop through cells
                 tblRows[r].childNodes[i].innerHTML= puzzle_sol[n];
                 n++;
                                   }
                                }
                                           }
        onload=function (){populateTable("564127938718359426239684715941278653356941872827536149485763291693412587172895364")}
        </script>
      </head>
      <body>
        <table id="tbl1">
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
        </table>
      </body>
    </html>
    V

  13. #28
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by Vince616 View Post
    Hi bud

    Here is one possible solution to your problem, what I have done is grabbed the table and made an array of the table rows then looped through the rows and looped through the cells.
    Code:
    <html>
      <head>
        <script type="text/javascript">
          function populateTable(puzzle_sol){
            puzzle_sol = puzzle_sol.split("");
            var tblRows=document.getElementById("tbl1").getElementsByTagName("tr"); // Make array of rows
            var n=0;
            for(var r=0;r<9;r++){  // Loop through rows
               for(var i=0;i<9;i++){  // Loop through cells
                 tblRows[r].childNodes[i].innerHTML= puzzle_sol[n];
                 n++;
                                   }
                                }
                                           }
        onload=function (){populateTable("564127938718359426239684715941278653356941872827536149485763291693412587172895364")}
        </script>
      </head>
      <body>
        <table id="tbl1">
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
        </table>
      </body>
    </html>
    V
    That would work but I'm doing through CSS framwork. Tables aren't an option. I think I've narrowed down why it isn't working.

    Code:
    function debuger()
    {
    	row_1 = document.getElementById("row_1");
    	row_2 = document.getElementById("row_2");
    	row_3 = document.getElementById("row_3");
    	row_4 = document.getElementById("row_4");
    	row_5 = document.getElementById("row_5");
    	row_6 = document.getElementById("row_6");
    	row_7 = document.getElementById("row_7");
    	row_8 = document.getElementById("row_8");
    	row_9 = document.getElementById("row_9");
    	
    	alert(row_1.length);
    	
    	for( var q = 0; q < row_1.length; q++ )
    	{
    		var temper = row_1[q];
    		alert("The contents of the " + (q) + ". Element is" + temper.innerHTML);
    	}
    	
    	//for(var i = 0; i < puzzle_3_sol.length; i++)
    	//{ 
    		//var temp = puzzle_3_sol[i];
    		
    		//if( i < 10)
    		//{
    			//row_1[i].innerHTML=temp;	
    		//}
    		//else
    		//{
    			//alert("herp");
    		//}
    	//}
    
    }
    As you can see each row should be populated by elements with a certian Id. The problem I think is that these rows aren't being populated. They can't find the Id's. This makes no sense to me, but in my mind it's the only conclusion. I say this beacuse I've treid to find out the length of the row_1 and it keeps getting returned as undefined. Please guys any help?

  14. #29
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    Javascript is made to work with scripts ! Do not write so many lines...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    body, html{font-family:courrier;font-size:12px;}
    td{width:20px;height:20px;padding:3px;border:1px solid gray;border-collapse:collapse;text-align:center;}
    </style>
    </head>
    <body>
    <div id="rsp"></div>
    <script type="text/javascript">
    // Your data : a string which give immediately an array with a split ! 
    var s="564127938718359426239684715941278653356941872827536149485763291693412587172895364".split('');
    // A string to build the HTML content (a table)
    var str='<table>';
    // A loop to build the game (to insert in a function) 
    for (var i=0;i<81;i++){
    	// A new line
    	if (!i%9) str+='<tr>';
    	// A cell with its content s[i] and its id cl0, cl1, cl2 etc...
    	str+='<td id="cl'+i+'">'+s[i]+'</td>';
    	// A end of line 
    	if (i%9==8) str+='</tr>';
    }
    // An alert to see our building 
    alert(str);
    document.getElementById('rsp').innerHTML=str+'</table>';
    </script>
    </body>
    </html>
    Then it's easy to find the cell at the 7th row and 2nd col (our rows and cols are 0,1,2,3,4,5,6,7,8). Its id is "cl55" (55=(7-1)*9+(2-1)) !
    It's to easy to place input type text (without border) in cells to allow new data...
    Last edited by 007Julien; 04-14-2013 at 04:41 AM.

  15. #30
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by 007Julien View Post
    Javascript is made to work with scripts ! Do not write so many lines...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    body, html{font-family:courrier;font-size:12px;}
    td{width:20px;height:20px;padding:3px;border:1px solid gray;border-collapse:collapse;text-align:center;}
    </style>
    </head>
    <body>
    <div id="rsp"></div>
    <script type="text/javascript">
    // Your data : a string which give immediately an array with a split ! 
    var s="564127938718359426239684715941278653356941872827536149485763291693412587172895364".split('');
    // A string to build the HTML content (a table)
    var str='<table>';
    // A loop to build the game (to insert in a function) 
    for (var i=0;i<81;i++){
    	// A new line
    	if (!i%9) str+='<tr>';
    	// A cell with its content s[i] and its id cl0, cl1, cl2 etc...
    	str+='<td id="cl'+i+'">'+s[i]+'</td>';
    	// A end of line 
    	if (i%9==8) str+='</tr>';
    }
    // An alert to see our building 
    alert(str);
    document.getElementById('rsp').innerHTML=str+'</table>';
    </script>
    </body>
    </html>
    Then it's easy to find the cell at the 7th row and 2nd col (our rows and cols are 0,1,2,3,4,5,6,7,8). Its id is "cl55" (55=(7-1)*9+(2-1)) !
    It's to easy to place input type text (without border) in cells to allow new data...
    Trust me I wish I could do it this way, my professor gave us a very strict breif. If I could I would do it your way becuase it would days of work. Btw I solved it. The problem was that getElementbyId only returns one element. Not multiple like any of the others. Pretty dumb, but I preserverd and now i have a class that acts as an id for each row. The only problem being now the generation for loop has stopped working.

    Code:
    for(var i = 0; i < temp_grid.length+1; i++)
    	{
    		//temp_grid[i].innerHTML=i;
    		alert(i);
    
    		row_1[i].innerHTML=puzzle_3_sol[i];		
    		row_2[i].innerHTML=puzzle_3_sol[i];
    		row_3[i].innerHTML=puzzle_3_sol[i];	
    		row_4[i].innerHTML=puzzle_3_sol[i];	
    		row_5[i].innerHTML=puzzle_3_sol[i];
    		row_6[i].innerHTML=puzzle_3_sol[i];
    		row_7[i].innerHTML=puzzle_3_sol[i];
    		row_8[i].innerHTML=puzzle_3_sol[i];
    		row_9[i].innerHTML=puzzle_3_sol[i];
    	}
    For some reason i never goes past 9. I've checked each of the parameters in the for loop and it should be going to at least 81. The problem I think lies in that the row's can only go up to 9 and becaue of that the function refuses to go futher. I've treid if statements, but they only work on the first row (first 9). If I am right any ideas how to solve this problem.

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