index.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. <canvas id="planoDeFundo">
  77. Seu navegador não suporta o recurso Canvas. Atualize-o para utilizar esse recurso
  78. </canvas>
  79. <script>
  80. var canvas = document.getElementById('planoDeFundo');
  81. var context = canvas.getContext('2d');
  82. var towerA = [], towerB = [], towerC = [], toMove = null;
  83. var idF=" ", idT=" ";
  84. function inicio (){
  85. //NavyBlue Lime Fuchsia lightPink Orange LightSteelBlue
  86. var colors = ["#000080","#00FF00", "#FF00FF", "#FFC0CB", "#FFA500", "#B0C4DE"];
  87. var x = 13.5;
  88. var y = 109;
  89. var w = 70;
  90. for (var i = 0; i < 6; i++) {
  91. var disk = new Disk(x,y,w,10,colors[i]);
  92. towerA.push(disk);
  93. x+=5; //moves to the right
  94. y-=11; //moves up, +1 to separate disks
  95. w-=10; //reduces disk width
  96. }
  97. drawScene();
  98. document.write("width:",canvas.width," height: ",canvas.height);
  99. }
  100. class Disk{
  101. constructor(x,y,w,h,fill){
  102. this.x=x;
  103. this.y=y;
  104. this.w=w;
  105. this.h=h;
  106. this.fill=fill;
  107. }
  108. draw(){
  109. context.fillStyle = this.fill;
  110. context.fillRect(this.x, this.y, this.w, this.h);
  111. }
  112. clear(){
  113. context.clearRect(this.x, this.y, this.w, this.h);
  114. }
  115. }
  116. function drawTowers(){
  117. context.beginPath();
  118. <!-- torre 1 -->
  119. context.rect(45,20,5, 100);//x, y, largura, altura
  120. context.fillStyle = 'yellow';
  121. context.fill();
  122. <!--Base t1-->
  123. context.rect(13.5,120,70, 5);
  124. context.fill();
  125. <!-- torre 2 -->
  126. context.rect(145,20 ,5, 100);
  127. context.fill();
  128. <!--Base t2-->
  129. context.rect(113.5,120,70, 5);
  130. context.fill();
  131. <!-- torre 3 -->
  132. context.rect(245,20 ,5, 100);
  133. context.fill();
  134. <!--Base t2-->
  135. context.rect(213.5,120,70, 5);
  136. context.fill();
  137. }
  138. function drawDisks (){
  139. for(i=0; i<towerA.length; i++){
  140. towerA[i].draw();
  141. }
  142. for(i=0; i<towerB.length; i++){
  143. towerB[i].draw();
  144. }
  145. for(i=0; i<towerC.length; i++){
  146. towerC[i].draw();
  147. }
  148. }
  149. function drawScene (){
  150. context.clearRect(0, 0, canvas.width, canvas.height);
  151. drawTowers();
  152. drawDisks();
  153. drawFromTo();
  154. }
  155. function buttonClick (id){
  156. console.log(id);
  157. if(toMove == null && getTower(id).length>0){
  158. toMove = id;
  159. idF= String.fromCharCode(65+id);
  160. idT= " ";
  161. drawScene();
  162. } else if(toMove != id){
  163. idT = String.fromCharCode(65+id);
  164. moveDisk(toMove, id)
  165. toMove=null;
  166. }
  167. }
  168. function drawFromTo(){
  169. context.font = "10px Arial";
  170. context.strokeText("Move from "+idF+" to "+idT, 10, 10);
  171. }
  172. function getTower (id){
  173. switch(id){
  174. case 0:
  175. return towerA;
  176. case 1:
  177. return towerB;
  178. case 2:
  179. return towerC;
  180. }
  181. }
  182. function moveDisk (origin, destiny){
  183. var originTower = getTower(origin), destinyTower = getTower(destiny), topDiskOrigin, topDiskDestiny;
  184. topDiskOrigin = originTower[originTower.length-1];
  185. if(destinyTower.length>0){
  186. topDiskDestiny = destinyTower[destinyTower.length-1];
  187. if(topDiskOrigin.w<topDiskDestiny.w){
  188. topDiskOrigin.x += 100*(destiny-origin);
  189. topDiskOrigin.y += 11*(originTower.length-destinyTower.length-1);
  190. originTower.pop();
  191. destinyTower.push(topDiskOrigin);
  192. drawScene();
  193. } else{
  194. alert("Movimento Inválido");
  195. idF =" ";
  196. idT =" ";
  197. drawScene();
  198. }
  199. }else{
  200. topDiskOrigin.x += 100*(destiny-origin);
  201. topDiskOrigin.y += 11*(originTower.length-destinyTower.length-1);
  202. originTower.pop();
  203. destinyTower.push(topDiskOrigin);
  204. drawScene();
  205. }
  206. }
  207. </script>
  208. <script> inicio(); </script>
  209. </body>
  210. </html>