noobatjavascrip
10-31-2012, 12:28 AM
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
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>
Thanks
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>