iHanoiFunctions.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. outputMsgBox.alpha = 0.8;
  175. gameWonMsg.text = "Parabéns!\n\nVocê moveu a torre em " + nMoves.text + " movimentos!\nMÍNIMO DE MOVIMENTOS POSSÍVEIS!";
  176. } else {
  177. outputMsgBox.alpha = 0.8;
  178. gameWonMsg.text = "Parabéns!\n\nVocê moveu a torre em " + nMoves.text + " movimentos!\n(Mínimo de movimentos possíveis: " + minimumMoves + ")";
  179. }
  180. } else {
  181. outputMsgBox.alpha = 0;
  182. gameWonMsg.text = ' ';
  183. }
  184. }
  185. function redoMove() {
  186. if (nCurrentMoves < moves.length) {
  187. var res = moves[nCurrentMoves].split(" ");
  188. redo = true;
  189. moveFromTo(parseInt(res[0]), parseInt(res[1]));
  190. if (nCurrentMoves == moves.length) redoButton.setInteractive({ useHandCursor: false });
  191. }
  192. }
  193. function undoMove() {
  194. if (nCurrentMoves > 0) {
  195. var res = moves[nCurrentMoves - 1].split(" ");
  196. undo = true;
  197. moveFromTo(parseInt(res[1]), parseInt(res[0]));
  198. if (nCurrentMoves == 0) undoButton.setInteractive({ useHandCursor: false });
  199. redoButton.setInteractive({ useHandCursor: true });
  200. }
  201. }
  202. function destroyDisks(torre) {
  203. for (var i = 0; i < torre.length; i++) {
  204. torre[i].destroy();
  205. }
  206. torre.splice(0, torre.length);
  207. }
  208. function restart(n = 0) {
  209. if (n > 0) diskQntity = n;
  210. restartVariables();
  211. var diskX = 220,
  212. diskY = 430;
  213. for (var i = diskQntity - 1; i >= 0; i--) {
  214. towerA[diskQntity - 1 - i] = scene.add.sprite(diskX, diskY, 'disk' + i).setScale(0.13); //.setDisplaySize(diskWidth, diskHeight);
  215. diskY -= 28;
  216. }
  217. outputMsgBox.alpha = 0;
  218. gameWonMsg.text = ' ';
  219. }
  220. function restartVariables() {
  221. destroyDisks(towerA);
  222. destroyDisks(towerB);
  223. destroyDisks(towerC);
  224. isMoving = false;
  225. moves.splice(0, moves.length);
  226. nCurrentMoves = 0;
  227. minimumMoves = Math.pow(2, diskQntity) - 1;
  228. }
  229. function selectDisks(qntity, game) {
  230. diskQntity = qntity;
  231. restartVariables();
  232. game.scene.start('sceneGame');
  233. }
  234. function invalidMovement(torre) {
  235. wrongMoveSprite.setX(220 + torre * 435);
  236. wrongBuzz.play();
  237. wrongMoveSprite.setAlpha(1);
  238. scene.time.delayedCall(1000, hideX, [], scene);
  239. }
  240. function hideX() {
  241. wrongMoveSprite.setAlpha(0);
  242. }