Hey guys, I made a function that checks whether there are any elements in the adjacents cells (right, left, up and down). I tried this :
Function NoAdjacentElements(Board:TwoDArray;pY,pX,Length:Integer;Horizontal:boolean):boolean;
var PosX,PosY:integer;
Begin
PosX := pX;
PosY := pY;
If (Horizontal = True) and ((PosX+Length)<20) then
While (pX<=PosX+Length) do begin
If (pY<20) and (Board[pY+1,pX]<>'*') then
NoAdjacentElements := False;
If (pY>1) and (Board[pY-1,pX]<>'*') then
NoAdjacentElements := False;
If (pX<20) and (Board[pY,pX+1]<>'*') then
NoAdjacentElements := False;
If (pX>1) and (Board[pY,pX-1]<>'*') then
NoAdjacentElements := False;
pX := pX + 1;
If pX = PosX+Length then
NoAdjacentElements := True;
end;
If (Horizontal = False) and ((PosY+Length)<20) then
While (py<=PosY+Length) do begin
If (pY<20) and (Board[pY+1,pX]<>'*') then
NoAdjacentElements := False;
If (pY>1) and (Board[pY-1,pX]<>'*') then
NoAdjacentElements := False;
If (pX<20) and (Board[pY,pX+1]<>'*') then
NoAdjacentElements := False;
If (pX>1) and (Board[pY,pX-1]<>'*') then
NoAdjacentElements := False;
pY := pY + 1;
If (pY = PosY+Length) then
NoAdjacentElements := True;
end;
end;
Board is a two dimentional array, 20x20 made of chars.
pY and pX are generated randomly, they always have values between 1 and 20.
However, once I run the whole thing, I end up with adjacents characters anyway. If possible, I would like for you guys to point if there's any kind of mistake with this function, or maybe I'm just using this function the wrong way.