|
@@ -1,120 +1,31 @@
|
|
|
-<!DOCTYPE html>
|
|
|
-<meta charset="UTF-8">
|
|
|
-<html>
|
|
|
- <head>
|
|
|
- <title> Torre de Hanoi </title>
|
|
|
- <Style>
|
|
|
- #planoDeFundo {
|
|
|
- background :transparent;
|
|
|
- left: 10%;
|
|
|
- top : 10%;
|
|
|
- width: 80%;
|
|
|
- height: 60%;
|
|
|
- position: absolute;
|
|
|
- }
|
|
|
- #fim_button{
|
|
|
- position: absolute;
|
|
|
- border:1px solid gray;
|
|
|
- border-radius: 7%;
|
|
|
- font-size: 0.875;
|
|
|
- word-wrap: break-word;
|
|
|
- max-width: 5%;
|
|
|
- left:10%;
|
|
|
- top: 5%;
|
|
|
- }
|
|
|
- #atualiza_button{
|
|
|
- position:absolute;
|
|
|
- border:1px solid gray;
|
|
|
- border-radius: 7%;
|
|
|
- font-size: 0.875;
|
|
|
- word-wrap: break-word;
|
|
|
- max-width: 7%;
|
|
|
- left: 15%;
|
|
|
- top:5%;
|
|
|
- }
|
|
|
-
|
|
|
- #A_button{
|
|
|
- position:absolute;
|
|
|
- border:1px solid gray;
|
|
|
- border-radius: 7%;
|
|
|
- font-size: 0.875;
|
|
|
- word-wrap: break-word;
|
|
|
- width: 20%;
|
|
|
- max-width: 20%;
|
|
|
- left: 13%;
|
|
|
- top:71%;
|
|
|
- }
|
|
|
- #B_button{
|
|
|
- position:absolute;
|
|
|
- border:1px solid gray;
|
|
|
- border-radius: 7%;
|
|
|
- font-size: 0.875;
|
|
|
- word-wrap: break-word;
|
|
|
- width: 20%;
|
|
|
- max-width: 20%;
|
|
|
- left: 40%;
|
|
|
- top:71%;
|
|
|
- }
|
|
|
- #C_button{
|
|
|
- position:absolute;
|
|
|
- border:1px solid gray;
|
|
|
- border-radius: 7%;
|
|
|
- font-size: 0.875;
|
|
|
- word-wrap: break-word;
|
|
|
- width: 20%;
|
|
|
- max-width: 20%;
|
|
|
- left: 67%;
|
|
|
- top:71%;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- </Style>
|
|
|
- </head>
|
|
|
- <body>
|
|
|
- <button id="fim_button">Fim</button>
|
|
|
- <button id="atualiza_button">Atualiza</button>
|
|
|
- <button id="A_button" onclick="buttonClick(0)">A</button>
|
|
|
- <button id="B_button" onclick="buttonClick(1)">B</button>
|
|
|
- <button id="C_button" onclick="buttonClick(2)">C</button>
|
|
|
- <select name ="Disks" onchange="reinicio()" id="selectDisks">
|
|
|
- <option value = "1">1 Disk</option>
|
|
|
- <option value = "2">2 Disk</option>
|
|
|
- <option value = "3">3 Disk</option>
|
|
|
- <option value = "4">4 Disk</option>
|
|
|
- <option value = "5">5 Disk</option>
|
|
|
- <option value = "6">6 Disk</option>
|
|
|
- <option value = "7">7 Disk</option>
|
|
|
-
|
|
|
- </select>
|
|
|
- <canvas id="planoDeFundo" width="1024" height="768">
|
|
|
- Seu navegador não suporta o recurso Canvas. Atualize-o para utilizar esse recurso
|
|
|
- </canvas>
|
|
|
- <script>
|
|
|
- var canvas = document.getElementById('planoDeFundo');
|
|
|
+var canvas = document.getElementById('planoDeFundo');
|
|
|
var context = canvas.getContext('2d');
|
|
|
var towerA = [], towerB = [], towerC = [];
|
|
|
var toMove = null;
|
|
|
var idF=" ", idT= " ";
|
|
|
- var nMovements = 0, nDisks = 0;
|
|
|
- //NavyBlue Lime Fuchsia lightPink Orange LightSteelBlue Pink
|
|
|
- var colors = ["#000080","#00FF00", "#FF00FF", "#FFC0CB", "#FFA500", "#B0C4DE","#F441AC"];
|
|
|
+ var nMovements = 0, nDisks;
|
|
|
+ var moves="";
|
|
|
+ var acertos;
|
|
|
+ var optimalSolution;
|
|
|
+ //NavyBlue Lime Fuchsia yellow Orange LightSteelBlue red
|
|
|
+ var colors = ["#000080","#00FF00", "#FF00FF", "yellow", "#FFA500", "#B0C4DE","red"];
|
|
|
|
|
|
|
|
|
- function inicio (){
|
|
|
- nDisks = document.getElementById('selectDisks').value;
|
|
|
+ function inicio (QntityDisks){
|
|
|
+ nDisks = QntityDisks;
|
|
|
+ optimalSolution= Math.pow(2,nDisks)-1;
|
|
|
var x = 0;
|
|
|
var y = 558;
|
|
|
var w = 330;
|
|
|
+ moves="";
|
|
|
for (var i = 0; i < nDisks; i++) {
|
|
|
var disk = new Disk(x,y,w,40,colors[i]);
|
|
|
towerA.push(disk);
|
|
|
- x+=25; //moves to the right
|
|
|
+ x+=20; //moves to the right
|
|
|
y-=42; //moves up, +1 to separate disks
|
|
|
- w-=50; //reduces disk width
|
|
|
+ w-=40; //reduces disk width
|
|
|
}
|
|
|
drawScene();
|
|
|
- console.log(y);
|
|
|
|
|
|
}
|
|
|
|
|
@@ -126,9 +37,10 @@
|
|
|
this.h=h;
|
|
|
this.fill=fill;
|
|
|
}
|
|
|
- draw(){
|
|
|
+ draw(){//(ctx, x, y, width, height, radius, fill, stroke)
|
|
|
context.fillStyle = this.fill;
|
|
|
- context.fillRect(this.x, this.y, this.w, this.h);
|
|
|
+ context.strokeStyle = "black";
|
|
|
+ roundRect(context, this.x, this.y, this.w, this.h, 20, 20, true);
|
|
|
}
|
|
|
clear(){
|
|
|
context.clearRect(this.x, this.y, this.w, this.h);
|
|
@@ -150,7 +62,7 @@
|
|
|
toMove=null;
|
|
|
inicio();
|
|
|
}
|
|
|
- function drawTowers(){
|
|
|
+ function drawTowers (){
|
|
|
context.beginPath();
|
|
|
|
|
|
context.fillStyle = 'black';
|
|
@@ -192,14 +104,13 @@
|
|
|
}
|
|
|
|
|
|
function buttonClick (id){
|
|
|
- console.log(id);
|
|
|
if(toMove == null && getTower(id).length>0){
|
|
|
toMove = id;
|
|
|
idF= String.fromCharCode(65+id);
|
|
|
idT= " ";
|
|
|
drawScene();
|
|
|
|
|
|
- } else if(toMove != id){
|
|
|
+ } else if(toMove != null && toMove != id){
|
|
|
idT = String.fromCharCode(65+id);
|
|
|
moveDisk(toMove, id);
|
|
|
toMove=null;
|
|
@@ -220,14 +131,16 @@
|
|
|
function drawFromTo (){
|
|
|
context.font = "50px Arial";
|
|
|
context.fillStyle = "black";
|
|
|
- context.fillText("Move from "+idF+" to ", 50, 50);
|
|
|
+ context.fillText("Move from "+idF+" to "+idT, 50, 50);
|
|
|
context.fillText("Movements: "+nMovements, 50, 100);
|
|
|
}
|
|
|
|
|
|
|
|
|
function moveDisk (origin, destiny){
|
|
|
var originTower = getTower(origin), destinyTower = getTower(destiny), topDiskOrigin, topDiskDestiny;
|
|
|
-
|
|
|
+ moves+="\n"+origin+" "+destiny;
|
|
|
+ console.clear();
|
|
|
+ console.log(moves);
|
|
|
|
|
|
topDiskOrigin = originTower[originTower.length-1];
|
|
|
if(destinyTower.length>0){
|
|
@@ -253,15 +166,40 @@
|
|
|
nMovements++;
|
|
|
drawScene();
|
|
|
}
|
|
|
+ acertos=0;
|
|
|
if(towerC.length == nDisks && nDisks!=0){
|
|
|
- alert("You Won!!!\nCongratulations!!");
|
|
|
+ acertos=1;
|
|
|
+ if(nMovements==optimalSolution){
|
|
|
+ alert("You Won in the least amount of movements!!!\nCongratulations!!!");
|
|
|
+ }else{
|
|
|
+ alert("You Won!!!\nCongratulations!!");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- </script>
|
|
|
-
|
|
|
- <script> inicio(); </script>
|
|
|
-
|
|
|
- </body>
|
|
|
-</html>
|
|
|
+ function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
|
|
|
+ if (typeof stroke == "undefined" ) {
|
|
|
+ stroke = true;
|
|
|
+ }
|
|
|
+ if (typeof radius === "undefined") {
|
|
|
+ radius = 5;
|
|
|
+ }
|
|
|
+ ctx.beginPath();
|
|
|
+ ctx.moveTo(x + radius, y);
|
|
|
+ ctx.lineTo(x + width - radius, y);
|
|
|
+ ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
|
|
|
+ ctx.lineTo(x + width, y + height - radius);
|
|
|
+ ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
|
|
|
+ ctx.lineTo(x + radius, y + height);
|
|
|
+ ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
|
|
|
+ ctx.lineTo(x, y + radius);
|
|
|
+ ctx.quadraticCurveTo(x, y, x + radius, y);
|
|
|
+ ctx.closePath();
|
|
|
+ if (stroke) {
|
|
|
+ ctx.stroke();
|
|
|
+ }
|
|
|
+ if (fill) {
|
|
|
+ ctx.fill();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ inicio();
|