iHanoiFunctions.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. var canvas = document.getElementById('planoDeFundo');
  2. var context = canvas.getContext('2d');
  3. var towerA = [], towerB = [], towerC = [];
  4. var toMove = null;
  5. var idF=" ", idT= " ";
  6. var nMovements = 0, nDisks;
  7. var moves="";
  8. var acertos;
  9. var optimalSolution;
  10. //NavyBlue Lime Fuchsia yellow Orange LightSteelBlue red
  11. var colors = ["#000080","#00FF00", "#FF00FF", "yellow", "#FFA500", "#B0C4DE","red"];
  12. function inicio (QntityDisks){
  13. nDisks = QntityDisks;
  14. optimalSolution= Math.pow(2,nDisks)-1;
  15. var x = 0;
  16. var y = 558;
  17. var w = 330;
  18. moves="";
  19. for (var i = 0; i < nDisks; i++) {
  20. var disk = new Disk(x,y,w,40,colors[i]);
  21. towerA.push(disk);
  22. x+=20; //moves to the right
  23. y-=42; //moves up, +1 to separate disks
  24. w-=40; //reduces disk width
  25. }
  26. drawScene();
  27. }
  28. class Disk{
  29. constructor(x,y,w,h,fill){
  30. this.x=x;
  31. this.y=y;
  32. this.w=w;
  33. this.h=h;
  34. this.fill=fill;
  35. }
  36. draw(){//(ctx, x, y, width, height, radius, fill, stroke)
  37. context.fillStyle = this.fill;
  38. context.strokeStyle = "black";
  39. roundRect(context, this.x, this.y, this.w, this.h, 20, 20, true);
  40. }
  41. clear(){
  42. context.clearRect(this.x, this.y, this.w, this.h);
  43. }
  44. }
  45. function reinicio (){
  46. while(towerA.length>0){
  47. towerA.pop();
  48. }
  49. while(towerB.length>0){
  50. towerB.pop();
  51. }
  52. while(towerC.length>0){
  53. towerC.pop();
  54. }
  55. nMovements=0;
  56. idF = " ";
  57. idT=" ";
  58. toMove=null;
  59. inicio();
  60. }
  61. function drawTowers (){
  62. context.beginPath();
  63. <!-- torre 1 -->
  64. context.fillStyle = 'black';
  65. context.fillRect(160, 260, 10, 340);//x, y, largura, altura
  66. <!--Base t1-->
  67. context.fillRect(0, 600, 330, 15);
  68. <!-- torre 2 -->
  69. context.fillRect(505, 260 ,10, 340);
  70. <!--Base t2-->
  71. context.fillRect(345, 600, 330, 15);
  72. <!-- torre 3 -->
  73. context.fillRect(850, 260, 10, 340);
  74. <!--Base t3-->
  75. context.fillRect(690, 600, 330, 15);
  76. }
  77. function drawDisks (){
  78. for(i=0; i<towerA.length; i++){
  79. towerA[i].draw();
  80. }
  81. for(i=0; i<towerB.length; i++){
  82. towerB[i].draw();
  83. }
  84. for(i=0; i<towerC.length; i++){
  85. towerC[i].draw();
  86. }
  87. }
  88. function drawScene (){
  89. context.clearRect(0, 0, canvas.width, canvas.height);
  90. drawTowers();
  91. drawDisks();
  92. drawFromTo();
  93. }
  94. function buttonClick (id){
  95. if(toMove == null && getTower(id).length>0){
  96. toMove = id;
  97. idF= String.fromCharCode(65+id);
  98. idT= " ";
  99. drawScene();
  100. } else if(toMove != null && toMove != id){
  101. idT = String.fromCharCode(65+id);
  102. moveDisk(toMove, id);
  103. toMove=null;
  104. }
  105. }
  106. function getTower (id){
  107. switch(id){
  108. case 0:
  109. return towerA;
  110. case 1:
  111. return towerB;
  112. case 2:
  113. return towerC;
  114. }
  115. }
  116. function drawFromTo (){
  117. context.font = "50px Arial";
  118. context.fillStyle = "black";
  119. context.fillText("Move from "+idF+" to "+idT, 50, 50);
  120. context.fillText("Movements: "+nMovements, 50, 100);
  121. }
  122. function moveDisk (origin, destiny){
  123. var originTower = getTower(origin), destinyTower = getTower(destiny), topDiskOrigin, topDiskDestiny;
  124. moves+="\n"+origin+" "+destiny;
  125. console.clear();
  126. console.log(moves);
  127. topDiskOrigin = originTower[originTower.length-1];
  128. if(destinyTower.length>0){
  129. topDiskDestiny = destinyTower[destinyTower.length-1];
  130. if(topDiskOrigin.w<topDiskDestiny.w){
  131. topDiskOrigin.x += 345*(destiny-origin);
  132. topDiskOrigin.y += 42*(originTower.length-destinyTower.length-1);
  133. originTower.pop();
  134. destinyTower.push(topDiskOrigin);
  135. nMovements++;
  136. drawScene();
  137. } else{
  138. alert("Movimento Inválido");
  139. idF =" ";
  140. idT =" ";
  141. drawScene();
  142. }
  143. }else{
  144. topDiskOrigin.x += 345*(destiny-origin);
  145. topDiskOrigin.y += 42*(originTower.length-destinyTower.length-1);
  146. originTower.pop();
  147. destinyTower.push(topDiskOrigin);
  148. nMovements++;
  149. drawScene();
  150. }
  151. acertos=0;
  152. if(towerC.length == nDisks && nDisks!=0){
  153. acertos=1;
  154. if(nMovements==optimalSolution){
  155. alert("You Won in the least amount of movements!!!\nCongratulations!!!");
  156. }else{
  157. alert("You Won!!!\nCongratulations!!");
  158. }
  159. }
  160. }
  161. function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
  162. if (typeof stroke == "undefined" ) {
  163. stroke = true;
  164. }
  165. if (typeof radius === "undefined") {
  166. radius = 5;
  167. }
  168. ctx.beginPath();
  169. ctx.moveTo(x + radius, y);
  170. ctx.lineTo(x + width - radius, y);
  171. ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  172. ctx.lineTo(x + width, y + height - radius);
  173. ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  174. ctx.lineTo(x + radius, y + height);
  175. ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  176. ctx.lineTo(x, y + radius);
  177. ctx.quadraticCurveTo(x, y, x + radius, y);
  178. ctx.closePath();
  179. if (stroke) {
  180. ctx.stroke();
  181. }
  182. if (fill) {
  183. ctx.fill();
  184. }
  185. }
  186. inicio();