Advertisement
g0ku

Untitled

Jan 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. function PCheckForShip(m,k,x,l:integer):boolean;
  2. var ok:boolean;
  3. br,i,j:integer;
  4.  
  5. begin
  6. ok:=false;
  7. if x=0 then
  8. begin
  9. br:=0;
  10. for i:=m-l+1 to m do
  11. if pBoard[i,k]=0 then
  12. br:=br+1;
  13. if br=l then
  14. ok:=true;
  15. end;
  16.  
  17. if x=1 then
  18. begin
  19. br:=0;
  20. for i:=m to m+l-1 do
  21. if PBoard[i,k]=0 then
  22. br:=br+1;
  23. if br=l then
  24. ok:=true;
  25. end;
  26.  
  27. if x=2 then
  28. begin
  29. br:=0;
  30. for j:=k-l+1 to k do
  31. if PBoard[m,j]=0 then
  32. br:=br+1;
  33. if br=l then
  34. ok:=true;
  35. end;
  36.  
  37. if x=3 then
  38. begin
  39. br:=0;
  40. for j:=k to k+l-1 do
  41. if PBoard[m,j]=0 then
  42. br:=br+1;
  43. if br=l then
  44. ok:=true;
  45. end;
  46.  
  47. PCheckForShip:=ok;
  48. end;
  49.  
  50. procedure PSetShip(m,k,x,l:integer);
  51. var i,j:integer;
  52. begin
  53. if x=0 then
  54. begin
  55. for i:=m-l to m+1 do
  56. for j:=k-1 to k+1 do
  57. PBoard[i,j]:=2;
  58. for i:=m-l+1 to m do
  59. begin
  60. PBoard[i,k]:=1;
  61. PMat[i,k].brush.color:=clblue;
  62. end;
  63. end;
  64. if x=1 then
  65. begin
  66. for i:=m-1 to m+l do
  67. for j:=k-1 to k+1 do
  68. PBoard[i,j]:=2;
  69. for i:=m to m+l-1 do
  70. begin
  71. PBoard[i,k]:=1;
  72. PMat[i,k].brush.color:=clblue;
  73. end;
  74. end;
  75. if x=2 then
  76. begin
  77. for i:=m-1 to m+1 do
  78. for j:=k-l to k+1 do
  79. PBoard[i,j]:=2;
  80. for j:=k-l+1 to k do
  81. begin
  82. PBoard[m,j]:=1;
  83. PMat[m,j].brush.color:=clblue;
  84. end;
  85. end;
  86. if x=3 then
  87. begin
  88. for i:=m-1 to m+1 do
  89. for j:=k-1 to k+l do
  90. PBoard[i,j]:=2;
  91. for j:=k to k+l-1 do
  92. begin
  93. PBoard[m,j]:=1;
  94. PMat[m,j].brush.color:=clblue;
  95. end;
  96. end;
  97. shipsset:=true;
  98. end;
  99.  
  100. procedure PGenerateRandom;
  101. var
  102. x,l,m,k,i,j:integer;
  103. begin
  104. for i:=0 to 9 do
  105. for j:=0 to 9 do
  106. PBoard[i,j]:=0;
  107. l:=4;
  108. m:=random(9);
  109. k:=random(9);
  110. repeat
  111. x:=random(3);
  112. until PCheckForShip(m,k,x,l);
  113. PSetShip(m,k,x,l);
  114. l:=3;
  115. for i:=1 to 2 do
  116. begin
  117. repeat
  118. m:=random(9);
  119. k:=random(9);
  120. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  121. repeat
  122. x:=random(3);
  123. until PCheckForShip(m,k,x,l);
  124. PSetShip(m,k,x,l);
  125. end;
  126. l:=2;
  127. for i:=1 to 3 do
  128. begin
  129. repeat
  130. m:=random(9);
  131. k:=random(9);
  132. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  133. repeat
  134. x:=random(3);
  135. until PCheckForShip(m,k,x,l);
  136. PSetShip(m,k,x,l);
  137. end;
  138. l:=1;
  139. for i:=1 to 4 do
  140. begin
  141. begin
  142. repeat
  143. m:=random(9);
  144. k:=random(9);
  145. until PCheckForShip(m,k,0,l) OR PCheckForShip(m,k,1,l) OR PCheckForShip(m,k,2,l) OR PCheckForShip(m,k,3,l);
  146. repeat
  147. x:=random(3);
  148. until PCheckForShip(m,k,x,l);
  149. PSetShip(m,k,x,l);
  150. end;
  151.  
  152.  
  153.  
  154. end;
  155.  
  156. end;
  157. function cCheckForShip(m,k,x,l:integer):boolean;
  158. var ok:boolean;
  159. br,i,j:integer;
  160.  
  161. begin
  162. ok:=false;
  163. if x=0 then
  164. begin
  165. br:=0;
  166. for i:=m-l+1 to m do
  167. if cBoard[i,k]=0 then
  168. br:=br+1;
  169. if br=l then
  170. ok:=true;
  171. end;
  172.  
  173. if x=1 then
  174. begin
  175. br:=0;
  176. for i:=m to m+l-1 do
  177. if cBoard[i,k]=0 then
  178. br:=br+1;
  179. if br=l then
  180. ok:=true;
  181. end;
  182.  
  183. if x=2 then
  184. begin
  185. br:=0;
  186. for j:=k-l+1 to k do
  187. if cBoard[m,j]=0 then
  188. br:=br+1;
  189. if br=l then
  190. ok:=true;
  191. end;
  192.  
  193. if x=3 then
  194. begin
  195. br:=0;
  196. for j:=k to k+l-1 do
  197. if cBoard[m,j]=0 then
  198. br:=br+1;
  199. if br=l then
  200. ok:=true;
  201. end;
  202.  
  203. cCheckForShip:=ok;
  204. end;
  205. procedure cSetShip(m,k,x,l:integer);
  206. var i,j:integer;
  207. begin
  208. if x=0 then
  209. begin
  210. for i:=m-l to m+1 do
  211. for j:=k-1 to k+1 do
  212. cBoard[i,j]:=2;
  213. for i:=m-l+1 to m do
  214.  
  215. cBoard[i,k]:=1;
  216.  
  217. end;
  218. if x=1 then
  219. begin
  220. for i:=m-1 to m+l do
  221. for j:=k-1 to k+1 do
  222. cBoard[i,j]:=2;
  223. for i:=m to m+l-1 do
  224.  
  225. cBoard[i,k]:=1;
  226.  
  227. end;
  228. if x=2 then
  229. begin
  230. for i:=m-1 to m+1 do
  231. for j:=k-l to k+1 do
  232. cBoard[i,j]:=2;
  233. for j:=k-l+1 to k do
  234.  
  235. cBoard[m,j]:=1;
  236.  
  237. end;
  238. if x=3 then
  239. begin
  240. for i:=m-1 to m+1 do
  241. for j:=k-1 to k+l do
  242. cBoard[i,j]:=2;
  243. for j:=k to k+l-1 do
  244. cBoard[m,j]:=1;
  245. end;
  246. shipsset:=true;
  247. end;
  248. procedure cGenerateRandom;
  249. var
  250. x,l,m,k,i,j:integer;
  251. begin
  252. for i:=0 to 9 do
  253. for j:=0 to 9 do
  254. cBoard[i,j]:=0;
  255. l:=4;
  256. m:=random(9);
  257. k:=random(9);
  258. repeat
  259. x:=random(3);
  260. until cCheckForShip(m,k,x,l);
  261. cSetShip(m,k,x,l);
  262. l:=3;
  263. for i:=1 to 2 do
  264. begin
  265. repeat
  266. m:=random(9);
  267. k:=random(9);
  268. until cCheckForShip(m,k,0,l) OR cCheckForShip(m,k,1,l) OR cCheckForShip(m,k,2,l) OR cCheckForShip(m,k,3,l);
  269. repeat
  270. x:=random(3);
  271. until cCheckForShip(m,k,x,l);
  272. cSetShip(m,k,x,l);
  273. end;
  274. l:=2;
  275. for i:=1 to 3 do
  276. begin
  277. repeat
  278. m:=random(9);
  279. k:=random(9);
  280. until cCheckForShip(m,k,0,l) OR cCheckForShip(m,k,1,l) OR cCheckForShip(m,k,2,l) OR cCheckForShip(m,k,3,l);
  281. repeat
  282. x:=random(3);
  283. until cCheckForShip(m,k,x,l);
  284. cSetShip(m,k,x,l);
  285. end;
  286. l:=1;
  287. for i:=1 to 4 do
  288. begin
  289. begin
  290. repeat
  291. m:=random(9);
  292. k:=random(9);
  293. until cCheckForShip(m,k,0,l) OR cCheckForShip(m,k,1,l) OR cCheckForShip(m,k,2,l) OR cCheckForShip(m,k,3,l);
  294. repeat
  295. x:=random(3);
  296. until cCheckForShip(m,k,x,l);
  297. cSetShip(m,k,x,l);
  298. end;
  299.  
  300.  
  301.  
  302. end;
  303.  
  304. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement