Recursion

We are going to solve the classic Tower of Hanoi. Using the Wikipedia article I want you to write a computer program that calculates the number of moves necessary to solve Tower of Hanoi given a number of disks. There are formulas that calculate this – you are NOT going to use them. You will calculate this by implementing the recursive algorithm of the tower and counting every time a block is moved. You may also want to print out the moves. Also I recommend not allowing input greater that 7 blocks – it starts getting pretty big after that.

by Neil Daley

HTML

<H2>
Towers Of Hanoi
</H2>

Let's solve the "Towers of Hanoi". <br/><br />Enter the amount of disks to test.  Try using a number between 0 and 7.<br/><br/>

<input type="textbox" id="inputValue" /><br/>

<input type="button" id="buttonList" value="# of Moves " onclick="handle_pts()" style="color:white; background-color:blue" />

<input type="button" id="buttonClear" value="Clear List" onclick="clearScr()" style="color:white; background-color:blue" />

<div id="output"></div>

<div id="disks"></div> 
<div id="init"></div>

<div id="cmplt"></div>
<div id="final"></div>

JavaScript

// Global variables
var xp = 0; // the popped item
var mo = 0; // number of moves

/*
Performs equivalent function as keypress for onclick, cursor returns back to textbox */
function handle_pts(){
	 testInput();
  document.getElementById("inputValue").value = "";     
  document.getElementById("inputValue").select();
}

var Node = function(_content) {
  this.next = null;
  this.last = null;
  this.content = _content;
}

var Stack = function() {
    this.top = null;
    this.bottom = null;

    this.push = function(_content) {
      if (this.bottom == null) {
        this.bottom = new Node(_content);
        this.top = this.bottom;
        return this;
      }

      var addedNode = new Node(_content);
      addedNode.last = this.top;
      this.top.next = addedNode;
      this.top = addedNode;
      return this;
    }

    this.pop = function() {
      if (this.top == null) {
        alert("Stack is Empty");
        return null;
      }

      if (this.bottom == this.top) {
        this.bottom = null;
        return this.top.content;
      }

      var a = this.top.content;
      this.top = this.top.last;
      this.top.next = null;
      
      return a;
    }

    this.toString = function() {
      var str = "";
      var node = this.bottom;
      
      while (node != null) {
      	//str += node.content;
        str += " " + node.content + ", ";
        node = node.next;
      }
      return str;
    }
  }
  
 // Clears screen for clutter clean up, values remain in memory 
function clearScr(){

	document.getElementById('output').innerHTML = "";
  document.getElementById('disks').innerHTML = "";
  document.getElementById('init').innerHTML = "";
  document.getElementById('cmplt').innerHTML = "";
  document.getElementById('final').innerHTML = "";
  document.getElementById("inputValue").select();
  mo = 0;
}



//Tests input value
function testInput(){
  	var n = document.getElementById("inputValue").value;
  if (!/^[0-9]+$/.test(n))
  {
  	alert("Only numbers...