Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JavaScript pic puzzle

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaScript pic puzzle

    right now here is my code for a javascript puzzle,
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     
    <html>
    <head>
     
    <title>Puzzling Puzzles</title>
    <link rel="stylesheet" type="text/css" href="../../../CSS/Global.CSS">
    <link rel="stylesheet" type="text/css" href="../../../CSS/Code.CSS">
    <script language="JavaScript" type="text/javascript">
    <!--
    var pics = new Array(14);
    var didUwin  = new Array(14);
    var moveCount=0;
    var choice;
    var win=true;
    var what
    var where
    for (var i=0;i<=14;i++)
    {
    pics[i]="Pics/"+i+".jpg";
    didUwin[i] ="Pics/"+i+".jpg";
    }
    function winCheck()
    {
     for(var i=0;i<15;i++)
     {
     	if(didUwin[i]!=pics[i])
     	{
     	 win=false;
    	 return(win);
     	}
     	else
     	{
     	 win=true;
     	}
     }
     return(win);
    }
    function randomPics()
    {
     for (var i=0;i<1000;i++)
     {
     move((Math.floor(Math.random()*15)))
     }
     for (i=0;i<=14;i++)
     {
     document.getElementById("a"+i).src=pics[i];
     }
     moveCount=0;
     document.getElementById("moveCount").value=moveCount;
     
     win=false;
    }
    function swapSquares(choice)
    {
     if (win==false)
     {
     	move(choice);
     	moveCount++;
     	var i
     	for (i=0;i<=14;i++)
     	{
     	 document.getElementById("a"+i).src=pics[i];
     	}
    	document.getElementById("moveCount").value=moveCount;
     	winCheck()
     	if (win==true)
     	{
     	 var answer = confirm("Congradualtions you won with a move count of\n"+moveCount+"\ndo you want to restart")
    	 if (answer)
    	 {randomPics()}
    	 else
    	 {return;}
     	}
     	else 
     	{return;}
     }
     else
     {}
    }
     
    function move(choice)
    {
     if (choice==0||choice==1||choice==4||choice==7||choice==10||choice==13||choice==14)//checks if the blank space is above, below, left, and right
     {
     	if (pics[choice+1]=="Pics/14.jpg")//checks to the right for blank
    	{
     
    		pics[choice+1]=pics[choice];//sets current blank to choice
    		pics[choice]="Pics/14.jpg";//sets choice to blank
    		return;
    	}
    	if (pics[choice-1]=="Pics/14.jpg")//checks to left for black
    	{
    		pics[choice-1]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	if (pics[choice-3]=="Pics/14.jpg")//checks below for blank
    	{
    		pics[choice-3]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	if (pics[choice+3]=="Pics/14.jpg")//checks above for blank
    	{
    		pics[choice+3]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	else
    	{}
     }
     if (choice==3||choice==6||choice==9||choice==12)
     {
      if (pics[choice+1]=="Pics/14.jpg")//checks to the right for blank
    	{
    		pics[choice+1]=pics[choice];//sets current blank to choice
    		pics[choice]="Pics/14.jpg";//sets choice to blank
    		return;
    	}
    	if (pics[choice-3]=="Pics/14.jpg")//checks below for blank
    	{
    		pics[choice-3]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	if (pics[choice+3]=="Pics/14.jpg")//checks above for blank
    	{
    		pics[choice+3]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	else
    	{}
     }
     if (choice==2||choice==5||choice==8||choice==11)
     {
    	if (pics[choice-1]=="Pics/14.jpg")//checks to left for black
    	{
    		pics[choice-1]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	if (pics[choice-3]=="Pics/14.jpg")//checks below for blank
    	{
    		pics[choice-3]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	if (pics[choice+3]=="Pics/14.jpg")//checks above for blank
    	{
    		pics[choice+3]=pics[choice];
    		pics[choice]="Pics/14.jpg";
    		return;
    	}
    	else
    	{}
     }
     moveCount--;
    }
    function identify()
    {
     
     for(var i=0;i<15;i++)//ID what space is incorrect
     {
     	if(didUwin[i]!=pics[i])
     	{
     	 what = i
    	 i = 15
    	}
     }
     for(var i=0;i<15;i++)
     {
     	if("pics\'+what+'.jpg" == pics[i])//ID where the needed space is
     	{
     	 where = i
    	}
     }
     alert(what)
     alert(where)
    }
    //-->
    </script>
    </head>
    <body>
     
    <table summary="Puzzle" id="puz" align="center">
    <tr>
    		<td><input type="image" src="pics\0.jpg" id="a0" onclick="swapSquares(0)">	</td>
    		<td><input type="image" src="pics\1.jpg" id="a1" onclick="swapSquares(1)"> </td>
    		<td><input type="image" src="pics\2.jpg" id="a2" onclick="swapSquares(2)"> </td>
    </tr>
    <tr>
    		<td><input type="image" src="pics\3.jpg" id="a3" onclick="swapSquares(3)"> </td>
    		<td><input type="image" src="pics\4.jpg" id="a4" onclick="swapSquares(4)"> </td>
    		<td><input type="image" src="pics\5.jpg" id="a5" onclick="swapSquares(5)"> </td>
    </tr>
    <tr>
    		<td><input type="image" src="pics\6.jpg" id="a6" onclick="swapSquares(6)"> </td>
    		<td><input type="image" src="pics\7.jpg" id="a7" onclick="swapSquares(7)"> </td>
    		<td><input type="image" src="pics\8.jpg" id="a8" onclick="swapSquares(8)"> </td>
    </tr>
    <tr>
    		<td><input type="image" src="pics\9.jpg" id="a9" onclick="swapSquares(9)"> </td>
    		<td><input type="image" src="pics\10.jpg" id="a10" onclick="swapSquares(10)"> </td>
    		<td><input type="image" src="pics\11.jpg" id="a11" onclick="swapSquares(11)"> </td>
    </tr>
    <tr>
    		<td><input type="image" src="pics\12.jpg" id="a12" onclick="swapSquares(12)"> </td>
    		<td><input type="image" src="pics\13.jpg" id="a13" onclick="swapSquares(13)"> </td>
    		<td><input type="image" src="Pics\14.jpg" id="a14" onclick="swapSquares(14)"> </tr>
    </table>
    <center>
    <input type="button" value="Id it" onclick="identify()">
    <br>
    <input type="button" value="Begin" onclick="randomPics()">
    <br>
    <input type="text" readonly="readonly" id="moveCount" value="0" size="1" disabled="disabled">
    <br><br><br>
    <a href="http://validator.w3.org/check?uri=referer">
    <img src="http://www.w3.org/Icons/valid-xhtml10"
    alt="Valid XHTML 1.0 Strict" height="31" width="88" border=0></a></center>
    </body>
    </html>

    i the initial puzzle done and now i'm working on an auto solver where you press a button and it solves for one move, right now i'm idenitifying what space is incorrect but i'm having issues with getting the second if statement to work

    function identify()
    {
     
     for(var i=0;i<15;i++)//ID what space is incorrect
     {
     	if(didUwin[i]!=pics[i])
     	{
     	 what = i
    	 i = 15
    	}
     }
     for(var i=0;i<15;i++)
     {
     	if("pics\'+what+'.jpg" == pics[i])//ID where the needed space is
     	{
     	 where = i
    	}
     }
     alert(what)
     alert(where)
    }



    link to puzzle without auto solve
    Last edited by fr334a11; March 25th, 2011 at 08:32 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaScript pic puzzle

    ok i fixed the code now i have

    function identify()
    {
     
     for(var i=0;i<15;i++)//finds what space isn't solved
     {
     	if(didUwin[i]!=pics[i])
     	{
     	 what = i
    	 i = 15
    	}
     }
     for(var i=0;i<15;i++)//finds where needed space is and where the blank is
     {
     	if("Pics/"+what+".jpg" == pics[i])
     	{
    	 where = i
    	}
    	if ("Pics/14.jpg" == pics[i])
    	{
    	 whereBlank = i
    	}
     }
     alert(what);
     alert(where);
     alert(whereBlank);

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaScript pic puzzle

    now to make a function that will move the blank space around the board to move the "where" to "what"'s location

Similar Threads

  1. Help to write Kakuro puzzle solver
    By divadola in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 12:10 PM
  2. javascript problem
    By kundan_101 in forum Member Introductions
    Replies: 2
    Last Post: December 30th, 2010, 09:02 AM
  3. Replies: 0
    Last Post: March 28th, 2010, 02:27 PM
  4. How can i put session in Javascript?
    By rajani in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 8th, 2009, 01:46 PM
  5. [SOLVED] Problem in implementation of Fifteen Puzzle with 2D Arrays
    By bruint in forum Collections and Generics
    Replies: 8
    Last Post: May 3rd, 2009, 11:37 PM