<< home >>
Algoritmizácia problémovej úlohy bludisko
<<- ->>


  • Pascal

BLUDISKO - realizácia Pascal


bludisko_pascal

bludisko_pascal


program bludisko;

uses crt;

const stena :
char = 'X'; { 1 v matici }
stopa : char = '.';
robot :
char = 'O';
ciel :
char = 'I'; { 9 v matici }

MAX = 20; { rozmer stvorcovej matice }

{ !!! matica je transponovana tak pozor na to !!! }

plocha : array[1..MAX, 1..MAX] of byte = (

(
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
(
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1),
(
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1),
(
1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1),
(
1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1),
(
1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1),
(
1,0,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1),
(
1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1),
(
1,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1),
(
1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
(
1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1),
(
1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1),
(
1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,0,0,0,0,1),
(
1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,1,1),
(
1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1),
(
1,0,1,1,1,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1),
(
1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1),
(
1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1),
(
1,0,1,0,0,0,9,0,0,0,0,1,0,0,0,0,0,0,0,1),
(
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

);

var key :
char;
x, y, x_c, y_c,
smer :
byte; { 1-sever, 2-vychod, 3-juh, 4-zapad }


procedure vykresli_plochu;
var x_i, y_i :
byte;
begin
clrscr;
textcolor(Red);
for y_i:=
1 to MAX do
for x_i:=
1 to MAX do begin
if plocha[x_i, y_i]=
1 then begin
gotoxy(x_i, y_i);
write(stena);
end;
if plocha[x_i, y_i]=
9 then begin
textcolor(LightBlue);
gotoxy(x_i, y_i);
write(ciel);
x_c:=x_i;
y_c:=y_i;
textcolor(Red);
end;
end;
textcolor(Green);
gotoxy(MAX+
5, 1);
write('BLUDISKO - algoritmus pravej ruky');
gotoxy(MAX+
12, 3);
write('q - quit');
textcolor(LightGreen);
gotoxy(
2, MAX-1);
write(robot);
gotoxy(
2, MAX-1);
end;

procedure krok(x_old, y_old, x_new, y_new :
byte);
begin
if (x_old<>x_c) or (y_old<>y_c) then begin
gotoxy(x_old, y_old);
write(stopa);
end;
gotoxy(x_new, y_new);
write(robot);
gotoxy(x_new, y_new);
{ nach kurzor netrci kade tade }
x:=x_new;
y:=y_new;
if plocha[x, y]=
9 then begin
textcolor(LightBlue);
write('W');
gotoxy(MAX+
12, 5);
write('c - continue');
gotoxy(x, y);
key:=' ';
while key<>'c' do begin
key:=readkey;
end;
gotoxy(MAX+
12, 5);
clreol;
gotoxy(x, y);
write(ciel);
textcolor(LightGreen);
end;
end;

begin
vykresli_plochu;
x:=
2;
y:=MAX-
1;
smer:=
1;
key:=' ';
while key<>'q' do begin
delay(
30000); { nuz 30 sekund to nebude ale co uz }
if keypressed then key:=readkey;
case smer of
1 : if plocha[x+1, y]<>1 then begin
krok(x, y, x+
1, y);
smer:=
2
end
else begin
if plocha[x, y-
1]<>1 then begin
krok(x, y, x, y-
1);
smer:=
1
end
else smer:=
4;
end;
2 : if plocha[x, y+1]<>1 then begin
krok(x, y, x, y+
1);
smer:=
3
end
else begin
if plocha[x+
1, y]<>1 then begin
krok(x, y, x+
1, y);
smer:=
2
end
else smer:=
1;
end;
3 : if plocha[x-1, y]<>1 then begin
krok(x, y, x-
1, y);
smer:=
4
end
else begin
if plocha[x, y+
1]<>1 then begin
krok(x, y, x, y+
1);
smer:=
3
end
else smer:=
2;
end;
4 : if plocha[x, y-1]<>1 then begin
krok(x, y, x, y-
1);
smer:=
1
end
else begin
if plocha[x-
1, y]<>1 then begin
krok(x, y, x-
1, y);
smer:=
4
end
else smer:=
3;
end;
end;
end;
gotoxy(
1, MAX+3);
textcolor(LightGray);
normvideo;
end.

<<- ->>