Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
- type
- { TForm1 }
- TForm1 = class(TForm)
- procedure FormCreate(Sender: TObject);
- private
- { private declarations }
- public
- pMat,cMat:array[0..9,0..9] of TShape;
- pBoard,pShots,cBoard,cShots: array[0..9,0..9] of integer;
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.lfm}
- { TForm1 }
- procedure TForm1.FormCreate(Sender: TObject);
- var i,j:integer;
- begin
- for i:=0 to 9 do
- for j:=0 to 9 do
- begin pMat[i,j]:= TShape.Create(Form1);
- with pMat[i,j] do
- begin
- width:=30;
- height:=30;
- left:=j*30+30;
- top:=i*30+30;
- visible:=true;
- parent:=Form1;
- shape:=stRectangle;
- name:='shapeA'+ IntToStr(i)+Inttostr(j);
- brush.color:=clwhite;
- brush.style:=bsSolid;
- pen.color:=clblack;
- pen.style:=psSolid;
- pen.width:=1;
- enabled:=true;
- end;
- end;
- for i:=0 to 9 do
- for j:=0 to 9 do
- begin cMat[i,j]:= TShape.Create(Form1);
- with cMat[i,j] do
- begin
- width:=30;
- height:=30;
- left:=j*30+420;
- top:=i*30+30;
- visible:=true;
- parent:=Form1;
- shape:=stRectangle;
- name:='shapeB'+ IntToStr(i)+Inttostr(j);
- brush.color:=clwhite;
- brush.style:=bsSolid;
- pen.color:=clblack;
- pen.style:=psSolid;
- pen.width:=1;
- enabled:=true;
- end;
- end;
- end;
- 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);
- 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
- PBoard[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
- PBoard[i,j]:=2;
- for i:=m to m+l-1 do
- PBoard[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
- PBoard[i,j]:=2;
- for j:=k-l+1 to k do
- PBoard[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
- PBoard[i,j]:=2;
- for j:=k to k+l-1 do
- PBoard[m,j]:=1;
- end;
- end;
- procedure PGenerateRandom;
- var
- x,l,m,k:integer;
- begin
- for i:=0 to 9 do
- for j:=0 to 9 do
- PBoard[i,j]:=0;
- l:=4;
- m:=random(9);
- k:=random(9);
- repeat
- x:=random(3);
- until PCheckForShip(m,k,x,l);
- PSetShip(m,k,x,l);
- l:=3;
- for i:=1 to 2 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;
- l:=2;
- for i:=1 to 3 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;
- l:=1
- for i:=1 to 4 do
- begin
- 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement