iHanoiFunctions.js 5.2 KB

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