Witam, otóż tworze sobie dla własnej satysfakcji [i też próby nauczenia się pascala] grę, w której wszystkie polecenia [na razie] będą odbywać się za pomocą wpisywania komend. Wymyśliłem sobie że mapy po których gracz będzie się poruszał będą w postaci pliku który jest wczytywany do tablicy, następnie gracz będzie mógł ruszyć się tam gdzie wartość danego elementu jest różny od 1 [1 = ściany]. Zrobiłem program wczytujący mape, system sprawdzający gdzie może się ruszyć gracz. I teraz pojawia się problem:
Jak przypisać pozycje startową [tu indeksy danego elementu np 4,3], tj dokładnie chodzi mi o to że po znalezieniu przez program wartości 2 w tablicy [miejsce startu], nie umiem zmusić go żeby przypisał indeksy tej wartości do pozycji startowej, gdzie potem program by dalej sprawdzał gdzie mogę iść, i ew potem po moim poleceniu ruszył się tam.
kod programu:
kod pomiedzy {-----} to moja próba rozwiązania problemu, ale nie działa i nie ma sensu wg mnie:p
USES CRT,DOS,GRAPH;
const N=12; {liczba kolumn i wierszy w tablicy}
var
curposx,curposy:integer;
c2:char;
tab:array[1..N,1..N] of integer;
szukana:integer;
linja:string;
plik:text;
startposx,startposy:integer;
{********************************************************}
function PobierzLiczbe( idx : integer; linja : string):integer;
var
pos1,pos2,x,i : integer;
pomoc,tmp : string;
wartosc : integer;
begin
pomoc:=linja;
if pos(';',pomoc)=0 then
begin
PobierzLiczbe:=0;
exit;
end;
for x:=1 to idx do
begin
pos1:=1;
pos2:=(pos(';',pomoc));
if pos2=0 then pos2:=999;
tmp:=copy(pomoc,pos1,pos2-1);
val(tmp,wartosc,i);
PobierzLiczbe:=wartosc;
delete(pomoc,pos1,pos2);
end;
end;
{********************************************************}
begin
{********************************************************}
Assign(plik,'D:\~~~~~gra1\Nowy folder\mapa.txt');
{$I-}
Reset(plik);
{$I+}
for curposx:=1 to N do
begin
readln(plik,linja);
for curposy:=1 to N do tab[curposx][curposy]:=PobierzLiczbe(curposy,linja);
end;
Close(plik);
{********************************************************}
{-------------------------------------------------------------------------}
szukana:=2;
for curposx:=1 to N do
for curposy:=1 to N do
if (tab[curposx][curposy]=szukana) then
begin
writeln('Indeksy szukanego elementu : ',curposx,' , ',curposy);
curposx:=curposx;
curposy:=curposy;
writeln(curposx,' ',curposy);
end;
{-------------------------------------------------------------------------}
if (tab[curposx+1,curposy] <> 1) and (tab[curposx-1,curposy] <> 1) and (tab[curposx,curposy+1] <> 1) and (tab[curposx,curposy-1] <> 1) then
begin
writeln('N S E W');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx,curposy-1] <> 1) and (tab[curposx+1,curposy] <> 1) then
begin
writeln('N S E');
if (tab[curposx-1,curposy] <> 1) and (tab[curposx,curposy+1] <> 1) and (tab[curposx,curposy-1] <> 1) then
begin
writeln('N S W');
if (tab[curposx,curposy-1] <> 1) and (tab[curposx+1,curposy] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('S E W');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx+1,curposy] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('N E W');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx,curposy-1] <> 1) then
begin
writeln('N S');
if (tab[curposx+1,curposy] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('E W');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('N W');
if (tab[curposx,curposy-1] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('S W');
if (tab[curposx,curposy-1] <> 1) and (tab[curposx+1,curposy] <> 1) then
begin
writeln('S E');
if (tab[curposx,curposy-1] <> 1) and (tab[curposx+1,curposy] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('S E W');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx+1,curposy] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('N E W');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx,curposy-1] <> 1) and (tab[curposx+1,curposy] <> 1) then
begin
writeln('N S E');
if (tab[curposx,curposy+1] <> 1) and (tab[curposx,curposy-1] <> 1) and (tab[curposx-1,curposy] <> 1) then
begin
writeln('N S W');
if (tab[curposx,curposy-1] <> 1) then
begin
writeln('S');
if (tab[curposx,curposy+1] <> 1) then
begin
writeln('N');
if (tab[curposx+1,curposy] <> 1) then
begin
writeln('E');
if (tab[curposx-1,curposy] <> 1) then
begin
writeln('W');
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
c2:=readkey;
end.
oraz zawartość pliku mapa.txt:
0;1;1;1;1;1;0;0;0;0;0;0
0;1;0;0;0;1;0;0;0;0;0;0
0;1;0;1;0;1;0;0;0;0;0;0
0;1;2;1;3;1;0;0;0;0;0;0
0;1;1;1;1;1;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0;0;0;0;0
0 - puste pola - można po nich chodzić
1 - ściany
2 - start
3 - koniec
fragmenty kodu pomiedzy {********} nie sa napisanie przeze mnie! zeby nikt sie nie plul ze kradne czyjas prace, znalazlem to na innym forum jako rozwiazanie do innego zadania.