function preload()
{
 this.length = preload.arguments.length;
 for (var i = 0; i < this.length; i++)
 {
  this[i] = new Image();
  this[i].src = preload.arguments[i];
 }
}
var pics = new preload("hanoi1.gif","hanoi2.gif",
  "hanoi3.gif","hanoi4.gif","hanoi5.gif","hanoi6.gif",
  "hanoi7.gif","hanoi1h.gif","hanoi2h.gif","hanoi3h.gif",
  "hanoi4h.gif","hanoi5h.gif","hanoi6h.gif","hanoi7h.gif");

var selectedr = null;
var selectedc = null;
var maxposts = 3;
var maxdisks = 7;
var all_posts = 3;
var startpost = 0;
var endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);
var disks = 3;
var imgwidth = 160;
var imgheight = 14;
var game_is_over = false;
var board = new Array(maxposts);
board[0] = new Array(maxdisks + 1);
board[1] = new Array(maxdisks + 1);
board[2] = new Array(maxdisks + 1);

function initboard(startpost, disks)
{
 var len = board[0].length;
 selectedc = null;
 selectedr = null;
 game_is_over = false;
 endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);
 for (i = 0; i < len; i++)
 {
  board[0][i] = 0;
  board[1][i] = 0;
  board[2][i] = 0;
 }
 for (i = len-disks, j = 0; i < len; i++, j++)
 {
  board[startpost][i] = len - j - 1;
 }
}

function drawall()
{
 for (j=0; j<board.length; j++)
 {
  for (i=0; i<board[j].length; i++)
  {
   draw(j,i, getName( board[j][i]));
  }
 }
 message("Sie können beginnen! Wählen Sie die Scheibe, die Sie bewegen möchten.");
}

function restart(start)
{
 startpost = start;
 disks = document.forms[0].disc.options[document.forms[0].disc.selectedIndex].text;
 initboard(startpost,disks);
 drawall();
}

initboard(startpost, disks);

function getName( num )
{
 if (num == 0) return "hanoip.gif";
 return "hanoi" + num + ".gif";
}

function message(str)
{
 if (!game_is_over)
 document.disp.message.value = str;
}

function isempty(num)
{
 for (i = 0; i < board[num].length; i++)
 {
  if ( board[num][i] != 0) return false;
 }
 return true;
}

function topmost(num)
{
 for (i = 0; i < board[num].length; i++)
 {
  if (board[num][i] != 0) return  i;
 }
 return -1;
}

function ispost(i,j)
{
 return (board[j][i] == 0);
}

function istopdisk(i,j)
{
 return (board[j][i-1] == 0);
}

function drawboard()
{
 document.writeln("<table cellspacing=0 cellpadding=0 border=0 bgcolor=EEEEEE>");
 document.write("<tr>");
 for (j = 0; j < board.length; j++)
 {
  document.write("<td>");
  document.write("<a href='javascript:clicked("+0+","+j+")'>");
  document.write("<img src='hanoipt.gif' border=0></a><br>");

  for (i=0; i< board[0].length; i++)
  {
   document.write("<a href='javascript:clicked("+i+","+j+")'>");
   document.write("<img src='");
   document.write(getName(board[j][i]) + "' name='pos"+ j + i + "' border=0><br></a>");
  }
  document.writeln("</td>");
 }
 document.write("</tr><tr><td bgcolor=black></td><td bgcolor=black></td><td bgcolor=black></td></TR></table>");
 document.write("<form name='disp'><br><textarea name='message' ");
 document.write("wrap=virtual rows=3 cols=45></textarea><br><br>");
 document.write("<b>Scheiben:</b> <select name=\"disc\" ");
 document.write("size=1><option selected>3<option>4<option>5");
 document.write("<option>6<option>7</select>");
 document.write(" <input type=button value=\"Spiel neu beginnen\" ");
 document.write("onClick=\"restart(startpost);\"> <input ");
 document.write("type=button value=\"Spiel lösen\" onClick=\"restart(startpost); ");
 document.write("setTimeout('hanoi(disks,startpost,endpost)',400)\"></form>");
}

function draw(x,y,name)
{
 document.images["pos"+x+""+y].src = name;
}

function clicked(i,j)
{
 document.forms[0].message.focus();
 if (game_is_over)  restart(startpost = endpost);
 if (!isselection() && ispost(i,j))
 {
  message("Wählen Sie die Scheibe aus, die Sie bewegen möchten.");
  return;
 }
 if (!ispost(i,j))
 {
  toggle(j);
  return;
 };
 if (ispost(i,j) && selectedc == j)
 {
  message("Bewegen Sie die Scheibe zu einer anderen Stange.");
  return;
 }
 if (!legalmove(j))
 {
  message("Dieser Zug ist nicht erlaubt. Versuchen Sie es erneut.");
  return;
 }
 move(j); return;
}

function legalmove(j)
{
 if (isempty(j)) return true;
 return (board[j][topmost(j)] < board[selectedc][selectedr]);
}

function isselection()
{
 return selectedc != null;
}

function toggle( num )
{
 var toppos = topmost(num);
 if (selectedc == num && selectedr == toppos)
 {
  selectedc = null; selectedr = null;
  draw(num,toppos,"hanoi" + board[num][toppos] + ".gif");
  message("Wählen Sie eine Scheibe aus, die Sie bewegen wollen.");
  return;
 }
 if (isselection())
 {
  draw(selectedc,selectedr,"hanoi" + board[selectedc][selectedr] + ".gif");
 }
 selectedc = num; selectedr = toppos;
 draw(num,toppos,"hanoi" + board[num][toppos] + "h.gif");
 message("Klicken Sie auf den Stab, auf den Sie die Scheibe legen wollen.");
}

function move( num )
{
 var toppos = (!isempty(num) ? topmost(num) : board[num].length);
 board[num][toppos-1] = board[selectedc][selectedr];
 board[selectedc][selectedr] = 0;
 draw(selectedc,selectedr,"hanoip.gif");
 draw(num,toppos-1,"hanoi" + board[num][toppos-1] + ".gif");
 selectedc = null; selectedr = null;
 message("Wählen Sie eine Scheibe, die Sie bewegen möchten.");
 game_over();
}

function hanoi(no_of_disks, start_post, goal_post)
{
 if (no_of_disks > 0)
 {
  var free_post = all_posts - start_post - goal_post;
  hanoi (no_of_disks - 1, start_post, free_post);
  toggle(start_post);
  delay(1000);
  move(goal_post);
  delay(1000);
  hanoi (no_of_disks - 1 , free_post, goal_post);
 }
}

function delay(num)
{
 for (i = 0; i < num; i++) ;
}

function game_over()
{
 var filledpost = null;
 var val = 0;
 for (k = 0; k < board.length; k++)
 {
  val += ( isempty(k) ? 1 : 0 );
  if (!isempty(k)) filledpost = k;
 }
 if (val == 2 && isempty(startpost))
 {
  message("Sie haben gewonnen!");
  game_is_over = true;
  endpost = filledpost;
 }
 return game_is_over;
}

drawboard();
message("Sie können beginnen. Wählen Sie eine Scheibe aus, die Sie bewegen möchten.");
