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,
- StdCtrls, CustomDrawnControls,customdrawn_common, MaskEdit;
- type
- { TForm1 }
- TForm1 = class(TForm)
- CDButton1: TCDButton;
- CDButton2: TCDButton;
- CDButton3: TCDButton;
- Label1: TLabel;
- cTurn: TShape;
- pTurn: TShape;
- procedure CDButton1Click(Sender: TObject);
- procedure CDButton2Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure pShapeMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- function pOdigraj(p,q:integer):boolean;
- function desno(x,y,tip:integer):integer;
- function levo(x,y,tip:integer):integer;
- function gore(x,y,tip:integer):integer;
- function dole(x,y,tip:integer):integer;
- function isPotopljen(x,y,tip:integer):boolean;
- {koor: record
- x,y:integer;
- end;}
- {procedure cOdigraj;}
- {function racunaj(x,y,max:integer):integer; }
- private
- { private declarations }
- public
- end;
- var
- Form1: TForm1;
- pBoard,pShots,cBoard,cShots: array[0..9,0..9] of integer;
- pHitCount,cHitCount:integer; {Koliko je pogodjenih u nizu}
- PMat,CMat:array[0..9,0..9] of TShape;
- {mode:integer;
- prob:array[0..9,0..9] of integer; }
- {maxprob:array[0..100]of koor;
- prethodni:koor; }
- {ships:array[1..4]of integer;}
- implementation
- {$R *.lfm}
- { TForm1 }
- procedure TForm1.FormCreate(Sender: TObject); {Pravi matricu shapeova i podesava pocetne vrednosti}
- 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;
- OnMouseDown:=@Form1.pShapeMouseDown;
- end;
- end;
- end;
- function TForm1.desno(x,y,tip:integer):integer;
- var br:integer;
- begin
- br:=0;
- if tip=1 then
- begin
- if x+1<=9 then
- begin
- if pShots[x,y]=1 then
- br:=1+desno(x+1,y,1)
- else br:=0;
- end
- else br:=0;
- end;
- if tip=2 then
- begin
- if x+1<=9 then
- begin
- if cShots[x,y]=1 then
- br:=1+desno(x+1,y,2)
- else br:=0;
- end
- else br:=0;
- end;
- desno:=br-1;
- end;
- function TForm1.levo(x,y,tip:integer):integer;
- var br:integer;
- begin
- br:=0;
- if tip=1 then
- begin
- if x-1>=0 then
- begin
- if pShots[x,y]=1 then
- br:=1+levo(x-1,y,1)
- else br:=0;
- end
- else br:=0;
- end;
- if tip=2 then
- begin
- if x-1>=0 then
- begin
- if cShots[x,y]=1 then
- br:=1+levo(x-1,y,2)
- else br:=0;
- end
- else br:=0;
- end;
- levo:=br-1;
- end;
- function TForm1.gore(x,y,tip:integer):integer;
- var br:integer;
- begin
- br:=0;
- if tip=1 then
- begin
- if y-1>=0 then
- begin
- if pShots[x,y]=1 then
- br:=1+gore(x,y-1,1)
- else br:=0;
- end
- else br:=0;
- end;
- if tip=2 then
- begin
- if y-1>=0 then
- begin
- if cShots[x,y]=1 then
- br:=1+gore(x,y-1,2)
- else br:=0;
- end
- else br:=0;
- end;
- gore:=br-1;
- end;
- function TForm1.dole(x,y,tip:integer):integer;
- var br:integer;
- begin
- br:=0;
- if tip=1 then
- begin
- if y+1<=9 then
- begin
- if pShots[x,y]=1 then
- br:=1+dole(x,y+1,1)
- else br:=0;
- end
- else br:=0;
- end;
- if tip=2 then
- begin
- if y+1<=9 then
- begin
- if cShots[x,y]=1 then
- br:=1+dole(x,y+1,2)
- else br:=0;
- end
- else br:=0;
- end;
- dole:=br-1;
- end;
- function TForm1.isPotopljen(x,y,tip:integer):boolean;
- begin
- if tip=1 then
- begin
- if (gore(x,y,1)+dole(x,y,1)+1=cBoard[x,y])or(levo(x,y,1)+desno(x,y,1)+1=cBoard[x,y])then
- isPotopljen:=true
- else isPotopljen:=false;
- end;
- if tip=2 then
- begin
- if (1+gore(x,y,2)+dole(x,y,2)=pBoard[x,y])or(1+levo(x,y,2)+desno(x,y,2)=pBoard[x,y])then
- isPotopljen:=true
- else isPotopljen:=false;
- end;
- end;
- {function TForm1.Racunaj(x,y,max:integer):integer;}
- {procedure TForm1.cOdigraj;
- var
- i,j,max:integer;
- begin
- if mode=1 then
- begin
- for i:=1 to 4 do
- if ships[i]<>0 then
- max:=i;
- for i:=0 to 9 do
- for j:=0 to 9 do
- end;
- end;}
- procedure TForm1.pShapeMouseDown(Sender: TObject; Button: TMouseButton; {Boji polje u zavisnosti da li je pogodjeno ili ne i unosi vrednosti u pShots}
- Shift: TShiftState; X, Y: Integer);
- var m1,m2,i,j:integer;
- begin
- if pTurn.Brush.color=clGreen then
- begin
- m1:=0;
- m2:=0;
- for i:=0 to 9 do
- for j:=0 to 9 do
- if Sender=cMat[i,j] then
- begin
- m1:=i;
- m2:=j;
- end;
- if (cBoard[m1,m2]<>0)and(cBoard[m1,m2]<>5) then
- begin
- pShots[m1,m2]:=1;
- cMat[m1,m2].Brush.Color:=clYellow;
- cMat[m1,m2].enabled:=false;
- if m1-1>=0 then
- begin
- if m2-1>=0 then
- begin
- cMat[m1-1,m2-1].Brush.Color:=clGray;
- cMat[m1-1,m2-1].enabled:=false;
- end;
- if m2+1<=9 then
- begin
- cMat[m1-1,m2+1].Brush.Color:=clGray;
- cMat[m1-1,m2+1].enabled:=false;
- end;
- end;
- if m1+1<=9 then
- begin
- if m2-1>=0 then
- begin
- cMat[m1+1,m2-1].Brush.Color:=clGray;
- cMat[m1+1,m2-1].enabled:=false;
- end;
- if m2+1<=9 then
- begin
- cMat[m1+1,m2+1].Brush.Color:=clGray;
- cMat[m1+1,m2+1].enabled:=false;
- end;
- end;
- if isPotopljen(m1,m2,1) then
- begin
- ShowMessage(inttostr(cBoard[m1,m2]));
- ShowMessage('brod potopljen');
- end;
- end
- else
- begin
- pShots[m1,m2]:=2;
- cMat[m1,m2].Brush.Color:=clGray;
- cMat[m1,m2].enabled:=false;
- {pTurn.Brush.color=clRed;
- cTurn.Brush.color=clGreen;}
- end;
- end;
- end;
- function TForm1.pOdigraj(p, q: integer): boolean;
- begin
- if cBoard[p,q]=1 then
- pOdigraj:=true
- else
- pOdigraj:=false;
- 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;
- if m>=l then
- begin
- for i:=m-l+1 to m do
- if pBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- end;
- if x=1 then
- begin
- br:=0;
- if m+l-1<=9 then
- begin
- for i:=m to m+l-1 do
- if PBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- end;
- if x=2 then
- begin
- br:=0;
- if k>=l then
- begin
- for j:=k-l+1 to k do
- if PBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- end;
- if x=3 then
- begin
- br:=0;
- if k+l-1<=9 then
- begin
- for j:=k to k+l-1 do
- if PBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- 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
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(pBoard[i,j]<>1)and(pBoard[i,j]<>2)and(pBoard[i,j]<>3)and(pBoard[i,j]<>4) then
- PBoard[i,j]:=5;
- for i:=m-l+1 to m do
- begin
- PBoard[i,k]:=l;
- 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
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(pBoard[i,j]<>1)and(pBoard[i,j]<>2)and(pBoard[i,j]<>3)and(pBoard[i,j]<>4) then
- PBoard[i,j]:=5;
- for i:=m to m+l-1 do
- begin
- PBoard[i,k]:=l;
- 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
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(pBoard[i,j]<>1)and(pBoard[i,j]<>2)and(pBoard[i,j]<>3)and(pBoard[i,j]<>4) then
- PBoard[i,j]:=5;
- for j:=k-l+1 to k do
- begin
- PBoard[m,j]:=l;
- 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
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(pBoard[i,j]<>1)and(pBoard[i,j]<>2)and(pBoard[i,j]<>3)and(pBoard[i,j]<>4) then
- PBoard[i,j]:=5;
- for j:=k to k+l-1 do
- begin
- PBoard[m,j]:=l;
- PMat[m,j].brush.color:=clblue;
- end;
- end;
- end;
- procedure PGenerateRandom;
- var
- x,l,m,k,i,j:integer;
- begin
- for l:=4 downto 1 do
- begin
- j:=4-l+1;
- for i:=1 to j do
- begin
- repeat
- randomize;
- 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;
- if m>=l then
- begin
- for i:=m-l+1 to m do
- if cBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- end;
- if x=1 then
- begin
- br:=0;
- if m+l-1<=9 then
- begin
- for i:=m to m+l-1 do
- if cBoard[i,k]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- end;
- if x=2 then
- begin
- br:=0;
- if k>=l then
- begin
- for j:=k-l+1 to k do
- if cBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- end;
- if x=3 then
- begin
- br:=0;
- if k+l-1<=9 then
- begin
- for j:=k to k+l-1 do
- if cBoard[m,j]=0 then
- br:=br+1;
- if br=l then
- ok:=true;
- end;
- 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
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(cBoard[i,j]<>1)and(cBoard[i,j]<>2)and(cBoard[i,j]<>3)and(cBoard[i,j]<>4) then
- cBoard[i,j]:=5;
- for i:=m-l+1 to m do
- cBoard[i,k]:=l;
- end;
- if x=1 then
- begin
- for i:=m-1 to m+l do
- for j:=k-1 to k+1 do
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(cBoard[i,j]<>1)and(cBoard[i,j]<>2)and(cBoard[i,j]<>3)and(cBoard[i,j]<>4) then
- cBoard[i,j]:=5;
- for i:=m to m+l-1 do
- cBoard[i,k]:=l;
- end;
- if x=2 then
- begin
- for i:=m-1 to m+1 do
- for j:=k-l to k+1 do
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(cBoard[i,j]<>1)and(cBoard[i,j]<>2)and(cBoard[i,j]<>3)and(cBoard[i,j]<>4) then
- cBoard[i,j]:=5;
- for j:=k-l+1 to k do
- cBoard[m,j]:=l;
- end;
- if x=3 then
- begin
- for i:=m-1 to m+1 do
- for j:=k-1 to k+l do
- if (i>=0)and(i<=9)and(j>=0)and(j<=9)and(cBoard[i,j]<>1)and(cBoard[i,j]<>2)and(cBoard[i,j]<>3)and(cBoard[i,j]<>4) then
- cBoard[i,j]:=5;
- for j:=k to k+l-1 do
- cBoard[m,j]:=l;
- end;
- end;
- procedure cGenerateRandom;
- var
- x,l,m,k,i,j:integer;
- begin
- for l:=4 downto 1 do
- begin
- j:=4-l+1;
- for i:=1 to j do
- begin
- repeat
- randomize;
- 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;
- procedure TForm1.CDButton1Click(Sender: TObject);
- begin
- cGenerateRandom;
- pGenerateRandom;
- end;
- procedure TForm1.CDButton2Click(Sender: TObject);
- var i,j:integer;
- begin
- pTurn.Brush.color:=clGreen;
- cTurn.Brush.color:=clRed;
- for i:=0 to 9 do
- for j:=0 to 9 do
- begin
- cBoard[i,j]:=0;
- cMat[i,j].brush.color:=clWhite;
- cMat[i,j].enabled:=true;
- end;
- for i:=0 to 9 do
- for j:=0 to 9 do
- begin
- PBoard[i,j]:=0;
- PMat[i,j].brush.color:=clwhite;
- end;
- for i:=1 to 4 do
- {ships[i]:=4-i+1;}
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement