USES CRT;
CONST
N=10;
VAR
Xm,Ym,Xb,Yb,Xf,Yf,i,j:Byte;
Ch:Char;
S:String;
b,b1,f:Boolean;
PROCEDURE Test_Box(Xt,Yt:Byte; var bt:boolean);
Begin
bt:=false;
If (Xt=Xb) and (Yt=Yb) then
begin
bt:=true;
gotoXY(1,N*2+2);
Write('Monkey found the box!');
gotoXY(Xt,Yt);
S:='MB';
end;
End;
PROCEDURE Test_Fruit(Xt,Yt:Byte; var bf:boolean);
Begin
bf:=false;
If (Xt=Xf) and (Yt=Yf) then
begin
bf:=true;
gotoXY(1,N*2+3);
Writeln('Monkey got its fruit!');
Write('Press "Enter" to exit');
gotoXY(Xt,Yt);
S:='MBF';
end;
End;
BEGIN
Randomize;
ClrScr;
For i:=1 to N+1 do
begin
For j:=1 to N do Write('|---');
Writeln('|');
If i<=N then
begin
For j:=1 to N do Write('| ');
Writeln('|');
end;
end;
Xb:=Random(N)*4+2;
Yb:=Random(N)*2+2;
Repeat
Xf:=Random(N)*4+2;
Yf:=Random(N)*2+2;
Until Not((Xf=Xb) and (Yf=Yb));
Repeat
Xm:=Random(N)*4+2;
Ym:=Random(N)*2+2;
Until Not((Xm=Xb) and (Ym=Yb)) and Not((Xm=Xf) and (Ym=Yf));
GotoXY(Xm,Ym);
Write('M');
b:=false;
f:=false;
S:='M';
Repeat
Ch:=ReadKey;
If (Ch=#72) and (Ym>2) then
begin
GotoXY(Xm,Ym);
Write(' ');
Ym:=Ym-2;
GotoXY(Xm,Ym);
If not(b) then
begin
Test_Box(Xm,Ym,b1);
If b1 then b:=true;
end;
If b then
begin
Test_Fruit(Xm,Ym,b1);
If b1 then f:=true;
end;
Write(S);
end;
If (Ch=#75) and (Xm>2) then
begin
GotoXY(Xm,Ym);
Write(' ');
Xm:=Xm-4;
GotoXY(Xm,Ym);
If not(b) then
begin
Test_Box(Xm,Ym,b1);
If b1 then b:=true;
end;
If b then
begin
Test_Fruit(Xm,Ym,b1);
If b1 then f:=true;
end;
Write(S);
end;
If (Ch=#77) and (Xm<4*N-2) then
begin
GotoXY(Xm,Ym);
Write(' ');
Xm:=Xm+4;
GotoXY(Xm,Ym);
If not(b) then
begin
Test_Box(Xm,Ym,b1);
If b1 then b:=true;
end;
If b then
begin
Test_Fruit(Xm,Ym,b1);
If b1 then f:=true;
end;
Write(S);
end;
If (Ch=#80) and (Ym<2*N) then
begin
GotoXY(Xm,Ym);
Write(' ');
Ym:=Ym+2;
GotoXY(Xm,Ym);
If not(b) then
begin
Test_Box(Xm,Ym,b1);
If b1 then b:=true;
end;
If b then
begin
Test_Fruit(Xm,Ym,b1);
If b1 then f:=true;
end;
Write(S);
end;
If Ch=#27 then Halt(0);
Until f;
Readln;
END.