Advertisement
g0ku

Untitled

Feb 15th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.22 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9. StdCtrls, CustomDrawnControls,customdrawn_common, MaskEdit;
  10.  
  11. type
  12.  
  13. { TForm1 }
  14.  
  15. TForm1 = class(TForm)
  16. CDButton1: TCDButton;
  17. CDButton2: TCDButton;
  18. CDButton3: TCDButton;
  19. Label1: TLabel;
  20. cTurn: TShape;
  21. pTurn: TShape;
  22. procedure CDButton1Click(Sender: TObject);
  23. procedure CDButton2Click(Sender: TObject);
  24. procedure FormCreate(Sender: TObject);
  25. procedure pShapeMouseDown(Sender: TObject; Button: TMouseButton;
  26. Shift: TShiftState; X, Y: Integer);
  27. function pOdigraj(p,q:integer):boolean;
  28. function desno(x,y,tip:integer):integer;
  29. function levo(x,y,tip:integer):integer;
  30. function gore(x,y,tip:integer):integer;
  31. function dole(x,y,tip:integer):integer;
  32. function isPotopljen(x,y,tip:integer):boolean;
  33. {koor: record
  34. x,y:integer;
  35. end;}
  36. {procedure cOdigraj;}
  37. {function racunaj(x,y,max:integer):integer; }
  38.  
  39. private
  40. { private declarations }
  41. public
  42.  
  43. end;
  44.  
  45. var
  46. Form1: TForm1;
  47. pBoard,pShots,cBoard,cShots: array[0..9,0..9] of integer;
  48. pHitCount,cHitCount:integer; {Koliko je pogodjenih u nizu}
  49. PMat,CMat:array[0..9,0..9] of TShape;
  50. {mode:integer;
  51. prob:array[0..9,0..9] of integer; }
  52. {maxprob:array[0..100]of koor;
  53. prethodni:koor; }
  54. {ships:array[1..4]of integer;}
  55.  
  56. implementation
  57.  
  58. {$R *.lfm}
  59.  
  60. { TForm1 }
  61.  
  62. procedure TForm1.FormCreate(Sender: TObject); {Pravi matricu shapeova i podesava pocetne vrednosti}
  63. var i,j:integer;
  64. begin
  65. for i:=0 to 9 do
  66. for j:=0 to 9 do
  67. begin pMat[i,j]:= TShape.Create(Form1);
  68. with pMat[i,j] do
  69. begin
  70. width:=30;
  71. height:=30;
  72. left:=j*30+30;
  73. top:=i*30+30;
  74. visible:=true;
  75. parent:=Form1;
  76. shape:=stRectangle;
  77. name:='shapeA'+ IntToStr(i)+Inttostr(j);
  78. brush.color:=clwhite;
  79. brush.style:=bsSolid;
  80. pen.color:=clblack;
  81. pen.style:=psSolid;
  82. pen.width:=1;
  83. enabled:=true;
  84.  
  85.  
  86. end;
  87. end;
  88. for i:=0 to 9 do
  89. for j:=0 to 9 do
  90. begin cMat[i,j]:= TShape.Create(Form1);
  91. with cMat[i,j] do
  92. begin
  93. width:=30;
  94. height:=30;
  95. left:=j*30+420;
  96. top:=i*30+30;
  97. visible:=true;
  98. parent:=Form1;
  99. shape:=stRectangle;
  100. name:='shapeB'+ IntToStr(i)+Inttostr(j);
  101. brush.color:=clwhite;
  102. brush.style:=bsSolid;
  103. pen.color:=clblack;
  104. pen.style:=psSolid;
  105. pen.width:=1;
  106. enabled:=true;
  107. OnMouseDown:=@Form1.pShapeMouseDown;
  108.  
  109.  
  110. end;
  111.  
  112. end;
  113.  
  114.  
  115. end;
  116.  
  117. function TForm1.desno(x,y,tip:integer):integer;
  118. var br:integer;
  119. begin
  120. br:=0;
  121. if tip=1 then
  122. begin
  123. if x+1<=9 then
  124. begin
  125. if pShots[x,y]=1 then
  126. br:=1+desno(x+1,y,1)
  127. else br:=0;
  128. end
  129. else br:=0;
  130. end;
  131.  
  132. if tip=2 then
  133. begin
  134. if x+1<=9 then
  135. begin
  136. if cShots[x,y]=1 then
  137. br:=1+desno(x+1,y,2)
  138. else br:=0;
  139. end
  140. else br:=0;
  141. end;
  142. desno:=br-1;
  143. end;
  144. function TForm1.levo(x,y,tip:integer):integer;
  145. var br:integer;
  146. begin
  147. br:=0;
  148. if tip=1 then
  149. begin
  150. if x-1>=0 then
  151. begin
  152. if pShots[x,y]=1 then
  153. br:=1+levo(x-1,y,1)
  154. else br:=0;
  155. end
  156. else br:=0;
  157. end;
  158.  
  159. if tip=2 then
  160. begin
  161. if x-1>=0 then
  162. begin
  163. if cShots[x,y]=1 then
  164. br:=1+levo(x-1,y,2)
  165. else br:=0;
  166. end
  167. else br:=0;
  168. end;
  169. levo:=br-1;
  170. end;
  171. function TForm1.gore(x,y,tip:integer):integer;
  172. var br:integer;
  173. begin
  174. br:=0;
  175. if tip=1 then
  176. begin
  177. if y-1>=0 then
  178. begin
  179. if pShots[x,y]=1 then
  180. br:=1+gore(x,y-1,1)
  181. else br:=0;
  182. end
  183. else br:=0;
  184. end;
  185.  
  186. if tip=2 then
  187. begin
  188. if y-1>=0 then
  189. begin
  190. if cShots[x,y]=1 then
  191. br:=1+gore(x,y-1,2)
  192. else br:=0;
  193. end
  194. else br:=0;
  195. end;
  196. gore:=br-1;
  197. end;
  198. function TForm1.dole(x,y,tip:integer):integer;
  199. var br:integer;
  200. begin
  201. br:=0;
  202. if tip=1 then
  203. begin
  204. if y+1<=9 then
  205. begin
  206. if pShots[x,y]=1 then
  207. br:=1+dole(x,y+1,1)
  208. else br:=0;
  209. end
  210. else br:=0;
  211. end;
  212.  
  213. if tip=2 then
  214. begin
  215. if y+1<=9 then
  216. begin
  217. if cShots[x,y]=1 then
  218. br:=1+dole(x,y+1,2)
  219. else br:=0;
  220. end
  221. else br:=0;
  222. end;
  223. dole:=br-1;
  224. end;
  225. function TForm1.isPotopljen(x,y,tip:integer):boolean;
  226. begin
  227. if tip=1 then
  228. begin
  229. 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
  230. isPotopljen:=true
  231. else isPotopljen:=false;
  232. end;
  233.  
  234. if tip=2 then
  235. begin
  236. 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
  237. isPotopljen:=true
  238. else isPotopljen:=false;
  239. end;
  240.  
  241. end;
  242. {function TForm1.Racunaj(x,y,max:integer):integer;}
  243. {procedure TForm1.cOdigraj;
  244. var
  245. i,j,max:integer;
  246. begin
  247. if mode=1 then
  248. begin
  249. for i:=1 to 4 do
  250. if ships[i]<>0 then
  251. max:=i;
  252. for i:=0 to 9 do
  253. for j:=0 to 9 do
  254.  
  255.  
  256. end;
  257. end;}
  258.  
  259. procedure TForm1.pShapeMouseDown(Sender: TObject; Button: TMouseButton; {Boji polje u zavisnosti da li je pogodjeno ili ne i unosi vrednosti u pShots}
  260. Shift: TShiftState; X, Y: Integer);
  261. var m1,m2,i,j:integer;
  262. begin
  263. if pTurn.Brush.color=clGreen then
  264. begin
  265.  
  266. m1:=0;
  267. m2:=0;
  268. for i:=0 to 9 do
  269. for j:=0 to 9 do
  270. if Sender=cMat[i,j] then
  271. begin
  272. m1:=i;
  273. m2:=j;
  274. end;
  275.  
  276. if (cBoard[m1,m2]<>0)and(cBoard[m1,m2]<>5) then
  277. begin
  278. pShots[m1,m2]:=1;
  279. cMat[m1,m2].Brush.Color:=clYellow;
  280. cMat[m1,m2].enabled:=false;
  281. if m1-1>=0 then
  282. begin
  283. if m2-1>=0 then
  284. begin
  285. cMat[m1-1,m2-1].Brush.Color:=clGray;
  286. cMat[m1-1,m2-1].enabled:=false;
  287. end;
  288. if m2+1<=9 then
  289. begin
  290. cMat[m1-1,m2+1].Brush.Color:=clGray;
  291. cMat[m1-1,m2+1].enabled:=false;
  292. end;
  293. end;
  294. if m1+1<=9 then
  295. begin
  296. if m2-1>=0 then
  297. begin
  298. cMat[m1+1,m2-1].Brush.Color:=clGray;
  299. cMat[m1+1,m2-1].enabled:=false;
  300. end;
  301. if m2+1<=9 then
  302. begin
  303. cMat[m1+1,m2+1].Brush.Color:=clGray;
  304. cMat[m1+1,m2+1].enabled:=false;
  305. end;
  306. end;
  307. if isPotopljen(m1,m2,1) then
  308. begin
  309. ShowMessage(inttostr(cBoard[m1,m2]));
  310. ShowMessage('brod potopljen');
  311. end;
  312. end
  313. else
  314. begin
  315. pShots[m1,m2]:=2;
  316. cMat[m1,m2].Brush.Color:=clGray;
  317. cMat[m1,m2].enabled:=false;
  318. {pTurn.Brush.color=clRed;
  319. cTurn.Brush.color=clGreen;}
  320. end;
  321.  
  322.  
  323.  
  324. end;
  325. end;
  326.  
  327.  
  328. function TForm1.pOdigraj(p, q: integer): boolean;
  329. begin
  330. if cBoard[p,q]=1 then
  331. pOdigraj:=true
  332. else
  333. pOdigraj:=false;
  334. end;
  335.  
  336.  
  337.  
  338. function PCheckForShip(m,k,x,l:integer):boolean;
  339. var ok:boolean;
  340. br,i,j:integer;
  341.  
  342. begin
  343. ok:=false;
  344. if x=0 then
  345. begin
  346. br:=0;
  347. if m>=l then
  348. begin
  349. for i:=m-l+1 to m do
  350. if pBoard[i,k]=0 then
  351. br:=br+1;
  352. if br=l then
  353. ok:=true;
  354.  
  355. end;
  356. end;
  357.  
  358. if x=1 then
  359. begin
  360. br:=0;
  361. if m+l-1<=9 then
  362. begin
  363. for i:=m to m+l-1 do
  364. if PBoard[i,k]=0 then
  365. br:=br+1;
  366. if br=l then
  367. ok:=true;
  368.  
  369. end;
  370. end;
  371.  
  372. if x=2 then
  373. begin
  374. br:=0;
  375. if k>=l then
  376. begin
  377. for j:=k-l+1 to k do
  378. if PBoard[m,j]=0 then
  379. br:=br+1;
  380. if br=l then
  381. ok:=true;
  382.  
  383. end;
  384. end;
  385.  
  386. if x=3 then
  387. begin
  388. br:=0;
  389. if k+l-1<=9 then
  390. begin
  391. for j:=k to k+l-1 do
  392. if PBoard[m,j]=0 then
  393. br:=br+1;
  394. if br=l then
  395. ok:=true;
  396.  
  397. end;
  398. end;
  399.  
  400. PCheckForShip:=ok;
  401. end;
  402.  
  403. procedure PSetShip(m,k,x,l:integer);
  404. var i,j:integer;
  405. begin
  406. if x=0 then
  407. begin
  408. for i:=m-l to m+1 do
  409. for j:=k-1 to k+1 do
  410. 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
  411. PBoard[i,j]:=5;
  412. for i:=m-l+1 to m do
  413. begin
  414. PBoard[i,k]:=l;
  415. PMat[i,k].brush.color:=clblue;
  416. end;
  417. end;
  418. if x=1 then
  419. begin
  420. for i:=m-1 to m+l do
  421. for j:=k-1 to k+1 do
  422. 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
  423. PBoard[i,j]:=5;
  424. for i:=m to m+l-1 do
  425. begin
  426. PBoard[i,k]:=l;
  427. PMat[i,k].brush.color:=clblue;
  428. end;
  429. end;
  430. if x=2 then
  431. begin
  432. for i:=m-1 to m+1 do
  433. for j:=k-l to k+1 do
  434. 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
  435. PBoard[i,j]:=5;
  436. for j:=k-l+1 to k do
  437. begin
  438. PBoard[m,j]:=l;
  439. PMat[m,j].brush.color:=clblue;
  440. end;
  441. end;
  442. if x=3 then
  443. begin
  444. for i:=m-1 to m+1 do
  445. for j:=k-1 to k+l do
  446. 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
  447. PBoard[i,j]:=5;
  448. for j:=k to k+l-1 do
  449. begin
  450. PBoard[m,j]:=l;
  451. PMat[m,j].brush.color:=clblue;
  452. end;
  453. end;
  454.  
  455. end;
  456.  
  457. procedure PGenerateRandom;
  458. var
  459. x,l,m,k,i,j:integer;
  460. begin
  461.  
  462. for l:=4 downto 1 do
  463. begin
  464. j:=4-l+1;
  465.  
  466. for i:=1 to j do
  467. begin
  468. repeat
  469. randomize;
  470. m:=random(9);
  471. k:=random(9);
  472. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  473.  
  474. repeat
  475. x:=random(3);
  476. until PCheckForShip(m,k,x,l);
  477.  
  478. PSetShip(m,k,x,l);
  479.  
  480. end;
  481.  
  482.  
  483.  
  484.  
  485. end;
  486.  
  487. end;
  488. function cCheckForShip(m,k,x,l:integer):boolean;
  489. var ok:boolean;
  490. br,i,j:integer;
  491.  
  492. begin
  493. ok:=false;
  494. if x=0 then
  495. begin
  496. br:=0;
  497. if m>=l then
  498. begin
  499. for i:=m-l+1 to m do
  500. if cBoard[i,k]=0 then
  501. br:=br+1;
  502. if br=l then
  503. ok:=true;
  504.  
  505. end;
  506. end;
  507.  
  508. if x=1 then
  509. begin
  510. br:=0;
  511. if m+l-1<=9 then
  512. begin
  513. for i:=m to m+l-1 do
  514. if cBoard[i,k]=0 then
  515. br:=br+1;
  516. if br=l then
  517. ok:=true;
  518.  
  519. end;
  520.  
  521. end;
  522.  
  523. if x=2 then
  524. begin
  525. br:=0;
  526. if k>=l then
  527. begin
  528. for j:=k-l+1 to k do
  529. if cBoard[m,j]=0 then
  530. br:=br+1;
  531. if br=l then
  532. ok:=true;
  533.  
  534. end;
  535. end;
  536.  
  537. if x=3 then
  538. begin
  539. br:=0;
  540. if k+l-1<=9 then
  541. begin
  542. for j:=k to k+l-1 do
  543. if cBoard[m,j]=0 then
  544. br:=br+1;
  545. if br=l then
  546. ok:=true;
  547.  
  548. end;
  549.  
  550. end;
  551.  
  552. cCheckForShip:=ok;
  553. end;
  554. procedure cSetShip(m,k,x,l:integer);
  555. var i,j:integer;
  556. begin
  557. if x=0 then
  558. begin
  559.  
  560. for i:=m-l to m+1 do
  561. for j:=k-1 to k+1 do
  562. 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
  563. cBoard[i,j]:=5;
  564. for i:=m-l+1 to m do
  565.  
  566. cBoard[i,k]:=l;
  567.  
  568. end;
  569. if x=1 then
  570. begin
  571.  
  572. for i:=m-1 to m+l do
  573. for j:=k-1 to k+1 do
  574. 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
  575. cBoard[i,j]:=5;
  576. for i:=m to m+l-1 do
  577.  
  578. cBoard[i,k]:=l;
  579.  
  580. end;
  581. if x=2 then
  582. begin
  583.  
  584. for i:=m-1 to m+1 do
  585. for j:=k-l to k+1 do
  586. 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
  587. cBoard[i,j]:=5;
  588. for j:=k-l+1 to k do
  589.  
  590. cBoard[m,j]:=l;
  591.  
  592. end;
  593. if x=3 then
  594. begin
  595.  
  596. for i:=m-1 to m+1 do
  597. for j:=k-1 to k+l do
  598. 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
  599. cBoard[i,j]:=5;
  600. for j:=k to k+l-1 do
  601. cBoard[m,j]:=l;
  602. end;
  603.  
  604. end;
  605. procedure cGenerateRandom;
  606. var
  607. x,l,m,k,i,j:integer;
  608. begin
  609.  
  610. for l:=4 downto 1 do
  611. begin
  612. j:=4-l+1;
  613.  
  614. for i:=1 to j do
  615. begin
  616. repeat
  617. randomize;
  618. m:=random(9);
  619. k:=random(9);
  620. until CCheckForShip(m,k,0,l) OR CCheckForShip(m,k,1,l) OR CCheckForShip(m,k,2,l) OR CCheckForShip(m,k,3,l);
  621.  
  622. repeat
  623. x:=random(3);
  624. until CCheckForShip(m,k,x,l);
  625.  
  626. CSetShip(m,k,x,l);
  627.  
  628. end;
  629.  
  630.  
  631.  
  632. end;
  633.  
  634. end;
  635.  
  636. procedure TForm1.CDButton1Click(Sender: TObject);
  637.  
  638. begin
  639.  
  640.  
  641. cGenerateRandom;
  642. pGenerateRandom;
  643.  
  644. end;
  645.  
  646. procedure TForm1.CDButton2Click(Sender: TObject);
  647. var i,j:integer;
  648. begin
  649. pTurn.Brush.color:=clGreen;
  650. cTurn.Brush.color:=clRed;
  651. for i:=0 to 9 do
  652. for j:=0 to 9 do
  653. begin
  654. cBoard[i,j]:=0;
  655. cMat[i,j].brush.color:=clWhite;
  656. cMat[i,j].enabled:=true;
  657. end;
  658. for i:=0 to 9 do
  659. for j:=0 to 9 do
  660. begin
  661. PBoard[i,j]:=0;
  662. PMat[i,j].brush.color:=clwhite;
  663. end;
  664. for i:=1 to 4 do
  665. {ships[i]:=4-i+1;}
  666. end;
  667.  
  668. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement