dh-Materialien
JavaScript Programme
 

Schiebepuzzle




// Puzzle (c)2007 dh

var
place00X = 1*4;   // x-Koordinate des aktuell leeren Feldes
var place00Y = 1*2;   // y-Koordinate des aktuell leeren Feldes

var mouseDownX = 1*0; // x-Koordinate des angeklickten Feldes
var mouseDownY = 1*0; // y-Koordinate des angeklickten Feldes

var changeSrc = "";   // Dateiname des Bildes des angeklickten Feldes
var active = false;   // active = true : Feld wurde angeklickt

function done() {
   result = true;
   for (i = 1; i <= 5; i++) {
      if (result == false) break;
      for (j = 1; j <= 5; j++) {
         var elementsSrc = document.getElementById("b_"+i+j).src;
         len = elementsSrc.length;
         elementsSrc = elementsSrc.substring(len-7,len);
         imgName = "b"+i+j+".gif";
         if (elementsSrc == imgName){} else result = false;
         if ((i == 3) && (j == 3)) result = true;
         if (result == false) break;
      }
   }
   return result;
}

function pieceUp(nr) {
   mouseDownY = nr%10;
   h = nr - nr%10;
   h/=10;
   mouseDownX = h;

   var check = 1;
   if ((place00X==mouseDownX)&&(Math.abs(place00Y-mouseDownY)==1)) check--;
   if ((place00Y==mouseDownY)&&(Math.abs(place00X-mouseDownX)==1)) check--;
   if (check == 0) {
      elementsName = "b_" + mouseDownX + mouseDownY;
      changeSrc = document.getElementById(elementsName).src;
      len = changeSrc.length;
      changeSrc = changeSrc.substring(len-7,len);
      document.getElementById(elementsName).src="b00.gif";
      document.getElementById("b_"+place00X+place00Y).src=changeSrc;
      place00X = mouseDownX;
      place00Y = mouseDownY;
      active = false;
   }
   if (done() == true) {
      alert ("Gratuliere!");
      document.f_puzzle.b_33.src = "b33.gif";
   }
}