iHanoiFunctions.js 5.3 KB

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