Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function PCheckForShip(m,k,x,l:integer):boolean;
- var ok:boolean;
- br,i,j:integer;
- begin
- ok:=false;
- if x=0 then
- begin
- br:=0;
- for i:=m-l+1 to m do
- if pBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- if x=1 then
- begin
- br:=0;
- for i:=m to m+l-1 do
- if PBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- if x=2 then
- begin
- br:=0;
- for j:=k-l+1 to k do
- if PBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- if x=3 then
- begin
- br:=0;
- for j:=k to k+l-1 do
- if PBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- PCheckForShip:=ok;
- end;
- procedure PSetShip(m,k,x,l:integer);
- var i,j:integer;
- begin
- if x=0 then
- begin
- for i:=m-l to m+1 do
- for j:=k-1 to k+1 do
- PBoard[i,j]:=2;
- for i:=m-l+1 to m do
- begin
- PBoard[i,k]:=1;
- PMat[i,k].brush.color:=clblue;
- end;
- end;
- if x=1 then
- begin
- for i:=m-1 to m+l do
- for j:=k-1 to k+1 do
- PBoard[i,j]:=2;
- for i:=m to m+l-1 do
- begin
- PBoard[i,k]:=1;
- PMat[i,k].brush.color:=clblue;
- end;
- end;
- if x=2 then
- begin
- for i:=m-1 to m+1 do
- for j:=k-l to k+1 do
- PBoard[i,j]:=2;
- for j:=k-l+1 to k do
- begin
- PBoard[m,j]:=1;
- PMat[m,j].brush.color:=clblue;
- end;
- end;
- if x=3 then
- begin
- for i:=m-1 to m+1 do
- for j:=k-1 to k+l do
- PBoard[i,j]:=2;
- for j:=k to k+l-1 do
- begin
- PBoard[m,j]:=1;
- PMat[m,j].brush.color:=clblue;
- end;
- end;
- end;
- procedure PGenerateRandom;
- var
- x,l,m,k,i,j:integer;
- begin
- for i:=0 to 9 do
- for j:=0 to 9 do
- PBoard[i,j]:=0;
- for l:=4 downto 1 do
- begin
- j:=4-l+1;
- for i:=1 to j do
- begin
- repeat
- m:=random(9);
- k:=random(9);
- until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
- repeat
- x:=random(3);
- until PCheckForShip(m,k,x,l);
- PSetShip(m,k,x,l);
- end;
- end;
- end;
- function cCheckForShip(m,k,x,l:integer):boolean;
- var ok:boolean;
- br,i,j:integer;
- begin
- ok:=false;
- if x=0 then
- begin
- br:=0;
- for i:=m-l+1 to m do
- if cBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- if x=1 then
- begin
- br:=0;
- for i:=m to m+l-1 do
- if cBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- if x=2 then
- begin
- br:=0;
- for j:=k-l+1 to k do
- if cBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- if x=3 then
- begin
- br:=0;
- for j:=k to k+l-1 do
- if cBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- cCheckForShip:=ok;
- end;
- procedure cSetShip(m,k,x,l:integer);
- var i,j:integer;
- begin
- if x=0 then
- begin
- for i:=m-l to m+1 do
- for j:=k-1 to k+1 do
- cBoard[i,j]:=2;
- for i:=m-l+1 to m do
- cBoard[i,k]:=1;
- end;
- if x=1 then
- begin
- for i:=m-1 to m+l do
- for j:=k-1 to k+1 do
- cBoard[i,j]:=2;
- for i:=m to m+l-1 do
- cBoard[i,k]:=1;
- end;
- if x=2 then
- begin
- for i:=m-1 to m+1 do
- for j:=k-l to k+1 do
- cBoard[i,j]:=2;
- for j:=k-l+1 to k do
- cBoard[m,j]:=1;
- end;
- if x=3 then
- begin
- for i:=m-1 to m+1 do
- for j:=k-1 to k+l do
- cBoard[i,j]:=2;
- for j:=k to k+l-1 do
- cBoard[m,j]:=1;
- end;
- shipsset:=true;
- end;
- procedure cGenerateRandom;
- var
- x,l,m,k,i,j:integer;
- begin
- for i:=0 to 9 do
- for j:=0 to 9 do
- cBoard[i,j]:=0;
- for l:=4 downto 1 do
- begin
- j:=4-l+1;
- for i:=1 to j do
- begin
- repeat
- m:=random(9);
- k:=random(9);
- until CCheckForShip(m,k,0,l) OR CCheckForShip(m,k,1,l) OR CCheckForShip(m,k,2,l) OR CCheckForShip(m,k,3,l);
- repeat
- x:=random(3);
- until CCheckForShip(m,k,x,l);
- CSetShip(m,k,x,l);
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement