Assignment 7

Recursion

by F Fornos

HTML

<h3>
The Towers of Hanoi
</h3>
<br> -----------------------------------------------------------------------------------------------------------------------------------------------------------
<br>
<b>The story behind it:</b>
<br><i>Mathematical puzzle which consists of three individual pillars.
<br>One of which contains any given number of discs of different sizes placed on top of each other neatly in order. <br>Stacked from largest to smallest, from bottom to top.</i>
<br>
<br>
<b>The objective of the puzzle is to move the entire stack of discs to another pillar, obeying the following simple rules:</b>
<br> 1. Each disc can slide onto any of the pillars. However, only one disc can be moved at a time.
<br> 2. Each move consists of taking the upper disc from one of the stacks and placing it on top of another stack.
<br> 3. No disc may be placed on top of a smaller disc.
<br> -----------------------------------------------------------------------------------------------------------------------------------------------------------
<br>
<br>
<br>
<h4>
Let's give it a try...
</h4> How many discs to stack?:
<input type="textbox" id="nDiscs" value="8" size="1" />
<input type="button" value="Find the Solution" onClick="findSolution();" />
<br>
<h5>
Caution: Attempting to solve the Tower with more discs may drastically affect performance.
</h5>

<p id='reportOut'></p>

JavaScript

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

  var Queue = function() {
    this.first = null;
    this.last = null;
    this.push = function(_content) {
      if (this.first == null) {
        this.first = new Node(_content);
        this.last = this.first;
        return this;
      }

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

    this.deleteFirst = function() {
      if (this.first == null) {
        return null;
      }

      var valtoDelete = this.first.content;
      if (this.last == this.first) {
        this.first = null;
        this.last = null;
        return valtoDelete;
      } else {
        this.first = this.first.next;
        this.first.previous = null;
        return valtoDelete;
      }
    }


    this.deleteLast = function() {
      if (this.first == null) {
        return null;
      }

      var valtoDelete = this.last.content;
      if (this.last == this.first) {
        this.first = null;
        this.last = null;
        return valtoDelete;
      } else {
        this.last = this.last.previous;
        this.last.next = null;
        return valtoDelete;
      }
    }

    this.toString = function() {
      var str = "";
      var node = this.first;
      if (this.first == null) {
        str = "empty";
      } else {

      }
      while (node != null) {
        str += node.content + "&nbsp";
        node = node.next;
      }
      return str;
    }

    this.countElements = function() {
      var countX = 0;
      var node = this.first;
      while (node != null) {
        node = node.next;
        countX = countX + 1;
      }
      return countX;
    }
  }

  var valtoHold = '';
  var iCount = 0;

  //basic recursive algorith
  function basicRecursion(x, fromPillar, toPillar, sparePillar) {
    if (x > 1) {
      basicRecursion(x - 1,...