/
HANOI.CPP
125 lines (98 loc) · 2.22 KB
/
HANOI.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//algoritmo Torre de hanoi
//arquivos de cabecalho
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
int pilha[3][10];
int altura[3] = {0,0,0};
int disco[10] = {1,2,3,4,5,6,7,8,9,10};
// coloca o disco na torre
void empilha(int x, int torre)
{
pilha[torre][altura[torre]] = x;
altura[torre]++;
}
// remove o disco da torre
int desempilha(int torre)
{
altura[torre]--;
return pilha[torre][altura[torre]];
}
/* parte grafica */
//inicia o grafico
int inicia_grafico()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\borlandc\\bgi");
return graphresult();
}
//retira o disco graficamente
void apaga_disco(int tam,int torre)
{
int x, y;
y = (getmaxy() / 2) - ((altura[torre] + 1) * 10);
x = (getmaxx() / 2) + ((torre - 1) * 100);
setcolor(BLACK);
tam = 10 - tam;
tam *= 5;
line(x - tam, y,x - tam,y + 9);
line(x - tam, y,x + tam,y);
line(x + tam, y,x + tam,y + 9);
}
//coloca o disco graficamente
void desenha_disco(int tam,int torre)
{
int x, y;
x = (getmaxx() / 2) + ((torre - 1) * 100);
y = (getmaxy() / 2) - ((altura[torre]) * 10);
tam = 10 - tam;
tam *= 5;
setcolor(WHITE);
rectangle(x - tam,y,x + tam,y+10);
}
//algoritmo de movimentacao de disco
void movediscos(int n, int torreI, int torreF)
{
//variaveis locais
int torreAux = 3 - (torreI + torreF);
int tam;
//se existe apenas um disco a ser movido
if(n == 1)
{
tam = desempilha(torreI);
empilha(tam,torreF);
apaga_disco(tam,torreI);
desenha_disco(tam,torreF);
delay(500);
}
else
{
movediscos(n-1,torreI, torreAux);
movediscos(1, torreI, torreF);
movediscos(n-1,torreAux,torreF);
}
}
void main()
{
int i, y, tam_torre;
cout << "\nEntre com o tamanho da torre: ";
cin >> tam_torre;
if(tam_torre > 10)
{
cout << "\n O Programa aceita no maximo 10 discos";
exit(1);
}
inicia_grafico();
line(1,getmaxy() / 2 ,getmaxx(),getmaxy() / 2);
for(i=0;i<tam_torre;i++)
{
altura[0]++;
pilha[0][i] = disco[i];
desenha_disco(i + 1,0);
}
movediscos(tam_torre,0,1);
getch();
}