123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- var isMoving = false, diskOrigin, diskDestiny, movements;
- var nWrongMoves = 0;
- var moves = [], totalMoves = []; //vector to save movements
- var acertou=0;
- var optimalSolution;
- var redo = false, undo = false;
- var textDict = {disk0: "stroked0",
- disk1: "stroked1",
- disk2: "stroked2",
- disk3: "stroked3",
- disk4: "stroked4",
- disk5: "stroked5",
- stroked0: "disk0",
- stroked1: "disk1",
- stroked2: "disk2",
- stroked3: "disk3",
- stroked4: "disk4",
- stroked5: "disk5"}
- //To know which tower was selected
- function getTower (id){
- switch(id){
- case 0:
- return towerA;
- case 1:
- return towerB;
- case 2:
- return towerC;
- }
- }
- function selectA (){
- if (!isMoving){
- if(towerA.length>0){
- diskOrigin = 0;
- isMoving = true;
- towerA[towerA.length-1].setTexture(textDict[towerA[towerA.length-1].texture.key]);
- }
- } else{
- if(diskOrigin!=0){
- moveFromTo(diskOrigin, 0);
- } else{
- towerA[towerA.length-1].setTexture(textDict[towerA[towerA.length-1].texture.key]);
- isMoving=false;
- nMoves.text++;
- }
- }
- }
- function selectB (){
- if (!isMoving){
- if(towerB.length>0){
- diskOrigin = 1;
- isMoving = true;
- towerB[towerB.length-1].setTexture(textDict[towerB[towerB.length-1].texture.key]);
- }
- } else{
- if(diskOrigin!=1){
- moveFromTo(diskOrigin, 1);
- } else{
- towerB[towerB.length-1].setTexture(textDict[towerB[towerB.length-1].texture.key]);
- isMoving=false;
- nMoves.text++;
- }
- }
- }
- function selectC (){
- if (!isMoving){
- if(towerC.length>0){
- diskOrigin = 2;
- isMoving = true;
- towerC[towerC.length-1].setTexture(textDict[towerC[towerC.length-1].texture.key]); //levantar o disco
- }
- } else{
- if(diskOrigin!=2){
- moveFromTo(diskOrigin, 2);
- } else{
- towerC[towerC.length-1].setTexture(textDict[towerC[towerC.length-1].texture.key]);
- isMoving=false;
- nMoves.text++;
- }
- }
- }
- function moveFromTo (origin, destiny){
- var originTower = getTower(origin), destinyTower = getTower(destiny);
- var topDiskOrigin = originTower[originTower.length-1], topDiskDestiny;
- if (!undo && !redo) {
- topDiskOrigin.setTexture(textDict[topDiskOrigin.texture.key]);
- }
- if (destinyTower.length>0){
- //movement to a tower with a disk in it
- topDiskDestiny = destinyTower[destinyTower.length-1];
- if(topDiskOrigin.displayWidth<=topDiskDestiny.displayWidth){
- topDiskOrigin.x += 435*(destiny-origin);
- topDiskOrigin.y += 28*(originTower.length-destinyTower.length-1);
- originTower.pop();
- destinyTower.push(topDiskOrigin);
- //control variables
- nMoves.text++;
- totalMoves.push(origin+" "+destiny);
- if (!undo){
- //if it is either a redo or a new move we must add new moves to the current sequence for possible 'undos'
- if(!redo){
- //if it is a new move, we clean all possible "redos"
- moves.splice(nCurrentMoves, moves.length- nCurrentMoves);
- moves.push(origin+" "+destiny);
- }
- nCurrentMoves++;
- undoButton.setInteractive({useHandCursor: true});
- undoButton.setTexture('undo');
- if(nCurrentMoves==moves.length){
- redoButton.disableInteractive();
- redoButton.setTexture('redoDisabled');
- }
- redo=false;
- }else{
- nCurrentMoves--;
- undo=false;
- redoButton.setInteractive({useHandCursor: true});
- redoButton.setTexture('redo');
- if(nCurrentMoves==0){
- undoButton.disableInteractive();
- undoButton.setTexture('undoDisabled');
- }
- }
- }
- }else{
- //move to a tower without a disk in it
- topDiskOrigin.x += 435*(destiny-origin);
- topDiskOrigin.y += 28*(originTower.length-destinyTower.length-1);
- originTower.pop();
- destinyTower.push(topDiskOrigin);
- //control variables
- nMoves.text++;
- totalMoves.push(origin+" "+destiny);
- if (!undo){
- //if it is either a redo or a new move we must add new moves to the current sequence for possible 'undos'
- if(!redo){
- //if it is a new move, we clean all possible "redos" and add the new move to the list
- moves.splice(nCurrentMoves, moves.length- nCurrentMoves);
- moves.push(origin+" "+destiny);
- }
- nCurrentMoves++;
- if(nCurrentMoves==moves.length){
- redoButton.disableInteractive();
- redoButton.setTexture('redoDisabled');
- }
- undoButton.setInteractive({useHandCursor: true});
- undoButton.setTexture('undo');
- redo=false;
- }else{
- nCurrentMoves--;
- undo=false;
- redoButton.setInteractive({useHandCursor: true});
- redoButton.setTexture('redo');
- if(nCurrentMoves==0){
- undoButton.disableInteractive();
- undoButton.setTexture('undoDisabled');
- }
- }
- }
- movements+=origin+" "+destiny+"\n"
- isMoving=false;
- if (towerC.length == diskQntity){
- gameWonMsg.text = "Você terminou de mover a torre!!! Parabéns!!"
- }else{
- gameWonMsg.text ='';
- }
- }
- function redoMove(){
- if (nCurrentMoves < moves.length){
- var res = moves[nCurrentMoves].split(" ");
- redo=true;
- moveFromTo(parseInt(res[0]), parseInt(res[1]));
- if(nCurrentMoves==moves.length) redoButton.setInteractive({useHandCursor: false});
- }
- }
- function undoMove(){
- if(nCurrentMoves>0){
- var res = moves[nCurrentMoves-1].split(" ");
- undo=true;
- moveFromTo(parseInt(res[1]), parseInt(res[0]));
- if(nCurrentMoves==0) undoButton.setInteractive({useHandCursor: false});
- redoButton.setInteractive({useHandCursor: true});
- }
- }
|