Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. function fun(ob){ //passo l'oggetto sulla quale ho cliccato
  2. //se quello sopra o quello sotto della quale ho cliccato e' il div vuoto lo sposto
  3. if($(ob).prev().prev().prev().prev().attr('id') == "nothing" || $(ob).next().next().next().next().attr('id') == "nothing"){
  4. if($("#nothing").index()!=0)
  5. swap1(ob)
  6. else
  7. swap2(ob)
  8. }
  9. //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
  10. else if(($(ob).next().attr('id') == "nothing") && (assoc[parseInt($("#nothing").index())] != assoc[parseInt($(ob).index())]))
  11. swap1(ob)
  12. //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
  13. 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)){
  14. if($("#nothing").index()!=0)
  15. swap1(ob)
  16. else
  17. $("#nothing").insertAfter($(ob));
  18. //se e' il primo div non posso eseguire prev() allora eseguo insAfter
  19.  
  20. }
  21. //}
  22. }
  23.  
  24. function shuf(){ //mischio la matrice
  25. //vettore con le posizioni possibili
  26. var pos=["up","down","left","right"];
  27. var n=Math.floor(Math.random()*(100-10)+10), location;
  28. for (var i=0; i<n; i++){
  29. location=Math.floor(Math.random()); //scelgo se destra o sinistra
  30. if(location==0)
  31. //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
  32. swap1(get_ob(pos[Math.floor(Math.random()*3)]));
  33. else
  34. swap2(get_ob(pos[Math.floor(Math.random()*3)]));
  35. }
  36. }
  37.  
  38. function get_ob(p){
  39. if(p=="up"){
  40. //se non ho sforato andando troppo indietro nel DOM
  41. if($("#nothing").prev().prev().prev().prev().parent().attr('id')=="puzzlearea")
  42. return $("#nothing").prev().prev().prev().prev();
  43.  
  44. }
  45. if(p=="down"){
  46. if($("#nothing").next().next().next().next().parent().attr('id')=="puzzlearea")
  47. return $("#nothing").next().next().next().next();
  48. }
  49. if(p=="left"){
  50. if($("#nothing").index()!=0)
  51. return $("#nothing").prev();
  52. }
  53. if(p=="right"){
  54. if($("#nothing").index()!=15)
  55. return $("#nothing").next()
  56. }
  57. }
  58.  
  59. function swap1(ob){
  60. aj=$("#nothing").prev();
  61. $("#nothing").insertBefore($(ob));
  62. $(ob).insertAfter($(aj));
  63. }
  64.  
  65. function swap2(ob){
  66. aj=$("#nothing").next();
  67. $("#nothing").insertAfter($(ob));
  68. $(ob).insertBefore($(aj));
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement