iHanoiFunctions.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. var isMoving = false,
  2. diskOrigin, diskDestiny, movements;
  3. var nWrongMoves = 0;
  4. var moves = [],
  5. totalMoves = []; //vector to save movements
  6. var acertou = 0;
  7. var optimalSolution;
  8. var redo = false,
  9. undo = false;
  10. var textDict = {
  11. disk0: "stroked0",
  12. disk1: "stroked1",
  13. disk2: "stroked2",
  14. disk3: "stroked3",
  15. disk4: "stroked4",
  16. disk5: "stroked5",
  17. stroked0: "disk0",
  18. stroked1: "disk1",
  19. stroked2: "disk2",
  20. stroked3: "disk3",
  21. stroked4: "disk4",
  22. stroked5: "disk5"
  23. };
  24. var wrongBuzz = new Audio('assets/sounds/wrongBuzz.mp3');
  25. //freesounds.org
  26. //To know which tower was selected
  27. function getTower(id) {
  28. switch (id) {
  29. case 0:
  30. return towerA;
  31. case 1:
  32. return towerB;
  33. case 2:
  34. return towerC;
  35. }
  36. }
  37. function selectA() {
  38. if (!isMoving) {
  39. if (towerA.length > 0) {
  40. diskOrigin = 0;
  41. isMoving = true;
  42. towerA[towerA.length - 1].setTexture(textDict[towerA[towerA.length - 1].texture.key]);
  43. }
  44. } else {
  45. if (diskOrigin != 0) {
  46. moveFromTo(diskOrigin, 0);
  47. } else {
  48. towerA[towerA.length - 1].setTexture(textDict[towerA[towerA.length - 1].texture.key]);
  49. isMoving = false;
  50. nMoves.text++;
  51. }
  52. }
  53. }
  54. function selectB() {
  55. if (!isMoving) {
  56. if (towerB.length > 0) {
  57. diskOrigin = 1;
  58. isMoving = true;
  59. towerB[towerB.length - 1].setTexture(textDict[towerB[towerB.length - 1].texture.key]);
  60. }
  61. } else {
  62. if (diskOrigin != 1) {
  63. moveFromTo(diskOrigin, 1);
  64. } else {
  65. towerB[towerB.length - 1].setTexture(textDict[towerB[towerB.length - 1].texture.key]);
  66. isMoving = false;
  67. nMoves.text++;
  68. }
  69. }
  70. }
  71. function selectC() {
  72. if (!isMoving) {
  73. if (towerC.length > 0) {
  74. diskOrigin = 2;
  75. isMoving = true;
  76. towerC[towerC.length - 1].setTexture(textDict[towerC[towerC.length - 1].texture.key]); //levantar o disco
  77. }
  78. } else {
  79. if (diskOrigin != 2) {
  80. moveFromTo(diskOrigin, 2);
  81. } else {
  82. towerC[towerC.length - 1].setTexture(textDict[towerC[towerC.length - 1].texture.key]);
  83. isMoving = false;
  84. nMoves.text++;
  85. }
  86. }
  87. }
  88. function moveFromTo(origin, destiny) {
  89. var originTower = getTower(origin),
  90. destinyTower = getTower(destiny);
  91. var topDiskOrigin = originTower[originTower.length - 1],
  92. topDiskDestiny;
  93. if (!undo && !redo) {
  94. topDiskOrigin.setTexture(textDict[topDiskOrigin.texture.key]);
  95. }
  96. if (destinyTower.length > 0) {
  97. //movement to a tower with a disk in it
  98. topDiskDestiny = destinyTower[destinyTower.length - 1];
  99. if (topDiskOrigin.displayWidth <= topDiskDestiny.displayWidth) {
  100. topDiskOrigin.x += 435 * (destiny - origin);
  101. topDiskOrigin.y += 28 * (originTower.length - destinyTower.length - 1);
  102. originTower.pop();
  103. destinyTower.push(topDiskOrigin);
  104. //control variables
  105. nMoves.text++;
  106. totalMoves.push(origin + " " + destiny);
  107. if (!undo) {
  108. //if it is either a redo or a new move we must add new moves to the current sequence for possible 'undos'
  109. if (!redo) {
  110. //if it is a new move, we clean all possible "redos"
  111. moves.splice(nCurrentMoves, moves.length - nCurrentMoves);
  112. moves.push(origin + " " + destiny);
  113. }
  114. nCurrentMoves++;
  115. undoButton.setInteractive({ useHandCursor: true });
  116. undoButton.setTexture('undo');
  117. if (nCurrentMoves == moves.length) {
  118. redoButton.disableInteractive();
  119. redoButton.setTexture('redoDisabled');
  120. }
  121. redo = false;
  122. } else {
  123. nCurrentMoves--;
  124. undo = false;
  125. redoButton.setInteractive({ useHandCursor: true });
  126. redoButton.setTexture('redo');
  127. if (nCurrentMoves == 0) {
  128. undoButton.disableInteractive();
  129. undoButton.setTexture('undoDisabled');
  130. }
  131. }
  132. } else {
  133. invalidMovement(destiny);
  134. }
  135. } else {
  136. //move to a tower without a disk in it
  137. topDiskOrigin.x += 435 * (destiny - origin);
  138. topDiskOrigin.y += 28 * (originTower.length - destinyTower.length - 1);
  139. originTower.pop();
  140. destinyTower.push(topDiskOrigin);
  141. //control variables
  142. nMoves.text++;
  143. totalMoves.push(origin + " " + destiny);
  144. if (!undo) {
  145. //if it is either a redo or a new move we must add new moves to the current sequence for possible 'undos'
  146. if (!redo) {
  147. //if it is a new move, we clean all possible "redos" and add the new move to the list
  148. moves.splice(nCurrentMoves, moves.length - nCurrentMoves);
  149. moves.push(origin + " " + destiny);
  150. }
  151. nCurrentMoves++;
  152. if (nCurrentMoves == moves.length) {
  153. redoButton.disableInteractive();
  154. redoButton.setTexture('redoDisabled');
  155. }
  156. undoButton.setInteractive({ useHandCursor: true });
  157. undoButton.setTexture('undo');
  158. redo = false;
  159. } else {
  160. nCurrentMoves--;
  161. undo = false;
  162. redoButton.setInteractive({ useHandCursor: true });
  163. redoButton.setTexture('redo');
  164. if (nCurrentMoves == 0) {
  165. undoButton.disableInteractive();
  166. undoButton.setTexture('undoDisabled');
  167. }
  168. }
  169. }
  170. movements += origin + " " + destiny + "\n"
  171. isMoving = false;
  172. if (towerC.length == diskQntity) {
  173. if (nMoves.text == minimumMoves) {
  174. gameWonMsg.text = "Você terminou de mover a torre com o menor \n número de movimentos possivel " + nMoves.text + "!!";
  175. } else {
  176. gameWonMsg.text = "Você terminou de mover a torre em " + nMoves.text + " movimentos!!\n O minimo eram " + minimumMoves + " movimentos";
  177. }
  178. } else {
  179. gameWonMsg.text = '';
  180. }
  181. }
  182. function redoMove() {
  183. if (nCurrentMoves < moves.length) {
  184. var res = moves[nCurrentMoves].split(" ");
  185. redo = true;
  186. moveFromTo(parseInt(res[0]), parseInt(res[1]));
  187. if (nCurrentMoves == moves.length) redoButton.setInteractive({ useHandCursor: false });
  188. }
  189. }
  190. function undoMove() {
  191. if (nCurrentMoves > 0) {
  192. var res = moves[nCurrentMoves - 1].split(" ");
  193. undo = true;
  194. moveFromTo(parseInt(res[1]), parseInt(res[0]));
  195. if (nCurrentMoves == 0) undoButton.setInteractive({ useHandCursor: false });
  196. redoButton.setInteractive({ useHandCursor: true });
  197. }
  198. }
  199. function destroyDisks(torre) {
  200. for (var i = 0; i < torre.length; i++) {
  201. torre[i].destroy();
  202. }
  203. torre.splice(0, torre.length);
  204. }
  205. function restart(n = 0) {
  206. if (n > 0) diskQntity = n;
  207. restartVariables();
  208. var diskX = 220,
  209. diskY = 430;
  210. for (var i = diskQntity - 1; i >= 0; i--) {
  211. towerA[diskQntity - 1 - i] = scene.add.sprite(diskX, diskY, 'disk' + i).setScale(0.13); //.setDisplaySize(diskWidth, diskHeight);
  212. diskY -= 28;
  213. }
  214. }
  215. function restartVariables() {
  216. destroyDisks(towerA);
  217. destroyDisks(towerB);
  218. destroyDisks(towerC);
  219. isMoving = false;
  220. moves.splice(0, moves.length);
  221. nCurrentMoves = 0;
  222. minimumMoves = Math.pow(2, diskQntity) - 1;
  223. }
  224. function selectDisks(qntity, game) {
  225. diskQntity = qntity;
  226. restartVariables();
  227. game.scene.start('sceneGame');
  228. }
  229. function invalidMovement(torre) {
  230. wrongMoveSprite.setX(220 + torre * 435);
  231. wrongBuzz.play();
  232. wrongMoveSprite.setAlpha(1);
  233. scene.time.delayedCall(1000, hideX, [], scene);
  234. }
  235. function hideX() {
  236. wrongMoveSprite.setAlpha(0);
  237. }