iHanoi.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!DOCTYPE html>
  2. <meta charset="UTF-8">
  3. <html>
  4. <head>
  5. <title> Torre de Hanoi </title>
  6. <Style>
  7. #planoDeFundo {
  8. background :transparent;
  9. left: 10%;
  10. top : 10%;
  11. width: 80%;
  12. height: 60%;
  13. position: absolute;
  14. }
  15. #fim_button{
  16. position: absolute;
  17. border:1px solid gray;
  18. border-radius: 7%;
  19. font-size: 0.875;
  20. word-wrap: break-word;
  21. max-width: 5%;
  22. left:10%;
  23. top: 5%;
  24. }
  25. #atualiza_button{
  26. position:absolute;
  27. border:1px solid gray;
  28. border-radius: 7%;
  29. font-size: 0.875;
  30. word-wrap: break-word;
  31. max-width: 7%;
  32. left: 15%;
  33. top:5%;
  34. }
  35. #A_button{
  36. position:absolute;
  37. border:1px solid gray;
  38. border-radius: 7%;
  39. font-size: 0.875;
  40. word-wrap: break-word;
  41. width: 20%;
  42. max-width: 20%;
  43. left: 13%;
  44. top:71%;
  45. }
  46. #B_button{
  47. position:absolute;
  48. border:1px solid gray;
  49. border-radius: 7%;
  50. font-size: 0.875;
  51. word-wrap: break-word;
  52. width: 20%;
  53. max-width: 20%;
  54. left: 40%;
  55. top:71%;
  56. }
  57. #C_button{
  58. position:absolute;
  59. border:1px solid gray;
  60. border-radius: 7%;
  61. font-size: 0.875;
  62. word-wrap: break-word;
  63. width: 20%;
  64. max-width: 20%;
  65. left: 67%;
  66. top:71%;
  67. }
  68. </Style>
  69. </head>
  70. <body>
  71. <button id="fim_button">Fim</button>
  72. <button id="atualiza_button">Atualiza</button>
  73. <button id="A_button" onclick="buttonClick(0)">A</button>
  74. <button id="B_button" onclick="buttonClick(1)">B</button>
  75. <button id="C_button" onclick="buttonClick(2)">C</button>
  76. <select name ="Disks" onchange="reinicio()" id="selectDisks">
  77. <option value = "1">1 Disk</option>
  78. <option value = "2">2 Disk</option>
  79. <option value = "3">3 Disk</option>
  80. <option value = "4">4 Disk</option>
  81. <option value = "5">5 Disk</option>
  82. <option value = "6">6 Disk</option>
  83. <option value = "7">7 Disk</option>
  84. </select>
  85. <canvas id="planoDeFundo" width="1024" height="768">
  86. Seu navegador não suporta o recurso Canvas. Atualize-o para utilizar esse recurso
  87. </canvas>
  88. <script>
  89. var canvas = document.getElementById('planoDeFundo');
  90. var context = canvas.getContext('2d');
  91. var towerA = [], towerB = [], towerC = [];
  92. var toMove = null;
  93. var idF=" ", idT= " ";
  94. var nMovements = 0, nDisks = 0;
  95. //NavyBlue Lime Fuchsia lightPink Orange LightSteelBlue Pink
  96. var colors = ["#000080","#00FF00", "#FF00FF", "#FFC0CB", "#FFA500", "#B0C4DE","#F441AC"];
  97. function inicio (){
  98. nDisks = document.getElementById('selectDisks').value;
  99. var x = 0;
  100. var y = 558;
  101. var w = 330;
  102. for (var i = 0; i < nDisks; i++) {
  103. var disk = new Disk(x,y,w,40,colors[i]);
  104. towerA.push(disk);
  105. x+=25; //moves to the right
  106. y-=42; //moves up, +1 to separate disks
  107. w-=50; //reduces disk width
  108. }
  109. drawScene();
  110. console.log(y);
  111. }
  112. class Disk{
  113. constructor(x,y,w,h,fill){
  114. this.x=x;
  115. this.y=y;
  116. this.w=w;
  117. this.h=h;
  118. this.fill=fill;
  119. }
  120. draw(){
  121. context.fillStyle = this.fill;
  122. context.fillRect(this.x, this.y, this.w, this.h);
  123. }
  124. clear(){
  125. context.clearRect(this.x, this.y, this.w, this.h);
  126. }
  127. }
  128. function reinicio (){
  129. while(towerA.length>0){
  130. towerA.pop();
  131. }
  132. while(towerB.length>0){
  133. towerB.pop();
  134. }
  135. while(towerC.length>0){
  136. towerC.pop();
  137. }
  138. nMovements=0;
  139. idF = " ";
  140. idT=" ";
  141. toMove=null;
  142. inicio();
  143. }
  144. function drawTowers(){
  145. context.beginPath();
  146. <!-- torre 1 -->
  147. context.fillStyle = 'black';
  148. context.fillRect(160, 260, 10, 340);//x, y, largura, altura
  149. <!--Base t1-->
  150. context.fillRect(0, 600, 330, 15);
  151. <!-- torre 2 -->
  152. context.fillRect(505, 260 ,10, 340);
  153. <!--Base t2-->
  154. context.fillRect(345, 600, 330, 15);
  155. <!-- torre 3 -->
  156. context.fillRect(850, 260, 10, 340);
  157. <!--Base t3-->
  158. context.fillRect(690, 600, 330, 15);
  159. }
  160. function drawDisks (){
  161. for(i=0; i<towerA.length; i++){
  162. towerA[i].draw();
  163. }
  164. for(i=0; i<towerB.length; i++){
  165. towerB[i].draw();
  166. }
  167. for(i=0; i<towerC.length; i++){
  168. towerC[i].draw();
  169. }
  170. }
  171. function drawScene (){
  172. context.clearRect(0, 0, canvas.width, canvas.height);
  173. drawTowers();
  174. drawDisks();
  175. drawFromTo();
  176. }
  177. function buttonClick (id){
  178. console.log(id);
  179. if(toMove == null && getTower(id).length>0){
  180. toMove = id;
  181. idF= String.fromCharCode(65+id);
  182. idT= " ";
  183. drawScene();
  184. } else if(toMove != id){
  185. idT = String.fromCharCode(65+id);
  186. moveDisk(toMove, id);
  187. toMove=null;
  188. }
  189. }
  190. function getTower (id){
  191. switch(id){
  192. case 0:
  193. return towerA;
  194. case 1:
  195. return towerB;
  196. case 2:
  197. return towerC;
  198. }
  199. }
  200. function drawFromTo (){
  201. context.font = "50px Arial";
  202. context.fillStyle = "black";
  203. context.fillText("Move from "+idF+" to ", 50, 50);
  204. context.fillText("Movements: "+nMovements, 50, 100);
  205. }
  206. function moveDisk (origin, destiny){
  207. var originTower = getTower(origin), destinyTower = getTower(destiny), topDiskOrigin, topDiskDestiny;
  208. topDiskOrigin = originTower[originTower.length-1];
  209. if(destinyTower.length>0){
  210. topDiskDestiny = destinyTower[destinyTower.length-1];
  211. if(topDiskOrigin.w<topDiskDestiny.w){
  212. topDiskOrigin.x += 345*(destiny-origin);
  213. topDiskOrigin.y += 42*(originTower.length-destinyTower.length-1);
  214. originTower.pop();
  215. destinyTower.push(topDiskOrigin);
  216. nMovements++;
  217. drawScene();
  218. } else{
  219. alert("Movimento Inválido");
  220. idF =" ";
  221. idT =" ";
  222. drawScene();
  223. }
  224. }else{
  225. topDiskOrigin.x += 345*(destiny-origin);
  226. topDiskOrigin.y += 42*(originTower.length-destinyTower.length-1);
  227. originTower.pop();
  228. destinyTower.push(topDiskOrigin);
  229. nMovements++;
  230. drawScene();
  231. }
  232. if(towerC.length == nDisks && nDisks!=0){
  233. alert("You Won!!!\nCongratulations!!");
  234. }
  235. }
  236. </script>
  237. <script> inicio(); </script>
  238. </body>
  239. </html>