www.webdeveloper.com
Results 1 to 9 of 9

Thread: Programming Issue...

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    58

    Programming Issue...

    A while back I worked on a sudoku game with a help of a lot of the guys on the forum. I've got a weird bug that I need some help with.

    Basically I worked on this game in the wrong way. I had to create the game using a css grid. The problem I had is that I made it without generating it through Javascript. Not a huge problem at first, but it was only after I generated the puzzles that I saw the big problem. Instead of generating puzzles by rows it generated them by every 9x9 square. This meant that not only the generation of puzzles was wrong, but comparing them to the solution would also be wrong, along with any hint method I put in. It took a long three weeks+ but I fix two of those. However coding the hint method has made me realise that my approach was awful.

    My question is.

    Do I carry on with trying to fix something that shouldn't have been this broken from the start? I'm going to pose the hint code below. Please don't scream.

    Or

    Do I wipe this thing from the earth and start again? if you think it's easier for me to simply just delete everything, and generate the grid via script then I'm going to need some help, how would I go about doing this?


    The horror that is the Hint code.

    Code:
    function hint_btn(solution, grid, puzzleHolder)
    { 
    	if(solution[0] == null)
    	{
    		alert("Please choose a game");
    	}
    	else
    	{
    		//HINT BUTTON
    		var temp = Math.floor((Math.random()*81));
    
    
    		//If temp is not = 0 then temp gets a new random number.  
    		if (puzzleHolder[temp] != 0)
    		{
    			temp = Math.floor((Math.random()*81));
    		}
    		else
    		{
    			hint = solution[temp];	
    		}
    			
    
    		//alert(hint + " and " + temp);
    			
    			//For loop through the length of the grid. If puzzleHolder[i] = 0, then cycle through that. 
    			//If i 
    			for(var i = 0; i <= grid.length; i++)
    			{
    				if(i < 9)
    				{
    					if(puzzleHolder[i] == 0)
    					{
    						//alert("The amount of 0's in this row " + i)
    						
    						//CHECK THIS SOMETHING WRONG WITH i == HINT: DONE!!!
    						if(i == temp)
    						{
    							if(row_1[i].getElementsByTagName('input').input.value != solution[i] && row_1[i].getElementsByTagName('input').input.value != "" )
    							{
    								//row_1[i].getElementsByTagName('input').input.value = hint;
    								//row_1[i].getElementsByTagName('input').input.value = hint;
    								alert("Wrong Value: " + row_1[i].getElementsByTagName('input').input.value);
    							}
    						}
    					}
    				}
    				
    				if (i>= 9  && i < 18)
    				{
    					//row_2[i-9].innerHTML=puzzleHolder[i];
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if(row_2[i-9].getElementsByTagName('input').input.value != solution[i] && row_2[i-9].getElementsByTagName('input').input.value != "" )
    							{
    								alert("Wrong Value: " + row_2[i-9].getElementsByTagName('input').input.value);
    							}
    		
    						}
    					}
    				}
    				
    				if( i >= 18 && i < 27)
    				{
    					//row_3[i-18].innerHTML=puzzleHolder[i];	
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if(row_3[i-18].getElementsByTagName('input').input.value != solution[i] && row_3[i-18].getElementsByTagName('input').input.value != "")
    							{
    						  		 alert("Wrong Value: " + row_3[i-18].getElementsByTagName('input').input.value);	
    							}
    		
    						}
    					}
    				}
    				
    				if( i >= 27 && i < 36)
    				{
    					//row_4[i-27].innerHTML=puzzleHolder[i];
    					
    					//alert(puzzleHolder[i]);	
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if( row_4[i-27].getElementsByTagName('input').input.value != solution[i] && row_4[i-27].getElementsByTagName('input').input.value != "")
    							{
    						  		 alert("Wrong Value: " + row_4[i-27].getElementsByTagName('input').input.value);	
    							}
    
    						}
    					}
    				}
    				
    				if( i >= 36 && i < 45)
    				{
    					//row_5[i-36].innerHTML=puzzleHolder[i];
    					
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if(row_5[i-36].getElementsByTagName('input').input.value != solution[i] && row_5[i-36].getElementsByTagName('input').input.value != "")
    							{
    								alert("Wrong Value: " + row_5[i-36].getElementsByTagName('input').input.value);		
    							}
    
    						}
    					}
    				}
    				
    				if( i >= 45 && i < 54)
    				{
    					//row_6[i - 45].innerHTML=puzzleHolder[i];
    					
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if( row_6[i-45].getElementsByTagName('input').input.value != solution[i] && row_6[i-45].getElementsByTagName('input').input.value != "")
    							{
    								alert("Wrong Value: " + row_6[i-45].getElementsByTagName('input').input.value);		
    							}
    	
    						}
    					}
    				}	
    				
    				if (i >= 54 && i < 63)
    				{
    					//row_7[i-54].innerHTML=puzzleHolder[i];
    					
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if(row_7[i-54].getElementsByTagName('input').input.value != solution[i] && row_7[i-54].getElementsByTagName('input').input.value != "")
    							{
    								alert("Wrong Value: " + row_7[i-54].getElementsByTagName('input').input.value);			
    							}
    
    						}
    					}
    				}
    				
    				if( i >= 63 && i < 72)
    				{
    					//row_8[i-63].innerHTML=puzzleHolder[i];
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if (row_8[i-63].getElementsByTagName('input').input.value != solution[i] && row_8[i-63].getElementsByTagName('input').input.value != "")
    							{
    					   			alert("Wrong Value: " + row_8[i-63].getElementsByTagName('input').input.value);	
    							}
    
    						}
    					}
    				}	
    				
    				if( i >= 72 && i < 82)
    				{
    					//row_9[i-72].innerHTML=puzzleHolder[i];
    					
    					if(puzzleHolder[i] == 0)
    					{
    						if(i == temp)
    						{
    							if( row_9[i-72].getElementsByTagName('input').input.value != solution[i] && row_9[i-72].getElementsByTagName('input').input.value != "")
    							{
    							 	alert("Wrong Value: " + row_9[i-72].getElementsByTagName('input').input.value);	
    							}
    						}
    					}
    				}
    			}
    		}
    }

  2. #2
    Join Date
    Mar 2013
    Posts
    58
    Code:
    <script>
    
    var grid;
     
    function create_grid()
    {
    	grid = document.getElementsByTagName("body");
    	grid.innerHTML = "GANGNAM STYLE";
    }
    </script> 
    </head> 
    <body onload="create_grid()"> 
    
    </body>
    Decided to check on the creation option. Not promising. Nothing happens. If I put an alert there it does. Any ideas?

  3. #3
    Join Date
    Jul 2012
    Posts
    55
    And to your second script, I recommend never to write anything so vaguely. Put a <p id="id"> and set the inner html inside of this.
    document.getElementById("id");

  4. #4
    Join Date
    Jul 2012
    Posts
    55
    Just quickly glimpsing,
    You could have these defined else where, but I'll show you variables that are not declared any where:
    hint
    i

    Also, do not use infinite if statements, that is terrible. At least use the switch statement likewise:
    switch(i) {
    case '1': code;
    break;
    case '2' etc..
    default: alert("This is if not cases are matched");
    }

    Also by looking at this:
    if(row_1[i].getElementsByTagName('input').input.value != solution[i] && row_1[i].getElementsByTagName('input').input.value != "" )

    I can easily tell you are avoiding function parameters as best as possible lulz.
    If you learn how to structure code better, it can really help you out, as you are more likely to run into more issues with infinite if statements, which is essentially deciding boolean for comparing two things, which can easily be flagged in a function.

    Also, getting an element by tag name ['input'] is relating to all input tags. So if you have multiple input tags they all get set to the value you are assigning, use ById("id") to be specific. (In the case you have multiple input fields as I presume you do).

    Again, look into passing parameters in functions, otherwise this whole segment of code makes you look like you're trying to attempt first grade math and applying it to calculus based physics.

    function lol(parameter, perhapsanother) {
    flag = parameter + perhapsanother;
    return flag;
    }
    now you have a replacement for condition statement.
    not
    if (checkparam == checkanotherparam) {
    param+...
    if (param..

    you should get the point

  5. #5
    Join Date
    Mar 2013
    Posts
    58
    Quote Originally Posted by jazzmasterkc View Post
    Just quickly glimpsing,
    You could have these defined else where, but I'll show you variables that are not declared any where:
    hint
    i

    Also, do not use infinite if statements, that is terrible. At least use the switch statement likewise:
    switch(i) {
    case '1': code;
    break;
    case '2' etc..
    default: alert("This is if not cases are matched");
    }

    Also by looking at this:
    if(row_1[i].getElementsByTagName('input').input.value != solution[i] && row_1[i].getElementsByTagName('input').input.value != "" )

    I can easily tell you are avoiding function parameters as best as possible lulz.
    If you learn how to structure code better, it can really help you out, as you are more likely to run into more issues with infinite if statements, which is essentially deciding boolean for comparing two things, which can easily be flagged in a function.

    Also, getting an element by tag name ['input'] is relating to all input tags. So if you have multiple input tags they all get set to the value you are assigning, use ById("id") to be specific. (In the case you have multiple input fields as I presume you do).

    Again, look into passing parameters in functions, otherwise this whole segment of code makes you look like you're trying to attempt first grade math and applying it to calculus based physics.

    function lol(parameter, perhapsanother) {
    flag = parameter + perhapsanother;
    return flag;
    }
    now you have a replacement for condition statement.
    not
    if (checkparam == checkanotherparam) {
    param+...
    if (param..

    you should get the point
    Thanks for the advice. Harsh but fair. So after the fixing the original problem I decided to go to town on it with the advice you've give me.

    Code:
    function checkElement(row, i_1, i_2, solution, row_name)
    {
    	var values = row[i_2].getElementsByTagName('input').input.value;
    
    	if(values != solution[i_1] && values != "" )
    	{
    		alert(row_name + " Wrong Value: " + values);
    	}
    	
    }
    
    function hint_btn(solution, grid, puzzleHolder)
    { 
    	if(solution[0] == null)
    	{
    		alert("Please choose a game");
    	}
    	else
    	{			
    		//For loop through the length of the grid. If puzzleHolder[i] = 0, then cycle through that. 
    		for(var i = 0; i <= grid.length; i++)
    		{
    			if(i < 9)
    			{
    				if(puzzleHolder[i] == 0)
    				{					
    					checkElement(row_1, i, i, solution, "Row 1");
    				}
    			}
    				
    			if (i>= 9  && i < 18)
    			{
    				if(puzzleHolder[i] == 0)
    				{					
    					checkElement(row_2, i,  i-9, solution, "Row 2");
    				}
    			}
    				
    			if( i >= 18 && i < 27)
    			{
    				if(puzzleHolder[i] == 0)
    				{					
    					checkElement(row_3, i,  i-18, solution, "Row 3");
    				}
    			}
    				
    			if( i >= 27 && i < 36)
    			{	
    				if(puzzleHolder[i] == 0)
    				{				
    					checkElement(row_4, i,  i-27, solution, "Row 4");
    				}
    			}
    			
    			if( i >= 36 && i < 45)
    			{				
    				if(puzzleHolder[i] == 0)
    				{			
    					checkElement(row_5, i,  i-36, solution, "Row 5");
    				}
    			}
    				
    			if( i >= 45 && i < 54)
    			{
    				if(puzzleHolder[i] == 0)
    				{					
    					checkElement(row_6, i, i-45, solution, "Row 6");
    				}
    			}	
    				
    			if (i >= 54 && i < 63)
    			{
    				if(puzzleHolder[i] == 0)
    				{
    					checkElement(row_7, i, i-54, solution, "Row 7")
    				}
    			}
    				
    			if( i >= 63 && i < 72)
    			{
    				if(puzzleHolder[i] == 0)
    				{				
    					checkElement(row_8, i, i-63, solution, "Row 8")
    				}
    			}	
    				
    			if( i >= 72 && i < 82)
    			{				
    				if(puzzleHolder[i] == 0)
    				{				
    					checkElement(row_9, i, i-72, solution, "Row 9")
    				}
    			}
    		}
    	}
    }
    The only thing I'm still not sure about are switches. How would I put them in this and how would they replace or act as if statements?

    For instance would this work. switch(i<9){

    case a:

    //something

  6. #6
    Join Date
    Jul 2012
    Posts
    55
    You cannot use boolean expressions in switch statement, so you must do so before hand. I'm seeing this: for { if{ if { } } if { { ..etc

    ._.
    lulz

    have your flags become set in the for loop (boolean) then execute the switch statement.. After you see how that works, you can even reduce the equation down more

    example:
    for(stuf) {
    //for each if statement expression, give an abstract variable to it. like i<9 = var a ..soo
    var flagOne = i < 9;
    //this shows up as true or false
    var boolOne = new Boolean(flagOne);
    switch (boolOne) {
    case 'true': var flagTwo = i
    other code if you figure some other tricks out
    break;
    }

    and so forth.. now you can use that first layer of truth tables, you even get more control as well. Then you can proceede to make the next set of truth tables that if true will output your desired content. just make sure to set the varibles to work in the right scopes

    if statements show poor knowledge of coding, only use for small things. not the whole page

    But I suggest actually reworking this into a more logical spawn so you can control the hint output better. Likewise, set the flags when you set the grid so you do not have to re-evaluate it in the hint.

  7. #7
    Join Date
    Mar 2011
    Posts
    60
    You can calculate the row number and the value of i_2.
    row number = Math.floor(i/9)
    i_2 = -9+i%9

    I'm wondering why you're loop is running to i<=grid.length, instead of i<grid.length though.

    Code:
    var row, i_2, rows = [row_1, row_2, row_3, row_4, row_5, row_6, row_7, row_8, row_9];
    
    for(var i = 0; i < grid.length; i++){
      row = Math.floor(i/9);
      i_2 = -9+i%9;
      if(puzzleHolder[i] == 0){
        checkElement(rows[row], i, i_2, solution, "Row"+(row+1));
      }
    }
    
    // or if you need to include i=81
    
    
    for(var i = 0; i <= grid.length; i++){
      row = Math.floor(i/9);
      i_2 = -9+i%9;
      if(puzzleHolder[i] == 0 && i != 81){
        checkElement(rows[row], i, i_2, solution, "Row"+(row+1));
      }
      else if(puzzleHolder[i] == 0){
        checkElement(rows[8], i, i-72, solution, "Row"+(row+1));
      }
    }

  8. #8
    Join Date
    Mar 2013
    Posts
    58
    Code:
    function checkElement(row, i_1, i_2, solution, row_name)
    {
    	var values = row[i_2].getElementsByTagName('input').input.value;
    
    	if(values != solution[i_1] && values != "" )
    	{
    		alert(row_name + " Wrong Value: " + values);
    	}
    	
    	if(values == solution[i_1])
    	{
    		puzzleHolder[i_1] = values;
    		alert(win.toString());	
    	}
    	
    }

    Thanks for all the help guys. My code looks a million times better now. I just need your help with a small problem. For some reason the code doesn't change the value of puzzleHolder[i_1]. Any ideas?

  9. #9
    Join Date
    Mar 2011
    Posts
    60
    I guess the variable puzzleHolder isn't available within the scope of the function checkElement. You're errorconsole should throw an error like 'undefined variable: puzzleHolder'.

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