Advertisement
g0ku

Untitled

Dec 9th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 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.  
  10. type
  11.  
  12. { TForm1 }
  13.  
  14. TForm1 = class(TForm)
  15. procedure FormCreate(Sender: TObject);
  16. private
  17. { private declarations }
  18. public
  19. pMat,cMat:array[0..9,0..9] of TShape;
  20.  
  21. pBoard,pShots,cBoard,cShots: array[0..9,0..9] of integer;
  22. end;
  23.  
  24. var
  25. Form1: TForm1;
  26.  
  27.  
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. var i,j:integer;
  37. begin
  38. for i:=0 to 9 do
  39. for j:=0 to 9 do
  40. begin pMat[i,j]:= TShape.Create(Form1);
  41. with pMat[i,j] do
  42. begin
  43. width:=30;
  44. height:=30;
  45. left:=j*30+30;
  46. top:=i*30+30;
  47. visible:=true;
  48. parent:=Form1;
  49. shape:=stRectangle;
  50. name:='shapeA'+ IntToStr(i)+Inttostr(j);
  51. brush.color:=clwhite;
  52. brush.style:=bsSolid;
  53. pen.color:=clblack;
  54. pen.style:=psSolid;
  55. pen.width:=1;
  56. enabled:=true;
  57.  
  58. end;
  59. end;
  60. for i:=0 to 9 do
  61. for j:=0 to 9 do
  62. begin cMat[i,j]:= TShape.Create(Form1);
  63. with cMat[i,j] do
  64. begin
  65. width:=30;
  66. height:=30;
  67. left:=j*30+420;
  68. top:=i*30+30;
  69. visible:=true;
  70. parent:=Form1;
  71. shape:=stRectangle;
  72. name:='shapeB'+ IntToStr(i)+Inttostr(j);
  73. brush.color:=clwhite;
  74. brush.style:=bsSolid;
  75. pen.color:=clblack;
  76. pen.style:=psSolid;
  77. pen.width:=1;
  78. enabled:=true;
  79.  
  80. end;
  81.  
  82. end;
  83. end;
  84. function PCheckForShip(m,k,x,l:integer):boolean;
  85. var ok:boolean;
  86. br,i,j:integer;
  87.  
  88. begin
  89. ok:=false;
  90. if x=0 then
  91. begin
  92. br:=0;
  93. for i:=m-l+1 to m do
  94. if pBoard[i,k]=0 then
  95. br=br+1;
  96. if br=l then
  97. ok:=true;
  98. end;
  99.  
  100. if x=1 then
  101. begin
  102. br:=0;
  103. for i:=m to m+l-1 do
  104. if PBoard[i,k]=0 then
  105. br=br+1;
  106. if br=l then
  107. ok:=true;
  108. end;
  109.  
  110. if x=2 then
  111. begin
  112. br:=0;
  113. for j:=k-l+1 to k do
  114. if PBoard[m,j]=0 then
  115. br=br+1;
  116. if br=l then
  117. ok:=true;
  118. end;
  119.  
  120. if x=3 then
  121. begin
  122. br:=0;
  123. for j:=k to k+l-1 do
  124. if PBoard[m,j]=0 then
  125. br=br+1;
  126. if br=l then
  127. ok:=true;
  128. end;
  129.  
  130. PCheckForShip:=ok;
  131. end;
  132.  
  133. procedure PSetShip(m,k,x,l:integer);
  134. begin
  135. if x=0 then
  136. begin
  137. for i:=m-l to m+1 do
  138. for j:=k-1 to k+1 do
  139. PBoard[i,j]:=2;
  140. for i:=m-l+1 to m do
  141. PBoard[i,k]:=1;
  142. end;
  143. if x=1 then
  144. begin
  145. for i:=m-1 to m+l do
  146. for j:=k-1 to k+1 do
  147. PBoard[i,j]:=2;
  148. for i:=m to m+l-1 do
  149. PBoard[i,k]:=1;
  150. end;
  151. if x=2 then
  152. begin
  153. for i:=m-1 to m+1 do
  154. for j:=k-l to k+1 do
  155. PBoard[i,j]:=2;
  156. for j:=k-l+1 to k do
  157. PBoard[m,j]:=1;
  158. end;
  159. if x=3 then
  160. begin
  161. for i:=m-1 to m+1 do
  162. for j:=k-1 to k+l do
  163. PBoard[i,j]:=2;
  164. for j:=k to k+l-1 do
  165. PBoard[m,j]:=1;
  166. end;
  167. end;
  168.  
  169. procedure PGenerateRandom;
  170. var
  171. x,l,m,k:integer;
  172. begin
  173. for i:=0 to 9 do
  174. for j:=0 to 9 do
  175. PBoard[i,j]:=0;
  176. l:=4;
  177. m:=random(9);
  178. k:=random(9);
  179. repeat
  180. x:=random(3);
  181. until PCheckForShip(m,k,x,l);
  182. PSetShip(m,k,x,l);
  183. l:=3;
  184. for i:=1 to 2 do
  185. begin
  186. repeat
  187. m:=random(9);
  188. k:=random(9);
  189. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  190. repeat
  191. x:=random(3);
  192. until PCheckForShip(m,k,x,l);
  193. PSetShip(m,k,x,l)
  194. end;
  195. l:=2;
  196. for i:=1 to 3 do
  197. begin
  198. repeat
  199. m:=random(9);
  200. k:=random(9);
  201. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  202. repeat
  203. x:=random(3);
  204. until PCheckForShip(m,k,x,l);
  205. PSetShip(m,k,x,l)
  206. end;
  207. l:=1
  208. for i:=1 to 4 do
  209. begin
  210. begin
  211. repeat
  212. m:=random(9);
  213. k:=random(9);
  214. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  215. repeat
  216. x:=random(3);
  217. until PCheckForShip(m,k,x,l);
  218. PSetShip(m,k,x,l)
  219. end;
  220.  
  221.  
  222.  
  223. end;
  224.  
  225.  
  226. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement