Latley I am working on a school project and I have to introduce an algorithm, which in my case is the decisive algorithm for the puzzle of the Hanoi Tower. Due to my knowledge of HTML / CSS, I thought it would be nice to use these + Javascript to render steps on a web page.
I created a website, as well as a basic, recursive algorithm.
function move(n, beg, aux, end) { if (n == 1) { console.log(beg + '-->' + end + '\n'); setTowers(beg, end); } else { move(n - 1, beg, end, aux); move(1, beg, aux, end); move(n - 1, aux, beg, end); } }
Page layout (CSS code won't help here):
section#main-app .app-wrapper .tower-wrapper .tower#twr--a .block.blck--top .block.blck--middle .block.blck--bottom .tower#twr--b .block.blck--top .block.blck--middle .block.blck--bottom .tower#twr--c .block.blck--top .block.blck--middle .block.blck--bottom
Then I started to fight, because I had to somehow visualize it. I was thinking about putting each tower in an array:
var twrElemsA = document.getElementById('twr--a').getElementsByClassName('block'); towerA = jQuery.makeArray(twrElemsA);
Then the function actually sets the color. 2 things at the moment: it does not erase the color, if necessary, and it will not set the color at all, because I messed up the switch / operator, I donβt know how to do it correctly.
function setColors(target) { target.forEach(function (element) { switch (element) { case '<div class="block blck--top">': $(element).css({ 'background-color': 'red' }); break; case '<div class="block blck--middle">': $(element).css({ 'background-color': '#51616F' }); break; case '<div class="block blck--bottom">': $(element).css({ 'background-color': '#394B5A' }); break; } });
}
Now, if the move function returns, for example, A β B, setColors should then iterate over TowerB and set the background colors of all three blocks to a specific color. But the switch statement doesn't work, and I'm running out of ideas and time, which are a destructive combo.
Maybe someone knows what can help here. I appreciate any help!
Yours faithfully..