www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: Sudoku Game Help

  1. #1
    Join Date
    Mar 2013
    Posts
    61

    Sudoku Game Help

    I'm making a sudoku game as part of my web development course, and one of the things I need help with is this.

    Code:
    function input_values() 
    {
    	var values = document.getElementsByClassName(three-col);
    }
    So I'm using a css framwork to build my layout. I was thinking, as the game grid uses only one class, could I put all times this class is called, into a var. Then cycle through that var to build my game. This shows no errors, but I'm relativly new to Js and I would apreicate someone telling me how to test if this is right.

    The other thing I would like some help with is converting these to input text areas via Js and if anyone has any links to trutorials, or maybe an algorithm I could use to generate the puzzles.

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    You can probably find some things in this page (See the script) which solve Sudoku and display grids.
    You have probably to distinguish the board (see an interresting object in the preceding script) and his draw on the page...
    I never find good script about Sudoku generation in the past, but some pages with Sudokus like this 49.151 Sudokus with 17 entries

    Edit : Apparently this has changed see for example this paper...
    Last edited by 007Julien; 03-30-2013 at 02:49 PM.

  3. #3
    Join Date
    Mar 2013
    Posts
    61
    Thanks. I'll look through it when I can.

    I'm in a little snag other this.

    Code:
    document.getElementById("Difficulty").innerHTML="<button id = 1 onClick="returnDiff(this.id)">Easy</button> <button id = 2 onClick="returnDiff(this.id)">Normal</button> <button id = 3 onClick="returnDiff(this.id)">Hard</button>"

  4. #4
    Join Date
    Mar 2013
    Posts
    61
    Sorry. I ment to add that the problem is that it can't tell I want to write this line, becuase of all the " " inside of this.

  5. #5
    Join Date
    Nov 2010
    Posts
    1,083
    does this help?
    Code:
    document.getElementById("Difficulty").innerHTML="<button id = 1 onClick='returnDiff(this.id)'>Easy</button> <button id = 2 onClick='returnDiff(this.id)'>Normal</button> <button id = 3 onClick='returnDiff(this.id)'>Hard</button>"

  6. #6
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by xelawho View Post
    does this help?
    Code:
    document.getElementById("Difficulty").innerHTML="<button id = 1 onClick='returnDiff(this.id)'>Easy</button> <button id = 2 onClick='returnDiff(this.id)'>Normal</button> <button id = 3 onClick='returnDiff(this.id)'>Hard</button>"
    It does! Thank You. Can I ask how though? Was it really just adding an apostrophe instead of quotations? If so I feel really stupid.

  7. #7
    Join Date
    Mar 2013
    Posts
    61
    So this is an annoying problem. I'm trying to return the value of a var, that I've assigned inside a function the the var outside of it.

    Code:
    var choice;
    
    function returnDiff(clicked_id)
    {
    	choice = clicked_id;
    	alert(clicked_id);
    	return choice;
    }
    The reason for this is I want it to return the difficulty chosen. The test I've been using is this...

    [code[
    function generateGame(choice)
    {
    alert(choice);
    }
    [/code]

    Also I'm still looking for help on the first post. I know it registers all the squares that make the sudoku layout, but I've no idea how to acess the info inside.

  8. #8
    Join Date
    Nov 2010
    Posts
    1,083
    pretty much. A string lasts from the first single or double quote to the next matching one, so your code was rendering
    Code:
    document.getElementById("Difficulty").innerHTML="<button id = 1 onClick="
    as the first string, then junk in between, then
    Code:
    ">Easy</button> <button id = 2 onClick="
    as a separate, second string, etc

    you can get around it in your case by using singles within doubles (or vice versa, not important). If you need another set of quotes in there (for example if you were trying to pass another string as an argument in your onclicks) you would have to escape the quotes, which is another way to get around the problem you were having

  9. #9
    Join Date
    Nov 2010
    Posts
    1,083
    you don't need to return the value... and the only place that code is returning the value to is the onclick of the button anyway, which is kind of meaningless. All you need to do is

    Code:
    var choice;
    
    function returnDiff(clicked_id)
    {
    	choice = clicked_id;
    	alert(clicked_id);
    }
    and whenever that function is run the choice variable will be assigned whatever value "clicked_id" is

    (it may be a good time to point out that IDs that start with numbers are considered illegal html for most doctypes, unless you are coding in html5)

  10. #10
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by xelawho View Post
    you don't need to return the value... and the only place that code is returning the value to is the onclick of the button anyway, which is kind of meaningless. All you need to do is

    Code:
    var choice;
    
    function returnDiff(clicked_id)
    {
    	choice = clicked_id;
    	alert(clicked_id);
    }
    and whenever that function is run the choice variable will be assigned whatever value "clicked_id" is

    (it may be a good time to point out that IDs that start with numbers are considered illegal html for most doctypes, unless you are coding in html5)
    Thanks and I'll edit my code.

  11. #11
    Join Date
    Mar 2013
    Posts
    61
    The next thing I could with some help is reading a text file.

    http://school.maths.uwa.edu.au/~gordon/sudoku17

    This link containes thousands of possible sudoku puzzles for a 9x9 grid. What I would like to do is take a line, stick it in an array, then apply it to all my squares. The only part I need help with is the reading from a txt file.

  12. #12
    Join Date
    Mar 2013
    Posts
    61
    Just a heads up. I have solved the first post problem.

    Code:
    function generateGame(choice)
    {
    	if( choice == null)
    	{
    		alert("Please choose a Difficulty");	
    	}
    	else
    	{
    		generate = true;
    		
    		var grid = document.getElementsByClassName("three-col");
    		for(var i = 0; i < (grid.length+1); i++)
    		{
    			 var temp = grid[i];
    
                             alert("The contents of the " + (i) + ". Element is" + temp.innerHTML);
    		}
    	}
    }
    ignore the first if statement, but the solution I found was that adding innerHTML reads and then displays the content of that element. I reached this point with the help of this. http://help.dottoro.com/ljpxmhgp.php

  13. #13
    Join Date
    Nov 2010
    Posts
    1,083
    to read a text file you will have to use something like ajax (if you have the file on your own server). If it is hosted on someone else's server, you will need to use server-side coding, or possibly jsonp

  14. #14
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by xelawho View Post
    to read a text file you will have to use something like ajax (if you have the file on your own server). If it is hosted on someone else's server, you will need to use server-side coding, or possibly jsonp
    It's probably not going to be uploaded at all. I suppose a way around this would just to copy and paste the puzzles, but that just seems messy.

  15. #15
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    It's possible to copy and paste a text file in a textarea to read the content (a suit of 81 digits with 0 for leer cells) and draw a sudoku...

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