iHanoiFunctions.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var isMoving = false, diskOrigin, diskDestiny, movements;
  2. var nWrongMoves = 0;
  3. var moves = [], totalMoves = []; //vector to save movements
  4. var acertou=0;
  5. var optimalSolution;
  6. var redo = false, undo = false;
  7. var textDict = {disk0: "stroked0",
  8. disk1: "stroked1",
  9. disk2: "stroked2",
  10. disk3: "stroked3",
  11. disk4: "stroked4",
  12. disk5: "stroked5",
  13. stroked0: "disk0",
  14. stroked1: "disk1",
  15. stroked2: "disk2",
  16. stroked3: "disk3",
  17. stroked4: "disk4",
  18. stroked5: "disk5"}
  19. //To know which tower was selected
  20. function getTower (id){
  21. switch(id){
  22. case 0:
  23. return towerA;
  24. case 1:
  25. return towerB;
  26. case 2:
  27. return towerC;
  28. }
  29. }
  30. function selectA (){
  31. if (!isMoving){
  32. if(towerA.length>0){
  33. diskOrigin = 0;
  34. isMoving = true;
  35. towerA[towerA.length-1].setTexture(textDict[towerA[towerA.length-1].texture.key]);
  36. }
  37. } else{
  38. if(diskOrigin!=0){
  39. moveFromTo(diskOrigin, 0);
  40. } else{
  41. towerA[towerA.length-1].setTexture(textDict[towerA[towerA.length-1].texture.key]);
  42. isMoving=false;
  43. nMoves.text++;
  44. }
  45. }
  46. }
  47. function selectB (){
  48. if (!isMoving){
  49. if(towerB.length>0){
  50. diskOrigin = 1;
  51. isMoving = true;
  52. towerB[towerB.length-1].setTexture(textDict[towerB[towerB.length-1].texture.key]);
  53. }
  54. } else{
  55. if(diskOrigin!=1){
  56. moveFromTo(diskOrigin, 1);
  57. } else{
  58. towerB[towerB.length-1].setTexture(textDict[towerB[towerB.length-1].texture.key]);
  59. isMoving=false;
  60. nMoves.text++;
  61. }
  62. }
  63. }
  64. function selectC (){
  65. if (!isMoving){
  66. if(towerC.length>0){
  67. diskOrigin = 2;
  68. isMoving = true;
  69. towerC[towerC.length-1].setTexture(textDict[towerC[towerC.length-1].texture.key]); //levantar o disco
  70. }
  71. } else{
  72. if(diskOrigin!=2){
  73. moveFromTo(diskOrigin, 2);
  74. } else{
  75. towerC[towerC.length-1].setTexture(textDict[towerC[towerC.length-1].texture.key]);
  76. isMoving=false;
  77. nMoves.text++;
  78. }
  79. }
  80. }
  81. function moveFromTo (origin, destiny){
  82. var originTower = getTower(origin), destinyTower = getTower(destiny);
  83. var topDiskOrigin = originTower[originTower.length-1], topDiskDestiny;
  84. if (!undo && !redo) {
  85. topDiskOrigin.setTexture(textDict[topDiskOrigin.texture.key]);
  86. }
  87. if (destinyTower.length>0){
  88. //movement to a tower with a disk in it
  89. topDiskDestiny = destinyTower[destinyTower.length-1];
  90. if(topDiskOrigin.displayWidth<=topDiskDestiny.displayWidth){
  91. topDiskOrigin.x += 435*(destiny-origin);
  92. topDiskOrigin.y += 28*(originTower.length-destinyTower.length-1);
  93. originTower.pop();
  94. destinyTower.push(topDiskOrigin);
  95. //control variables
  96. nMoves.text++;
  97. totalMoves.push(origin+" "+destiny);
  98. if (!undo){
  99. //if it is either a redo or a new move we must add new moves to the current sequence for possible 'undos'
  100. if(!redo){
  101. //if it is a new move, we clean all possible "redos"
  102. moves.splice(nCurrentMoves, moves.length- nCurrentMoves);
  103. moves.push(origin+" "+destiny);
  104. }
  105. nCurrentMoves++;
  106. undoButton.setInteractive({useHandCursor: true});
  107. undoButton.setTexture('undo');
  108. if(nCurrentMoves==moves.length){
  109. redoButton.disableInteractive();
  110. redoButton.setTexture('redoDisabled');
  111. }
  112. redo=false;
  113. }else{
  114. nCurrentMoves--;
  115. undo=false;
  116. redoButton.setInteractive({useHandCursor: true});
  117. redoButton.setTexture('redo');
  118. if(nCurrentMoves==0){
  119. undoButton.disableInteractive();
  120. undoButton.setTexture('undoDisabled');
  121. }
  122. }
  123. }
  124. }else{
  125. //move to a tower without a disk in it
  126. topDiskOrigin.x += 435*(destiny-origin);
  127. topDiskOrigin.y += 28*(originTower.length-destinyTower.length-1);
  128. originTower.pop();
  129. destinyTower.push(topDiskOrigin);
  130. //control variables
  131. nMoves.text++;
  132. totalMoves.push(origin+" "+destiny);
  133. if (!undo){
  134. //if it is either a redo or a new move we must add new moves to the current sequence for possible 'undos'
  135. if(!redo){
  136. //if it is a new move, we clean all possible "redos" and add the new move to the list
  137. moves.splice(nCurrentMoves, moves.length- nCurrentMoves);
  138. moves.push(origin+" "+destiny);
  139. }
  140. nCurrentMoves++;
  141. if(nCurrentMoves==moves.length){
  142. redoButton.disableInteractive();
  143. redoButton.setTexture('redoDisabled');
  144. }
  145. undoButton.setInteractive({useHandCursor: true});
  146. undoButton.setTexture('undo');
  147. redo=false;
  148. }else{
  149. nCurrentMoves--;
  150. undo=false;
  151. redoButton.setInteractive({useHandCursor: true});
  152. redoButton.setTexture('redo');
  153. if(nCurrentMoves==0){
  154. undoButton.disableInteractive();
  155. undoButton.setTexture('undoDisabled');
  156. }
  157. }
  158. }
  159. movements+=origin+" "+destiny+"\n"
  160. isMoving=false;
  161. if (towerC.length == diskQntity){
  162. gameWonMsg.text = "Você terminou de mover a torre!!! Parabéns!!"
  163. }else{
  164. gameWonMsg.text ='';
  165. }
  166. }
  167. function redoMove(){
  168. if (nCurrentMoves < moves.length){
  169. var res = moves[nCurrentMoves].split(" ");
  170. redo=true;
  171. moveFromTo(parseInt(res[0]), parseInt(res[1]));
  172. if(nCurrentMoves==moves.length) redoButton.setInteractive({useHandCursor: false});
  173. }
  174. }
  175. function undoMove(){
  176. if(nCurrentMoves>0){
  177. var res = moves[nCurrentMoves-1].split(" ");
  178. undo=true;
  179. moveFromTo(parseInt(res[1]), parseInt(res[0]));
  180. if(nCurrentMoves==0) undoButton.setInteractive({useHandCursor: false});
  181. redoButton.setInteractive({useHandCursor: true});
  182. }
  183. }