Question
In Javascript and HTML using JSfiddle We are going to solve the classic Tower of Hanoi. Using the Wikipedia article I want you to write...
In Javascript and HTML using JSfiddle We are going to solve the classic Tower of Hanoi. Using the Wikipedia article I want you to write...
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.
This can easily be implemented by using your Stack from previous assignments all you really do in this is push and pop from the stack.
Answer the following questions in the interface of your code;
1. What is the Complexity (In Big O)?
2. Should we be concerned with concerned with the legend of the world ending when the 64 disk solution is physically solved it it takes 1 seconds for each move? How many years will it take to complete the tower?
Need help correcting code down below, make sure it works and that you have read the instructions thouroughly!!!!!!
HTML
Tower of Hanoi
Enter the number of disks (Disk amount must be < 7):
Javascript
var num = 0; var t = document.getElementById('uInput').value;
var Node = function(_content) { this.content = _content; this.next = null; this.last = null; return this; }
var Stack = function(_content) { this.top = null; this.bottom = null; this.length = 0; return this; }
Stack.prototype.push = function(_content) {
var node = new Node(_content); node.content = _content;
if (this.bottom == null) { this.bottom = node; this.top = this.bottom; this.length++; return this; }
node.last = this.top; this.top.next = node; this.top = node; this.length++; return this; }
Stack.prototype.pop = function() { if (this.bottom == null) { alert('Stack is empty.'); return false; }
if (this.bottom == this.top) { this.bottom = null; this.top = null; this.length = 0; return this.top }
var popped = this.top; this.top = this.top.last; this.top.next = null; this.length--; return popped; }
Stack.prototype.toString = function() { var string = ''; var n = this.bottom;
while (n != null) { string += n.content ; n = n.next; } return string; }
Stack.prototype.reverse = function(str) { var newString = ""; for (var i = str.length - 1; i >= 0; i--) { newString += str[i]; } return newString; }
function insertDisks(i) { i = parseInt(document.getElementById('uInput').value); if (i > 7) { alert('Input must only be less than 7'); document.getElementById('uInput').value = ''; document.getElementById('uInput').focus(); return false; } for (var t = i; t > 0 ; t--) { stackA.push(t); stackA.length++; //document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString();
} document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString();
}
var stackA = new Stack(); var stackB = new Stack(); var stackC = new Stack();
function aTob() { var n = parseInt(stackA.top.content); stackA.pop(n); stackB.push(n); document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); stackB.reverse(n); }
function aToc() { var n = parseInt(stackA.top.content); stackA.pop(n); stackC.push(n); document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); stackC.reverse(n); }
function bToa(){ var n = parseInt(stackB.top.content); stackB.pop(n); stackA.push(n); document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); stackA.reverse(n); }
function bToc(){ var n = parseInt(stackB.top.content); stackB.pop(n); stackC.push(n); document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); stackC.reverse(n); }
function cToa(){ var n = parseInt(stackC.top.content); stackC.pop(n); stackA.push(n); document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); stackA.reverse(n); }
function cTob(){ var n = parseInt(stackC.top.content); stackC.pop(n); stackB.push(n); document.getElementById('output').innerHTML = 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); stackB.reverse(n); }
function runHanoi(A, C, B) { var c = stackA.length; //alert(c); if (c >= 1) { runHanoi(c-1, A, B, C); C.push(A.pop().content); num++; runHanoi(c-1, B, C, A); document.getElementById('output1').innerHTML += 'Towers: Stack A :' + stackA.toString() + '_Stack B: ' + stackB.toString() +'_Stack C: ' + stackC.toString(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started