Hanoi Tower using Pascal

BY IN IB Computer Science, Turbo Pascal Comments Off on Hanoi Tower using Pascal

uses crt;

var
peg1,peg2,peg3: array[1..4] of string;
top1,top2,top3: byte;
m,n:byte;
t:string;

function pop(stack:byte):string;
begin
case stack of
1:
begin
pop:=peg1[top1];
peg1[top1]:=”;
dec(top1);
end;
2:
begin
pop:=peg2[top2];
peg2[top2]:=”;
dec(top2);
end;
3:
begin
pop:=peg3[top3];
peg3[top3]:=”;
dec(top3);
end;
end;
end;

procedure push(stack:byte;element:string);
begin
case stack of
1:
begin
inc(top1);
peg1[top1]:=element;
end;
2:
begin
inc(top2);
peg2[top2]:=element;
end;
3:
begin
inc(top3);
peg3[top3]:=element;
end;
end;
end;

procedure display;
var
i,y:byte;
begin
clrscr;
i:=1;
for y:=4 downto 1 do
begin
gotoxy(5,y);
if (peg1[i]<>”) then write(peg1[i]);

inc(i);
end;
i:=1;
for y:=4 downto 1 do
begin
gotoxy(15,y);
if (peg2[i]<>”) then write(peg2[i]);
inc(i);
end;
i:=1;
for y:=4 downto 1 do
begin
gotoxy(25,y);
if (peg3[i]<>”) then write(peg3[i]);
inc(i);
end;

end;

begin
clrscr;
top1:=4;
top2:=0;
top3:=0;

peg1[1]:=’*******’;
peg1[2]:=’ ***** ‘;
peg1[3]:=’ *** ‘;
peg1[4]:=’ * ‘;

display;
readln;

t:=pop(1);
push(2,t);
display;
readln;

t:=pop(1);
push(3,t);
display;
readln;

t:=pop(2);
push(3,t);
display;
readln;

t:=pop(1);
push(2,t);
display;
readln;

t:=pop(3);
push(1,t);
display;
readln;

t:=pop(3);
push(2,t);
display;
readln;

t:=pop(1);
push(2,t);
display;
readln;

t:=pop(1);
push(3,t);
display;
readln;

t:=pop(2);
push(3,t);
display;
readln;

t:=pop(2);
push(1,t);
display;
readln;

t:=pop(3);
push(1,t);
display;
readln;

t:=pop(2);
push(3,t);
display;
readln;

t:=pop(1);
push(2,t);
display;
readln;

t:=pop(1);
push(3,t);
display;
readln;

t:=pop(2);
push(3,t);
display;
readln;
end.




Comments are closed.