numb=3;
hlp=['','Tower of Hanoi is a mathematical puzzle game invented by the French mathematician Edouard Lucas in 1883. There is a board with three pits. In the first pit, a number of pieces are neatly stacked in order of size. The objective of the game is to move the entire stack to a different pit. The rules: Only one piece may be moved at a time, by dragging the topmost piece from one of the stacks and dropping it into a different pit, on top of the other pieces that may already lie there. Yet, no piece may be placed on top of a smaller one. The minimal number of moves for a stack of n pieces is 2<sup>n</sup>-1. Therefore, a stack of 3 pieces can be displaced in 7 moves.']

function preload()
{
count=0;
image=['bg.jpg','bg2.jpg','12.gif','11.gif','10.gif','9.gif','8.gif','7.gif','6.gif','5.gif','4.gif','3.gif','12a.gif','11a.gif','10a.gif','9a.gif','8a.gif','7a.gif','6a.gif','5a.gif','4a.gif','3a.gif','0.gif'];
picture=new Array(image.length);
for (i=0;i<image.length;i++)
{
picture[i]=new Image();
picture[i].onload=loadcheck;
picture[i].src=image[i];
}
}

function loadcheck()
{
count++;
if (count==image.length) setup(3);
}

function setup(b)
{
number=b;
picks=[0,0];
win=0;
moves=0;
move=[0,1];
events=['onmousedown','onmouseup'];
curs="auto";
inc="<a href=\"javascript:increase("+numb+");\" title=\"More pieces\">More<\/a> | ";
dec="";
if (number==3) pits=[12,8,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==4) pits=[12,10,8,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==5) pits=[12,10,8,6,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==6) pits=[12,11,10,9,8,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==7) pits=[12,11,10,9,8,7,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==8) pits=[12,11,10,9,8,7,6,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==9) pits=[12,11,10,9,8,7,6,5,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
if (number==10) pits=[12,11,10,9,8,7,6,5,4,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
undos=new Array();
td=new Array();
cell();
message=numb+" pieces (drag & drop).";
show();
}

function select(a)
{
if (win==1) message="Game over!";
else if (a>=0&&a<10&&move[0]==1&&(pits[0]!=0&&pits[0]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[1]!=0&&pits[1]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[2]!=0&&pits[2]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[3]!=0&&pits[3]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[4]!=0&&pits[4]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[5]!=0&&pits[5]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[6]!=0&&pits[6]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[7]!=0&&pits[7]-picks[1]<0)) message="Not possible!";
else if (a>=0&&a<10&&move[0]==1&&(pits[8]!=0&&pits[8]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[10]!=0&&pits[10]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[11]!=0&&pits[11]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[12]!=0&&pits[12]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[13]!=0&&pits[13]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[14]!=0&&pits[14]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[15]!=0&&pits[15]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[16]!=0&&pits[16]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[17]!=0&&pits[17]-picks[1]<0)) message="Not possible!";
else if (a>=10&&a<20&&move[0]==1&&(pits[18]!=0&&pits[18]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[20]!=0&&pits[20]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[21]!=0&&pits[21]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[22]!=0&&pits[22]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[23]!=0&&pits[23]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[24]!=0&&pits[24]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[25]!=0&&pits[25]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[26]!=0&&pits[26]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[27]!=0&&pits[27]-picks[1]<0)) message="Not possible!";
else if (a>=20&&a<30&&move[0]==1&&(pits[28]!=0&&pits[28]-picks[1]<0)) message="Not possible!";
else if ((pits[a]!=0&&move[0]==1)||(pits[a]==0&&move[0]==0)) message="Not possible!";
else if (pits[a+1]!=0&&a!=9&&a!=19&&a!=29) message="Not possible!";
else
{
if (move[0]==0)
{
undos[moves]=new Array();
for (i=0;i<30;i++) undos[moves][i]=pits[i];
picks=[a,pits[a]];
pits[picks[0]]=0;
curs="url("+picks[1]+"a.gif), move";
}
else
{
pits[a]=picks[1];
if (a>=0&&a<10) for(i=0;i<a;i++) if (pits[a-i-1]==0&&move[0]==1) {pits[a-i]=0; pits[a-i-1]=picks[1];}
if (a>=10&&a<20) for(i=0;i<a-10;i++) if (pits[a-i-1]==0&&move[0]==1) {pits[a-i]=0; pits[a-i-1]=picks[1];}
if (a>=20&&a<30) for(i=0;i<a-20;i++) if (pits[a-i-1]==0&&move[0]==1) {pits[a-i]=0; pits[a-i-1]=picks[1];}
curs="auto";
prove(a);
if (moves==0) message=numb+" pieces (drag & drop).";
else message="Moves: "+moves;
}
move.reverse();
events.reverse();
cell();
prove(a);
}
show();
}

function cell()
{
for (i=0;i<30;i++) td[i]="<img src=\""+pits[i]+".gif\" "+events[0]+"=\"select("+i+")\">";
}

function prove(a)
{
if (
(number==3&&((pits[10]==12&&pits[11]==8&&pits[12]==4)||(pits[20]==12&&pits[21]==8&&pits[22]==4)))||
(number==4&&((pits[10]==12&&pits[11]==10&&pits[12]==8&&pits[13]==6)||(pits[20]==12&&pits[21]==10&&pits[22]==8&&pits[23]==6)))||
(number==5&&((pits[10]==12&&pits[11]==10&&pits[12]==8&&pits[13]==6&&pits[14]==4)||(pits[20]==12&&pits[21]==10&&pits[22]==8&&pits[23]==6&&pits[24]==4)))||
(number==6&&((pits[10]==12&&pits[11]==11&&pits[12]==10&&pits[13]==9&&pits[14]==8&&pits[15]==7)||(pits[20]==12&&pits[21]==11&&pits[22]==10&&pits[23]==9&&pits[24]==8&&pits[25]==7)))||
(number==7&&((pits[10]==12&&pits[11]==11&&pits[12]==10&&pits[13]==9&&pits[14]==8&&pits[15]==7&&pits[16]==6)||(pits[20]==12&&pits[21]==11&&pits[22]==10&&pits[23]==9&&pits[24]==8&&pits[25]==7&&pits[26]==6)))||
(number==8&&((pits[10]==12&&pits[11]==11&&pits[12]==10&&pits[13]==9&&pits[14]==8&&pits[15]==7&&pits[16]==6&&pits[17]==5)||(pits[20]==12&&pits[21]==11&&pits[22]==10&&pits[23]==9&&pits[24]==8&&pits[25]==7&&pits[26]==6&&pits[27]==5)))||
(number==9&&((pits[10]==12&&pits[11]==11&&pits[12]==10&&pits[13]==9&&pits[14]==8&&pits[15]==7&&pits[16]==6&&pits[17]==5&&pits[18]==4)||(pits[20]==12&&pits[21]==11&&pits[22]==10&&pits[23]==9&&pits[24]==8&&pits[25]==7&&pits[26]==6&&pits[27]==5&&pits[28]==4)))||
(number==10&&((pits[10]==12&&pits[11]==11&&pits[12]==10&&pits[13]==9&&pits[14]==8&&pits[15]==7&&pits[16]==6&&pits[17]==5&&pits[18]==4&&pits[19]==3)||(pits[20]==12&&pits[21]==11&&pits[22]==10&&pits[23]==9&&pits[24]==8&&pits[25]==7&&pits[26]==6&&pits[27]==5&&pits[28]==4&&pits[29]==3)))
)
{
message="Moves: "+(moves+1)+" &#150; congratulations!";
win=1;
}
else if (a!=picks[0]&&move[0]==1) moves++;
}

function increase()
{
numb++;
setup(numb);
}

function decrease()
{
numb--;
setup(numb);
}

function show()
{
deactivate();
menu=dec+" | "+inc+" | "+und+" | "+res+" | <a href=\"javascript:help();\" title=\"Toggle help on/off\">Help<\/a> | "+ab+" | <a href=\"mailto:thomas.weibel@bluewin.ch\" title=\"E-mail to the author\">Mail<\/a>";
var display="<p id=\"title\">Tower of Hanoi<\/p><div style=\"cursor:"+curs+";\"><table><tr><td id=\"borderh\" colspan=\"7\"><\/td><td id=\"space\" rowspan=\"12\"><\/td><td id=\"help\" rowspan=\"12\"><span>"+hlp[0]+"<\/span><\/td><\/tr><tr><td id=\"borderv\" rowspan=\"10\"><\/td><td>"+td[9]+"<\/td><td id=\"borderv\" rowspan=\"10\"><\/td><td>"+td[19]+"<\/td><td id=\"borderv\" rowspan=\"10\"><\/td><td>"+td[29]+"<\/td><td id=\"borderv\" rowspan=\"10\"><\/td><\/tr><tr><td>"+td[8]+"<\/td><td>"+td[18]+"<\/td><td>"+td[28]+"<\/td><\/tr><tr><td>"+td[7]+"<\/td><td>"+td[17]+"<\/td><td>"+td[27]+"<\/td><\/tr><tr><td>"+td[6]+"<\/td><td>"+td[16]+"<\/td><td>"+td[26]+"<\/td><\/tr><tr><td>"+td[5]+"<\/td><td>"+td[15]+"<\/td><td>"+td[25]+"<\/td><\/tr><tr><td>"+td[4]+"<\/td><td>"+td[14]+"<\/td><td>"+td[24]+"<\/td><\/tr><tr><td>"+td[3]+"<\/td><td>"+td[13]+"<\/td><td>"+td[23]+"<\/td><\/tr><tr><td>"+td[2]+"<\/td><td>"+td[12]+"<\/td><td>"+td[22]+"<\/td><\/tr><tr><td>"+td[1]+"<\/td><td>"+td[11]+"<\/td><td>"+td[21]+"<\/td><\/tr><tr><td>"+td[0]+"<\/td><td>"+td[10]+"<\/td><td>"+td[20]+"<\/td><\/tr><tr><td id=\"borderh\" colspan=\"7\"><\/td><\/table><\/div><p id=\"copyright\">&copy; 2010 twb<\/p><p>"+message+"<\/p><p id=\"menu\">"+menu+"<\/p>";
document.getElementById('screen').innerHTML=display;
}

function deactivate()
{
if (numb==10) inc="<font id=\"grey\">More<\/font>";
else inc="<a href=\"javascript:increase("+numb+");\" title=\"More pieces\">More<\/a>";
if (numb==3) dec="<font id=\"grey\">Less<\/font>";
else dec="<a href=\"javascript:decrease("+numb+");\" title=\"Less pieces\">Less<\/a>";
if (moves==0) und="<font id=\"grey\">Undo<\/font>";
else und="<a href=\"javascript:undo();\" title=\"Undo move\">Undo<\/a>";
if (moves==0) res="<font id=\"grey\">Reset<\/font>";
else res="<a href=\"javascript:setup("+numb+");\" title=\"Reset game\">Reset<\/a>";
if (message=="<p>Tower of Hanoi 1.3 by www.thomasweibel.ch<\/p>") ab="<font id=\"grey\">About<\/font>";
else ab="<a href=\"javascript:about();\" title=\"About Tower of Hanoi\">About<\/a>";
}

function undo()
{
if (win!=1) moves--;
else win=0;
for (i=0;i<30;i++) pits[i]=undos[moves][i];
undos[moves].length--;
cell();
if (moves==0) message=numb+" pieces (drag & drop).";
else message="Moves: "+moves;
show();
}

function help()
{
hlp.reverse();
show();
}

function about()
{
message="<p>Tower of Hanoi 1.3 by www.thomasweibel.ch<\/p>";
show();
}
