iHanoiFunctions.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 reset (QntityDisks){
  35. nDisks = QntityDisks;
  36. towerA.splice(0,towerA.length);
  37. towerB.splice(0,towerB.length);
  38. towerC.splice(0,towerC.length);
  39. moves.splice(0,moves.length);
  40. nMovements=0;
  41. idF = " ";
  42. idT=" ";
  43. toMove=null;
  44. inicio(QntityDisks);
  45. }
  46. function drawTowers (){
  47. context.beginPath();
  48. context.fillStyle = pat;
  49. <!-- torre 1 -->
  50. roundRect(context, 160, 260, 20, 340, 5, pat, false);//ctx, x, y, largura, altura, radius, fill, stroke(T/F)
  51. <!--Base t1-->
  52. roundRect(context, 0, 590, 330, 20, 5, pat, false);
  53. <!-- torre 2 -->
  54. roundRect(context, 505, 260, 20, 340, 5, pat, false);
  55. <!--Base t2-->
  56. roundRect(context, 345, 590, 330, 20, 5, pat, false);
  57. <!-- torre 3 -->
  58. roundRect(context, 850, 260, 20, 340, 5, pat, false);
  59. <!--Base t3-->
  60. roundRect(context, 690, 590, 330, 20, 5, pat, false);
  61. }
  62. function drawDisks (){
  63. for(i=0; i<towerA.length; i++){
  64. towerA[i].draw();
  65. }
  66. for(i=0; i<towerB.length; i++){
  67. towerB[i].draw();
  68. }
  69. for(i=0; i<towerC.length; i++){
  70. towerC[i].draw();
  71. }
  72. }
  73. function drawScene (){
  74. context.clearRect(0, 0, canvas.width, canvas.height);
  75. drawTowers();
  76. drawDisks();
  77. drawFromTo();
  78. }
  79. function buttonClick (id){
  80. if(toMove == null && getTower(id).length>0){
  81. toMove = id;
  82. idF= String.fromCharCode(65+id);
  83. idT= " ";
  84. drawScene();
  85. } else if(toMove != null && toMove != id){
  86. idT = String.fromCharCode(65+id);
  87. moveDisk(toMove, id);
  88. toMove=null;
  89. }
  90. }
  91. function getTower (id){
  92. switch(id){
  93. case 0:
  94. return towerA;
  95. case 1:
  96. return towerB;
  97. case 2:
  98. return towerC;
  99. }
  100. }
  101. function drawFromTo (){
  102. context.font = "50px Arial";
  103. context.fillStyle = "black";
  104. context.fillText("Mover de "+idF+" para "+idT, 50, 50);
  105. context.fillText("Movimentos: "+nTotalMovements, 50, 100);
  106. }
  107. function moveDisk (origin, destiny){
  108. var originTower = getTower(origin), destinyTower = getTower(destiny), topDiskOrigin, topDiskDestiny;
  109. moveTime = Math.floor( (Date.now()- startOfMove) /1000);
  110. startOfMove = Date.now();//update start of movement
  111. if(nMovements< moves.length){
  112. moves.splice(nMovements, moves.length- nMovements);
  113. }
  114. moves.push(origin+" "+destiny);
  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("Você ganhou com a quantidade ótima de movimentos "+optimalSolution+"!!!\nParabens!!!");
  122. }else{
  123. alert("Você ganhou com "+nTotalMovements+" movimentos sendo o minimo possível "+optimalSolution);
  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. }