Torres de Hanoi en Pascal
Hola gente que tal?
amigos necesito su ayuda, estoy estudiando programacion y tengo que presentar un proyecto de Las Torres de Hanoi usando Pascal.
El proyecto debe seguir las sigtes reglas:
1-Mover los discos al 3er palo
2-Mover los discos de uno en uno
3-Mover los discos de un palo en uno (sin saltar ninguno)
4-Ningun disco grande estará encima de uno pequeño.
La verdad no tengo ni idea de como empezar
Alguien puede ayudarme?
amigos necesito su ayuda, estoy estudiando programacion y tengo que presentar un proyecto de Las Torres de Hanoi usando Pascal.
El proyecto debe seguir las sigtes reglas:
1-Mover los discos al 3er palo
2-Mover los discos de uno en uno
3-Mover los discos de un palo en uno (sin saltar ninguno)
4-Ningun disco grande estará encima de uno pequeño.
La verdad no tengo ni idea de como empezar
Alguien puede ayudarme?
Encontre, por casualidad, Hanoi en pascal.
Hace mucho tiempo que no programo en pascal y no tengo el compilador instalado, asi que no pude probarlo, pero creo que funciona bien.
Exito ! ! !
Hace mucho tiempo que no programo en pascal y no tengo el compilador instalado, asi que no pude probarlo, pero creo que funciona bien.
Exito ! ! !
program HanoiTowersA; {------------------------------------------------}
{ }
{ . }
{ source a : }
{ auxiliar b : }
{ target c : }
{ 1997 16 , - vii }
{------------------------------------------------}
uses fdelay,crt;
const
MAX = 12; { }
EMPTY = 0; { }
color : array[1..MAX] of byte = (LIGHTCYAN, LIGHTRED, LIGHTBLUE,
LIGHTGREEN, LIGHTMAGENTA,
CYAN, RED, BLUE, GREEN, MAGENTA,
LIGHTCYAN, LIGHTRED);
var
n : byte; { }
num : integer; { .... }
a : array['a'..'c', 1..MAX] of byte; { }
p : array['a'..'c'] of byte; { }
procedure InitArrays; {-------------------------}
{ }
var { . }
i : char; { } {-------------------------}
j : byte; { }
begin (* INIT ARRAYS *)
num := 1; { }
for j := 1 to n do
begin
a['a',j] := n-j+1; { }
a['b',j] := EMPTY; { }
a['c',j] := EMPTY; { }
end;
p['a'] := n; { }
p['b'] := 0; { }
p['c'] := 0; { }
end; (* INIT ARRAYS *)
procedure Show; {-------------------------}
{ }
var { . }
i : char; { } {-------------------------}
j : byte; { }
begin (* SHOW *)
for i := 'a' to 'c' do
for j := 1 to n do
begin
GotoXY((ord(i)-ord('a'))*15+22, 18-j);
if a[i,j] = EMPTY then
Write(' .')
else
Write(a[i,j]:2);
end (* END LOOPS *)
end; (* SHOW *)
procedure InitShow; {-------------------------}
{ }
begin (* INIT SHOW *) { . }
ClrScr; {-------------------------}
GotoXY(9,1);
Write('Hanoi Towers: ');
GotoXY(22,20);
Write('[a] [b] [c]');
end; (* INIT SHOW *)
function GetNum : byte; {-------------------------}
{ }
var {. }
n : byte; {-------------------------}
begin (* GET NUM *)
GotoXY(9,3);
Write('number of disks = ');
Readln(n);
GetNum := n;
end; (* GET NUM *)
procedure Move(n : byte; source, target : char); {-------------------------}
{ }
begin (* MOVE *) {-------------------------}
GotoXY(5,24);
Write(num:5,' : move disk ', n, ' from ', source, ' to ', target, ' ');
inc(num);
ReadKey;
inc(p[target]);
a[target,p[target]] := a[source,p[source]];
a[source,p[source]] := EMPTY;
dec(p[source]);
Show;
end; (* MOVE *)
{-------------------------}
{ }
{ }
{-------------------------}
procedure Towers(n : byte; { }
source,auxiliar,target : char); { }
begin (* TOWERS *)
if n = 1 then
Move(n,source,target) { n - }
else
begin
Towers(n-1,source,target,auxiliar); { n-1 }
Move(n,source,target); { n }
Towers(n-1,auxiliar,source,target); { n-1 }
end
end; (* TOWERS *)
begin (* MAIN *)
InitShow; { }
repeat
n := GetNum { }
until n <= MAX;
InitArrays; { }
Show; { }
Towers(n,'a','b','c'); { }
ReadKey;
end. (* MAIN *)
program HanoiX; {------------------------------------------------}
{ . }
{ source a : }
{ auxiliar b : }
{ target c : }
{ 1999 20 , - vii }
{------------------------------------------------}
uses fdelay,crt,Graph;
const
MAX = 12; { }
a = 1; { a }
b = 2; { b }
c = 3; { c }
d = 9; { }
e = 3; { }
h = 30; { }
dd = 5; { }
yo = 650; { }
top = 180; { }
BkColor = 140; { }
TextCol = 52; { }
ESCAPE = #27;
type
columns = a..c;
rows = 1..MAX;
Vector = array [rows] of byte;
Towers = record { 3 }
disk : Vector; { }
p : byte; { }
x : integer; { - }
end;
var
n : byte; { }
times : byte; { }
num : integer; { num }
t : array[columns] of Towers; { }
MaxX,MaxY : integer; { }
color : Vector; { }
procedure GraphInit; {---------------------------------}
{ 1024 x 768 : }
var { . 256 }
driver,mode,code : integer; {---------------------------------}
path : string[30];
begin (*GRAPH INIT*)
driver := InstallUserDriver('vesa',nil);
mode := 4;
path := 'd:bpbgi';
InitGraph(driver,mode,path);
code := GraphResult;
while code <> grOK do
begin { }
WriteLn('path defined as: ',path); { bgi- }
Write('enter new path name: ');
ReadLn(path);
driver := InstallUserDriver('vesa',nil);
mode := 4;
InitGraph(driver,mode,path);
code := GraphResult;
end;
MaxX := GetMaxX;
MaxY := GetMaxY;
t[b].x := Round(MaxX/(2*dd))*dd;
t[a].x := Round(MaxX/(5*dd))*dd;
t[c].x := Round((MaxX-t[a].x)/dd)*dd;
end; (*GRAPH INIT*)
procedure EndOfJob(fin : boolean); {---------------------------------}
{ }
begin (* END OF JOB *) {---------------------------------}
SetTextStyle(10,0,5);
SetColor(TextCol);
if fin then
OutTextXY(30,100,'end of game - good bye ! ! !')
else
OutTextXY(30,100,'user interruption - good bye !');
ReadKey;
CloseGraph
end; (* END OF JOB *)
function GetNum : byte; {---------------------------------}
{ }
var {---------------------------------}
n : byte;
a : char;
x : integer;
begin (* GET NUM *)
SetFillStyle(1,BkColor);
SetColor(TextCol);
x := 600;
OutTextXY(x,10,'number of disks : ');
repeat
n := 0;
x := 600 + TextWidth('number of disks : ');
bar(x,20,MaxX,60);
a := ReadKey;
While a <> #13 do
begin
OutTextXY(x,10,a);
x := x + TextWidth(a);
n := n*10 + ord(a) - ord('0');
a := ReadKey;
end; (* WHILE a <> enter *)
until n in [1..12];
GetNum := n;
end; (* GET NUM *)
procedure ShowNumber; {---------------------------------}
{ }
const {---------------------------------}
x = 400;
var
s : string;
begin (* SHOW NUMBER *)
inc(num);
str(num,s);
SetFillStyle(1,130);
Bar(x-30,20,x+TextWidth(s)+30,70);
SetColor(White);
OutTextXY(x,15,s);
SetColor(Black);
end; (* SHOW NUMBER *)
procedure Initialize; {---------------------------------}
{ }
var {---------------------------------}
i : columns; { }
j : rows; { }
begin (* INITIALIZE *)
num := -1; { }
times := 5;
with t[a] do { }
begin
p := n;
for j := 1 to n do
disk[j] := n-j+1;
end;
for i := b to c do { }
with t[i] do
p := 0;
for j := 1 to MAX do
color[j] := j + 40;
end; (* INITIALIZE *)
procedure GetUserChoice; {---------------------------------}
{ . }
var {---------------------------------}
choice : char;
begin (* GET USER CHOICE *)
if KeyPressed then
begin
choice := ReadKey;
case choice of
ESCAPE : begin
EndOfJob(FALSE);
halt(0);
end;
'-' : if times < 254 then
inc(times);
'+' : if times > 0 then
dec(times);
end (* CASE *)
end (* IF KEYPRESSED *)
end; (* GET USER CHOICE *)
procedure ShowDisk(x,y : integer; disk : byte; show : boolean);
begin (* SHOW DISK *) {---------------------------------}
GetUserChoice; { . }
if show then {---------------------------------}
begin
SetColor(Black);
SetFillStyle(1,color[disk])
end
else
begin
SetColor(BkColor);
SetFillStyle(1,BkColor);
end; (* IF SHOW *)
bar(x-(disk+2)*d,y-e,x-d-e,y-h+e);
bar(x+(disk+2)*d,y-e,x+d+e,y-h+e);
rectangle(x-(disk+2)*d,y-e,x-d-e,y-h+e);
rectangle(x+(disk+2)*d,y-e,x+d+e,y-h+e);
if show then
delay(times);
end; (* SHOW DISK *)
procedure FirstShow; {---------------------------------}
{ . }
var {---------------------------------}
j : rows; { }
y : integer; { a }
begin (* FIRST SHOUW *)
y := yo;
SetColor(Black);
with t[a] do
begin
for j := 1 to n do
begin
ShowDisk(x,y,disk[j],TRUE);
y := y - h;
end
end;
delay(500);
end; (* FIRST SHOW *)
procedure InitScreen; {---------------------------------}
{ . }
var {---------------------------------}
i : columns;
y : integer;
s : string[3];
begin (* INIT SCREEN *)
SetFillStyle(1,BkColor);
bar(0,0,MaxX,MaxX);
SetLineStyle(0,0,3);
SetTextStyle(10,0,3);
SetColor(TextCol);
GotoXY(9,1);
OutTextXY(30,10,'Hanoi Towers');
y := yo + e;
SetColor(Black);
for i := a to c do
with t[i] do
begin
SetFillStyle(1,53);
bar(x-d,y,x+d,y-Max*h-h);
SetFillStyle(1,54);
bar(x-15*d,y,x+15*d,y+2*d);
rectangle(x-d,y,x+d,y-Max*h-h);
rectangle(x-15*d,y,x+15*d,y+2*d);
SetColor(TextCol);
s := '(' + char(ord('a')+i-1) + ')';
OutTextXY(x-TextWidth(s) div 2,yo + 35,s);
SetColor(Black);
end; (* FOR I *)
SetFillStyle(1,32);
bar(30,y+2*d,MaxX-30,y+4*d);
rectangle(30,y+2*d,MaxX-30,y+4*d);
end; (* INIT SCREEN *)
procedure MoveUp(xx,yy,disk : integer);
begin {---------------------------------}
while yy > top do { }
begin {---------------------------------}
ShowDisk(xx,yy,disk,FALSE);
dec(yy,dd);
ShowDisk(xx,yy,disk,TRUE);
end
end;
procedure MoveRight(xx,limit,disk : integer);
begin {---------------------------------}
while xx < limit do { }
begin {---------------------------------}
ShowDisk(xx,top,disk,FALSE);
inc(xx,dd);
ShowDisk(xx,top,disk,TRUE);
end
end;
procedure MoveLeft(xx,limit,disk : integer);
begin {---------------------------------}
while xx > limit do { }
begin {---------------------------------}
ShowDisk(xx,top,disk,FALSE);
dec(xx,dd);
ShowDisk(xx,top,disk,TRUE);
end
end;
procedure MoveDown(xx,bottom,disk : integer);
var {---------------------------------}
yy : integer; { }
{---------------------------------}
begin
yy := top;
while yy < bottom do
begin
ShowDisk(xx,yy,disk,FALSE);
inc(yy,dd);
ShowDisk(xx,yy,disk,TRUE);
end
end;
procedure Move(n : byte; source, target : columns);
{---------------------------------}
begin (* MOVE *) { }
ShowNumber; {---------------------------------}
inc(t[target].p);
with t[source] do
begin
MoveUp(x,yo-p*h+h,disk[p]);
if t[target].x > x then
MoveRight(x,t[target].x,disk[p])
else
MoveLeft(x,t[target].x,disk[p]);
MoveDown(t[target].x,yo-t[target].p*h+h,disk[p]);
end;
t[target].disk[t[target].p] := t[source].disk[t[source].p];
dec(t[source].p);
end; (* MOVE *)
{---------------------------------}
{ - }
{---------------------------------}
procedure HanoiTowers(n : byte; { }
source,auxiliar,target : columns);{ }
begin (* HANOI TOWERS *)
if n = 1 then
Move(n,source,target) { n - }
else
begin { n-1 }
HanoiTowers(n-1,source,target,auxiliar);
Move(n,source,target); { n }
HanoiTowers(n-1,auxiliar,source,target);
end { n-1 }
end; (* HANOI TOWERS *)
begin (* MAIN *)
GraphInit;
InitScreen; { }
n := GetNum; { }
Initialize; { }
ShowNumber; { - }
FirstShow; { }
HanoiTowers(n,a,b,c); { }
EndOfJob(TRUE)
end. (* MAIN *)
{--------------------------- end of list --------------------------------}
{ . }
{ source a : }
{ auxiliar b : }
{ target c : }
{ 1999 20 , - vii }
{------------------------------------------------}
uses fdelay,crt,Graph;
const
MAX = 12; { }
a = 1; { a }
b = 2; { b }
c = 3; { c }
d = 9; { }
e = 3; { }
h = 30; { }
dd = 5; { }
yo = 650; { }
top = 180; { }
BkColor = 140; { }
TextCol = 52; { }
ESCAPE = #27;
type
columns = a..c;
rows = 1..MAX;
Vector = array [rows] of byte;
Towers = record { 3 }
disk : Vector; { }
p : byte; { }
x : integer; { - }
end;
var
n : byte; { }
times : byte; { }
num : integer; { num }
t : array[columns] of Towers; { }
MaxX,MaxY : integer; { }
color : Vector; { }
procedure GraphInit; {---------------------------------}
{ 1024 x 768 : }
var { . 256 }
driver,mode,code : integer; {---------------------------------}
path : string[30];
begin (*GRAPH INIT*)
driver := InstallUserDriver('vesa',nil);
mode := 4;
path := 'd:bpbgi';
InitGraph(driver,mode,path);
code := GraphResult;
while code <> grOK do
begin { }
WriteLn('path defined as: ',path); { bgi- }
Write('enter new path name: ');
ReadLn(path);
driver := InstallUserDriver('vesa',nil);
mode := 4;
InitGraph(driver,mode,path);
code := GraphResult;
end;
MaxX := GetMaxX;
MaxY := GetMaxY;
t[b].x := Round(MaxX/(2*dd))*dd;
t[a].x := Round(MaxX/(5*dd))*dd;
t[c].x := Round((MaxX-t[a].x)/dd)*dd;
end; (*GRAPH INIT*)
procedure EndOfJob(fin : boolean); {---------------------------------}
{ }
begin (* END OF JOB *) {---------------------------------}
SetTextStyle(10,0,5);
SetColor(TextCol);
if fin then
OutTextXY(30,100,'end of game - good bye ! ! !')
else
OutTextXY(30,100,'user interruption - good bye !');
ReadKey;
CloseGraph
end; (* END OF JOB *)
function GetNum : byte; {---------------------------------}
{ }
var {---------------------------------}
n : byte;
a : char;
x : integer;
begin (* GET NUM *)
SetFillStyle(1,BkColor);
SetColor(TextCol);
x := 600;
OutTextXY(x,10,'number of disks : ');
repeat
n := 0;
x := 600 + TextWidth('number of disks : ');
bar(x,20,MaxX,60);
a := ReadKey;
While a <> #13 do
begin
OutTextXY(x,10,a);
x := x + TextWidth(a);
n := n*10 + ord(a) - ord('0');
a := ReadKey;
end; (* WHILE a <> enter *)
until n in [1..12];
GetNum := n;
end; (* GET NUM *)
procedure ShowNumber; {---------------------------------}
{ }
const {---------------------------------}
x = 400;
var
s : string;
begin (* SHOW NUMBER *)
inc(num);
str(num,s);
SetFillStyle(1,130);
Bar(x-30,20,x+TextWidth(s)+30,70);
SetColor(White);
OutTextXY(x,15,s);
SetColor(Black);
end; (* SHOW NUMBER *)
procedure Initialize; {---------------------------------}
{ }
var {---------------------------------}
i : columns; { }
j : rows; { }
begin (* INITIALIZE *)
num := -1; { }
times := 5;
with t[a] do { }
begin
p := n;
for j := 1 to n do
disk[j] := n-j+1;
end;
for i := b to c do { }
with t[i] do
p := 0;
for j := 1 to MAX do
color[j] := j + 40;
end; (* INITIALIZE *)
procedure GetUserChoice; {---------------------------------}
{ . }
var {---------------------------------}
choice : char;
begin (* GET USER CHOICE *)
if KeyPressed then
begin
choice := ReadKey;
case choice of
ESCAPE : begin
EndOfJob(FALSE);
halt(0);
end;
'-' : if times < 254 then
inc(times);
'+' : if times > 0 then
dec(times);
end (* CASE *)
end (* IF KEYPRESSED *)
end; (* GET USER CHOICE *)
procedure ShowDisk(x,y : integer; disk : byte; show : boolean);
begin (* SHOW DISK *) {---------------------------------}
GetUserChoice; { . }
if show then {---------------------------------}
begin
SetColor(Black);
SetFillStyle(1,color[disk])
end
else
begin
SetColor(BkColor);
SetFillStyle(1,BkColor);
end; (* IF SHOW *)
bar(x-(disk+2)*d,y-e,x-d-e,y-h+e);
bar(x+(disk+2)*d,y-e,x+d+e,y-h+e);
rectangle(x-(disk+2)*d,y-e,x-d-e,y-h+e);
rectangle(x+(disk+2)*d,y-e,x+d+e,y-h+e);
if show then
delay(times);
end; (* SHOW DISK *)
procedure FirstShow; {---------------------------------}
{ . }
var {---------------------------------}
j : rows; { }
y : integer; { a }
begin (* FIRST SHOUW *)
y := yo;
SetColor(Black);
with t[a] do
begin
for j := 1 to n do
begin
ShowDisk(x,y,disk[j],TRUE);
y := y - h;
end
end;
delay(500);
end; (* FIRST SHOW *)
procedure InitScreen; {---------------------------------}
{ . }
var {---------------------------------}
i : columns;
y : integer;
s : string[3];
begin (* INIT SCREEN *)
SetFillStyle(1,BkColor);
bar(0,0,MaxX,MaxX);
SetLineStyle(0,0,3);
SetTextStyle(10,0,3);
SetColor(TextCol);
GotoXY(9,1);
OutTextXY(30,10,'Hanoi Towers');
y := yo + e;
SetColor(Black);
for i := a to c do
with t[i] do
begin
SetFillStyle(1,53);
bar(x-d,y,x+d,y-Max*h-h);
SetFillStyle(1,54);
bar(x-15*d,y,x+15*d,y+2*d);
rectangle(x-d,y,x+d,y-Max*h-h);
rectangle(x-15*d,y,x+15*d,y+2*d);
SetColor(TextCol);
s := '(' + char(ord('a')+i-1) + ')';
OutTextXY(x-TextWidth(s) div 2,yo + 35,s);
SetColor(Black);
end; (* FOR I *)
SetFillStyle(1,32);
bar(30,y+2*d,MaxX-30,y+4*d);
rectangle(30,y+2*d,MaxX-30,y+4*d);
end; (* INIT SCREEN *)
procedure MoveUp(xx,yy,disk : integer);
begin {---------------------------------}
while yy > top do { }
begin {---------------------------------}
ShowDisk(xx,yy,disk,FALSE);
dec(yy,dd);
ShowDisk(xx,yy,disk,TRUE);
end
end;
procedure MoveRight(xx,limit,disk : integer);
begin {---------------------------------}
while xx < limit do { }
begin {---------------------------------}
ShowDisk(xx,top,disk,FALSE);
inc(xx,dd);
ShowDisk(xx,top,disk,TRUE);
end
end;
procedure MoveLeft(xx,limit,disk : integer);
begin {---------------------------------}
while xx > limit do { }
begin {---------------------------------}
ShowDisk(xx,top,disk,FALSE);
dec(xx,dd);
ShowDisk(xx,top,disk,TRUE);
end
end;
procedure MoveDown(xx,bottom,disk : integer);
var {---------------------------------}
yy : integer; { }
{---------------------------------}
begin
yy := top;
while yy < bottom do
begin
ShowDisk(xx,yy,disk,FALSE);
inc(yy,dd);
ShowDisk(xx,yy,disk,TRUE);
end
end;
procedure Move(n : byte; source, target : columns);
{---------------------------------}
begin (* MOVE *) { }
ShowNumber; {---------------------------------}
inc(t[target].p);
with t[source] do
begin
MoveUp(x,yo-p*h+h,disk[p]);
if t[target].x > x then
MoveRight(x,t[target].x,disk[p])
else
MoveLeft(x,t[target].x,disk[p]);
MoveDown(t[target].x,yo-t[target].p*h+h,disk[p]);
end;
t[target].disk[t[target].p] := t[source].disk[t[source].p];
dec(t[source].p);
end; (* MOVE *)
{---------------------------------}
{ - }
{---------------------------------}
procedure HanoiTowers(n : byte; { }
source,auxiliar,target : columns);{ }
begin (* HANOI TOWERS *)
if n = 1 then
Move(n,source,target) { n - }
else
begin { n-1 }
HanoiTowers(n-1,source,target,auxiliar);
Move(n,source,target); { n }
HanoiTowers(n-1,auxiliar,source,target);
end { n-1 }
end; (* HANOI TOWERS *)
begin (* MAIN *)
GraphInit;
InitScreen; { }
n := GetNum; { }
Initialize; { }
ShowNumber; { - }
FirstShow; { }
HanoiTowers(n,a,b,c); { }
EndOfJob(TRUE)
end. (* MAIN *)
{--------------------------- end of list --------------------------------}
program HanoiTowers; (* solucion recursiva *)
uses fdelay,crt;
var
n : byte;
num : integer;
procedure Move(n : byte; source, target : char);
begin (* MOVE *)
WriteLn(num:5,' : move disk ', n, ' from ', source, ' to ', target);
inc(num);
end; (* MOVE *)
procedure Towers(n : byte; source,auxiliar,target : char);
begin (* TOWERS *)
if n = 1 then
Move(n,source,target)
else
begin
Towers(n-1,source,target,auxiliar);
Move(n,source,target);
Towers(n-1,auxiliar,source,target);
end
end; (* TOWERS *)
begin (* MAIN *)
ClrScr;
num := 1;
Write('number of disks = ');
Readln(n);
WriteLn;
Towers(n,'a','b','c');
ReadLn;
end. (* MAIN *)
uses fdelay,crt;
var
n : byte;
num : integer;
procedure Move(n : byte; source, target : char);
begin (* MOVE *)
WriteLn(num:5,' : move disk ', n, ' from ', source, ' to ', target);
inc(num);
end; (* MOVE *)
procedure Towers(n : byte; source,auxiliar,target : char);
begin (* TOWERS *)
if n = 1 then
Move(n,source,target)
else
begin
Towers(n-1,source,target,auxiliar);
Move(n,source,target);
Towers(n-1,auxiliar,source,target);
end
end; (* TOWERS *)
begin (* MAIN *)
ClrScr;
num := 1;
Write('number of disks = ');
Readln(n);
WriteLn;
Towers(n,'a','b','c');
ReadLn;
end. (* MAIN *)
