COP3530 Assignment 7 - Tower of Hanoi

by Ernest P. Guagliata

HTML

<h2>Tower of Hanoi</h2>
<div id='input'>
  Enter number of disks to evaluate.<br />
  <input id='numDisk' type='number' min='1' max='8' />
  <input type='button' value='Enter' onclick='buildTow(document.getElementById("numDisk").value)' />
  <input type='button' value='Reset' onclick='reset()' /><br />
  <div id='error'></div>
  Play<br />
  <input type='button' value='Slow' onclick='towHanoi(s1.length, "s1", "s2", "s3", 1000)' />
  <input type='button' value='Medium' onclick='towHanoi(s1.length, "s1", "s2", "s3", 375)' />
  <input type='button' value='Fast' onclick='towHanoi(s1.length, "s1", "s2", "s3", 0)' />
</div>
<div id='output'>
  <table>
    <tr>
      <th>S1</th><th>S2</th><th>S3</th>
    </tr>
    <tr>
      <td id='s1'></td><td id='s2'></td><td id='s3'></td>
    </tr>   
  </table>
  <div id='counter'></div>
  <div id='questions'>
  Time complexity for Tower of Hanoi is O<sub>(2<sup>n</sup> - 1)</sub><br />
  <h4>Question</h4>
  Should we be concerned with the legend of the world ending when the 64-disk solution is physically solved if it takes 1 second for each move?<br />
  <h4>Answer</h4>
  (2<sup>64</sup> - 1) s = 584.5 billion years<br />
  Scientist predict, the sun will burn out and turn into a white dwarf in 5 billon years.<br />
  It is not possible to solve on Earth.
  </div>
  <h4>Moves</h4>
  <div id='moves'></div>
</div>

CSS

div {
  line-height: 30px;
}

#input {
  white-space: nowrap;
}

h2 {
  margin: 5px 0px;
}

h4 {
  margin: 0px;
}

span {
  display: block;
}

th, td {
  width: 150px;
  text-align: center;
  vertical-align: bottom;
}
td {
  height: 240px;
}

#error {
  color: #ff0000;
}

JavaScript

window.onload = function() {
	s1 = new List('s1', null); //create doubly linked list
  s2 = new List('s2', null); //create doubly linked list
  s3 = new List('s3', null); //create doubly linked list
  
  //listen for enter key
  document.getElementById('numDisk').addEventListener('keypress', function(e) {
		if(e.keyCode === 13) {
			buildTow(document.getElementById('numDisk').value);
  	}
  });
}

var time = 0;
var timeouts = [];
var count = 0;

//define list
function List(_id, _content) {
	this.id = _id;
  this.length = 1;
  this.head = new Node(_content, null); //for new list, last = null
  this.tail = this.head; //for new list, head = tail
}

//define node
function Node(_content, _last) {
	this.id = 1001; //node id
	this.content = _content; //node contents
  this.next = null; //pointer to next node
  this.last = _last; //pointer to last node
}

//push - adds a node to the top of the stack
List.prototype.push = function(_content) {
	if(this.head == this.tail && this.head.content == null) { //adding content for the first time?
  this.head.content = _content;
  }
  else {
	this.head = new Node(_content, this.head); //List.head points to new-node, new-node.last points to old-head
  this.head.last.next = this.head; //old-head.next points to new-head
	this.head.id = this.head.last.id + 1; //assign next available node id
  this.length++;
  }
	this.print();
}

//pop - removes the last node from the stack
List.prototype.pop = function() {
	var temp = this.head.content;
  
	if(this.head == this.tail) { //if it's the last node in the stack
  	this.head.content = null;
  } else {
    	this.head = this.head.last;
  		this.head.next = null;
  		this.length--;
  	}

	this.print();
  return temp;
}

//print tower
List.prototype.print = function() {
  if(this.head.content == null) { //if the tower is empty
  	document.getElementById(this.id).innerHTML = '';
		return;
  }
  
	var current = this.head;
  var printList = '';

	while(current != null) { //prepare tower for printing...