Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fun(ob){ //passo l'oggetto sulla quale ho cliccato
- //se quello sopra o quello sotto della quale ho cliccato e' il div vuoto lo sposto
- if($(ob).prev().prev().prev().prev().attr('id') == "nothing" || $(ob).next().next().next().next().attr('id') == "nothing"){
- if($("#nothing").index()!=0)
- swap1(ob)
- else
- swap2(ob)
- }
- //se quello a destra e' il div vuoto (verifico anche con assoc che il div vuoto sia a sinistra ma nella riga sotto) lo sposto
- else if(($(ob).next().attr('id') == "nothing") && (assoc[parseInt($("#nothing").index())] != assoc[parseInt($(ob).index())]))
- swap1(ob)
- //se quello a sinistra e' il div vuoto (verifico anche con assoc che il div vuoto sia a destra ma nella riga sopra) lo sposto
- else if(($(ob).prev().attr('id') == "nothing") && (assoc[parseInt($("#nothing").index())] != assoc[parseInt($(ob).index())])){ //&& (as[parseInt($("#nothing").index())]!=2 || as[parseInt($(ob).index())]!=2)){
- if($("#nothing").index()!=0)
- swap1(ob)
- else
- $("#nothing").insertAfter($(ob));
- //se e' il primo div non posso eseguire prev() allora eseguo insAfter
- }
- //}
- }
- function shuf(){ //mischio la matrice
- //vettore con le posizioni possibili
- var pos=["up","down","left","right"];
- var n=Math.floor(Math.random()*(100-10)+10), location;
- for (var i=0; i<n; i++){
- location=Math.floor(Math.random()); //scelgo se destra o sinistra
- if(location==0)
- //chiamo la swap1 passando come parametro l'oggetto DOM preso dal metodo get_ob al quale passo una posizione random, in questo modo swappo con random la posizione ovvero "up, down, left, right", la direzione ovvero "sinistra e destra", il numero di volte complessivo di swap con il for
- swap1(get_ob(pos[Math.floor(Math.random()*3)]));
- else
- swap2(get_ob(pos[Math.floor(Math.random()*3)]));
- }
- }
- function get_ob(p){
- if(p=="up"){
- //se non ho sforato andando troppo indietro nel DOM
- if($("#nothing").prev().prev().prev().prev().parent().attr('id')=="puzzlearea")
- return $("#nothing").prev().prev().prev().prev();
- }
- if(p=="down"){
- if($("#nothing").next().next().next().next().parent().attr('id')=="puzzlearea")
- return $("#nothing").next().next().next().next();
- }
- if(p=="left"){
- if($("#nothing").index()!=0)
- return $("#nothing").prev();
- }
- if(p=="right"){
- if($("#nothing").index()!=15)
- return $("#nothing").next()
- }
- }
- function swap1(ob){
- aj=$("#nothing").prev();
- $("#nothing").insertBefore($(ob));
- $(ob).insertAfter($(aj));
- }
- function swap2(ob){
- aj=$("#nothing").next();
- $("#nothing").insertAfter($(ob));
- $(ob).insertBefore($(aj));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement