This is a hw assignment... and know its frowned upon.
Im not looking for the answer just some help.
I have to have 1 image sliced into 4 separate images.
I have to have them in a predetermined order and when you click on a image it swaps them. Ultimately when the images are all in the right place, it makes the picture whole. The part i am having trouble with is knowing when they are in the right order and setting an alert to let the user know they are in the right order.

I know i need to set an 2 arrays. One of the solved order and one to keep track of the order they are in but i cant seem how to figure that part out

//assume all pieces are in right place
// puzzleissolved=true
//is piece zero in right place?
//if (the name of the first file is not what we think it should be)
// if (document.imgaes[0].src!=F[0])
// puzzlesolved=false
//then puzzlesolved=false
//if not remember it
//is piece one in right place?
//if not remember it
//is piece two in right place?
//if not remember it
//is piece three in right place?
//if not remember it
//now ask if we remember finding anythying out of place
//if so puzzle isnt solved
//if not puzzle is solved and we celebrate
}

maybe loops through document.images[x].src and compare them to F[x] if all are the same then return true otherwise return false. You need to call the function from somewhere also. I could give you the code but want you to do it because it is homework and you need to learn from it

function Swap(img,nu,id,finish,start){
var srt=window[start],fin=window[finish],z0=0;
if (!Swap[id]){
Swap[id]=[]; // make an array as a property of function Swap with a name of id
}
if (!Swap[id][0]){
Swap[id][0]=[img,img.src,nu];
}
else if (img!=Swap[id][0][0]){
Swap[id][1]=[img,img.src];
Swap[id][0][0].src=Swap[id][1][1];
Swap[id][1][0].src=Swap[id][0][1];
srt[nu]=Swap[id][0][1].substring(Swap[id][0][1].lastIndexOf('/')+1);
srt[Swap[id][0][2]]=Swap[id][1][1].substring(Swap[id][1][1].lastIndexOf('/')+1); // chop the src to only the file name
Swap[id]=[];
for (;z0<fin.length;z0++){
if (fin[z0]!=srt[z0]){
return;
}
}
alert('complete');
}
}

I don't think you've got to the point of needing to detect the order.
Your image swapping code makes no sense. You're passing a parameter that doesn't get used, and how does the user know what effect clicking on any image will have?

xelawho

09-28-2012, 03:53 PM

rather than just feeding you the answer, it's probably going to be more helpful if you think this through in plain terms before trying to translate it to code. What it sounds like you need is:
- a starting array of jumbled images
- an array that contains the correct order
- a way of choosing one image to swap (the onclick event of the image tag, like you have, works for this)
- a way of choosing where to place that image (again, the onclick event of the image tag, like you have, works for this)
- a way of changing the jumbled array based on the user's choice (this page (http://www.w3schools.com/jsref/jsref_obj_array.asp) is flawed in many ways, but gives a good overview of your options here and examples on how to use them)
- a way of populating the image sources based on the new array order
- a way of comparing the new array to the one with the correct order

if that sounds about right, maybe you could start coding it and come back if you run into specific problems