Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    img scramble puzzle help

    This is hw and I do not want the answer. The problem is I have code to randomely scramble the images inside a table 9x9. And when I scramble them one of my images is replaced by a blank img. After it is scramble when i click an image to the left,right,bottom,top of the image it swaps the blank image with it. But the problem is it only does it once, then nothing happens. Just looking for suggestions

    Thanks
    Code:
    function scramble(){
    	 			 
    				 A=[1,2,3,4,5,6,7,8,9]
    				 for (i=0;i<9;i++){
    				 r=Math.floor(Math.random()* A.length)
    				 document.images[i].src="tree0_0"+A[r]+".jpg"
    				 A.splice(r,1)	 
    				 
    					}
    					 			r=Math.floor(Math.random()*9)
    					      document.images[r].src="blank.jpg"
    					 
    					      blank=r
    }
    
    function slide(n){
    
    				
    				 if (blank-1==n){
    				  alert("left of blank")
    					temp=document.images[n].src
    					document.images[n].src=document.images[blank].src
    					document.images[blank].src=temp
    					}
    				 if (blank+1==n){
    				  alert("right of blank")
    					temp=document.images[n].src
    					document.images[n].src=document.images[blank].src
    					document.images[blank].src=temp
    					}
    				 if (blank+3==n){
    				  alert("below blank")
    					temp=document.images[n].src
    					document.images[n].src=document.images[blank].src
    					document.images[blank].src=temp
    					}
    				 if (blank-3==n){
    				  alert("above blank")
    					temp=document.images[n].src
    					document.images[n].src=document.images[blank].src
    					document.images[blank].src=temp
    					}
    
    }
    </script>
    Last edited by VIPStephan; 10-31-2012 at 10:43 AM. Reason: corrected code BB tags

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 Posts
    problem (though not obvious), a random scrambling leads to a set-up, that can only be resolved in 50% of the cases. check http://www.cut-the-knot.org/pythagoras/history15.shtml for more information.

    next one, where do you define the events?



    and if you’re too desperate: an Image Slider I once made.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You forgot to change the value of the variable blank after swapping it with tile n in your slide(n) function.

    The very last step in slide(n) should be blank = n;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Why do you duplicate the code 4 times in doing the swap with blank?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    dormilich im not to desperate, i know they cant always be solved.

    Old pedant thank you I actually realized that right after i posted it... I have for if statements for above the blank image and below and to the right and the left

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    WHY?

    Why not simply do this:
    Code:
    function slide(n)
    {
        if (blank-1 = =n)
        {
            alert("left of blank")
        } else if ( blank+1 == n ) {
            alert("right of blank")
        } else if ( blank+3 == n ) {
            alert("below blank")
        } else if ( blank-3 == n ) {
            alert("above blank")
        } else {
            alert("invalid click");
            return;
        }
        temp = document.images[n].src;
        document.images[n].src = document.images[blank].src;
        document.images[blank].src = temp;
        blank = n;
    }
    Or, if you are ready to get rid of the alerts:
    Code:
    function slide(n)
    {
        if ( n == blank-1 || n == blank+1 || n == blank+3 || n == blank-3 )
        {
            temp = document.images[n].src;
            document.images[n].src = document.images[blank].src;
            document.images[blank].src = temp;
            blank = n;
        }
    }
    Or, a bit more elegant (but probably your class hasn't covered switch yet):
    Code:
    function slide(n)
    {
        switch ( n )
        {
            case blank-1:
            case blank+1:
            case blank+3:
            case blank-3:
                temp = document.images[n].src;
                document.images[n].src = document.images[blank].src;
                document.images[blank].src = temp;
                blank = n;
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    old pedant... yes you are right, your way is way more efficiant with less code. Wasnt thinking I suppose. And I have learned switch cases in c++ but not javascript. Same principles i suppose?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    The switch in JavaScript is much more flexible than the one in Java.

    In Java, all the case values must be constants.

    In JavaScript, the case values can be expressions, as I show there.

    So it's actually even handier than the Java version, isn't it?

    Oh...and you can use strings, too.

    Code:
    switch ( name.toLowerCase() )
    {
        case "john": 
            xxxxx
           break;
        case "ma" + "ry":
           xxxxx
           break;
        default:
           xxxxxx
    }
    So really flexible!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •