Rod.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. Procedure RodNewTurn(cz:byte; var x,y : byte);far;
  2. Var
  3. x1,y1,PR,i,max,MAXX,MAXY,MAXI,MAXprX,MAXprY,MAXprI: byte;
  4. maxp,maxprp : integer;
  5. ar : array [1..20] of byte;
  6. apr : array [1..20] of byte;
  7. found,tmp,tmpPR,FoundPR,KON : boolean;
  8. Procedure FindY(x1,y1,d:byte);
  9. begin
  10. case d of
  11. 5: if (pole[x1,y1-1]=0) then begin x:=x1;y:=y1-1 end else
  12. if (pole[x1,y1-2]=0) then begin x:=x1;y:=y1-2 end else
  13. if (pole[x1,y1-3]=0) then begin x:=x1;y:=y1-3 end else
  14. begin x:=x1;y:=y1-4 end;
  15. 4: if (pole[x1,y1-1]=0) then begin x:=x1;y:=y1-1 end else
  16. if (pole[x1,y1+1]=0) then begin x:=x1;y:=y1+1 end else
  17. if (pole[x1,y1-2]=0) then begin x:=x1;y:=y1-2 end else
  18. begin x:=x1;y:=y1-3 end;
  19. 3: if (pole[x1,y1-1]=0) then begin x:=x1;y:=y1-1 end else
  20. if (pole[x1,y1+1]=0) then begin x:=x1;y:=y1+1 end else
  21. if (pole[x1,y1-2]=0) then begin x:=x1;y:=y1-2 end else
  22. begin x:=x1;y:=y1+2 end;
  23. 2: if (pole[x1,y1-1]=0) then begin x:=x1;y:=y1-1 end else
  24. if (pole[x1,y1+1]=0) then begin x:=x1;y:=y1+1 end else
  25. if (pole[x1,y1+2]=0) then begin x:=x1;y:=y1+2 end else
  26. begin x:=x1;y:=y1+3 end;
  27. 1: if (pole[x1,y1+1]=0) then begin x:=x1;y:=y1+1 end else
  28. if (pole[x1,y1+2]=0) then begin x:=x1;y:=y1+2 end else
  29. if (pole[x1,y1+3]=0) then begin x:=x1;y:=y1+3 end else
  30. begin x:=x1;y:=y1+4 end;
  31. end;
  32. end;
  33. Procedure FindX(x1,y1,d:byte);
  34. begin
  35. case d of
  36. 5: if (pole[x1-1,y1]=0) then begin x:=x1-1;y:=y1 end else
  37. if (pole[x1-2,y1]=0) then begin x:=x1-2;y:=y1 end else
  38. if (pole[x1-3,y1]=0) then begin x:=x1-3;y:=y1 end else
  39. begin x:=x1-4;y:=y1-4 end;
  40. 4: if (pole[x1-1,y1]=0) then begin x:=x1-1;y:=y1 end else
  41. if (pole[x1+1,y1]=0) then begin x:=x1+1;y:=y1 end else
  42. if (pole[x1-2,y1]=0) then begin x:=x1-2;y:=y1 end else
  43. begin x:=x1-3;y:=y1 end;
  44. 3: if (pole[x1-1,y1]=0) then begin x:=x1-1;y:=y1 end else
  45. if (pole[x1+1,y1]=0) then begin x:=x1+1;y:=y1 end else
  46. if (pole[x1-2,y1]=0) then begin x:=x1-2;y:=y1 end else
  47. begin x:=x1+2;y:=y1 end;
  48. 2: if (pole[x1-1,y1]=0) then begin x:=x1-1;y:=y1 end else
  49. if (pole[x1+1,y1]=0) then begin x:=x1+1;y:=y1 end else
  50. if (pole[x1+2,y1]=0) then begin x:=x1+2;y:=y1 end else
  51. begin x:=x1+3;y:=y1 end;
  52. 1: if (pole[x1+1,y1]=0) then begin x:=x1+1;y:=y1 end else
  53. if (pole[x1+2,y1]=0) then begin x:=x1+2;y:=y1 end else
  54. if (pole[x1+3,y1]=0) then begin x:=x1+3;y:=y1 end else
  55. begin x:=x1+4;y:=y1 end;
  56. end;
  57. end;
  58. Procedure FindYX(x1,y1,d:byte);
  59. begin
  60. case d of
  61. 5: if (pole[x1-1,y1-1]=0) then begin x:=x1-1;y:=y1-1 end else
  62. if (pole[x1-2,y1-2]=0) then begin x:=x1-2;y:=y1-2 end else
  63. if (pole[x1-3,y1-3]=0) then begin x:=x1-3;y:=y1-3 end else
  64. begin x:=x1-4;y:=y1-4 end;
  65. 4: if (pole[x1-1,y1-1]=0) then begin x:=x1-1;y:=y1-1 end else
  66. if (pole[x1+1,y1+1]=0) then begin x:=x1+1;y:=y1+1 end else
  67. if (pole[x1-2,y1-2]=0) then begin x:=x1-2;y:=y1-2 end else
  68. begin x:=x1-3;y:=y1-3 end;
  69. 3: if (pole[x1-1,y1-1]=0) then begin x:=x1-1;y:=y1-1 end else
  70. if (pole[x1+1,y1+1]=0) then begin x:=x1+1;y:=y1+1 end else
  71. if (pole[x1-2,y1-2]=0) then begin x:=x1-2;y:=y1-2 end else
  72. begin x:=x1+2;y:=y1+2 end;
  73. 2: if (pole[x1-1,y1-1]=0) then begin x:=x1-1;y:=y1-1 end else
  74. if (pole[x1+1,y1+1]=0) then begin x:=x1+1;y:=y1+1 end else
  75. if (pole[x1+2,y1+2]=0) then begin x:=x1+2;y:=y1+2 end else
  76. begin x:=x1+3;y:=y1+3 end;
  77. 1: if (pole[x1+1,y1+1]=0) then begin x:=x1+1;y:=y1+1 end else
  78. if (pole[x1+2,y1+2]=0) then begin x:=x1+2;y:=y1+2 end else
  79. if (pole[x1+3,y1+3]=0) then begin x:=x1+3;y:=y1+3 end else
  80. begin x:=x1+4;y:=y1+4 end;
  81. end;
  82. end;
  83. Procedure Find_YX(x1,y1,d:byte);
  84. begin
  85. case d of
  86. 5: if (pole[x1+1,y1-1]=0) then begin x:=x1+1;y:=y1-1 end else
  87. if (pole[x1+2,y1-2]=0) then begin x:=x1+2;y:=y1-2 end else
  88. if (pole[x1+3,y1-3]=0) then begin x:=x1+3;y:=y1-3 end else
  89. begin x:=x1+4;y:=y1-4 end;
  90. 4: if (pole[x1+1,y1-1]=0) then begin x:=x1+1;y:=y1-1 end else
  91. if (pole[x1-1,y1+1]=0) then begin x:=x1-1;y:=y1+1 end else
  92. if (pole[x1+2,y1-2]=0) then begin x:=x1+2;y:=y1-2 end else
  93. begin x:=x1+3;y:=y1-3 end;
  94. 3: if (pole[x1+1,y1-1]=0) then begin x:=x1+1;y:=y1-1 end else
  95. if (pole[x1-1,y1+1]=0) then begin x:=x1-1;y:=y1+1 end else
  96. if (pole[x1+2,y1-2]=0) then begin x:=x1+2;y:=y1-2 end else
  97. begin x:=x1-2;y:=y1+2 end;
  98. 2: if (pole[x1+1,y1-1]=0) then begin x:=x1+1;y:=y1-1 end else
  99. if (pole[x1-1,y1+1]=0) then begin x:=x1-1;y:=y1+1 end else
  100. if (pole[x1-2,y1+2]=0) then begin x:=x1-2;y:=y1+2 end else
  101. begin x:=x1-3;y:=y1+3 end;
  102. 1: if (pole[x1-1,y1+1]=0) then begin x:=x1-1;y:=y1+1 end else
  103. if (pole[x1-2,y1+2]=0) then begin x:=x1-2;y:=y1+2 end else
  104. if (pole[x1-3,y1+3]=0) then begin x:=x1-3;y:=y1+3 end else
  105. begin x:=x1-4;y:=y1+4 end;
  106. end;
  107. end;
  108. Procedure AnalY(x1,y1:byte;c:boolean);
  109. var pr,cz1,j,i : byte;
  110. begin
  111. if c then cz1 := cz else cz1:=3-cz;
  112. if c then pr := 3-cz else pr := cz;
  113. for i := 5 downto 1 do
  114. if (pole[x1,y1-i+1]<>pr) and
  115. (pole[x1,y1-i+2]<>pr) and
  116. (pole[x1,y1-i+3]<>pr) and (y1-i+1>0) and (y1+5-i<n+1) and
  117. (pole[x1,y1-i+4]<>pr) and
  118. (pole[x1,y1-i+5]<>pr) then
  119. begin
  120. tmp := true;
  121. for j := 1 to 5 do if pole[x1,y1-i+j] = cz1 then
  122. if c then inc(ar[i]) else inc(aPr[i]);
  123. { if not c then
  124. begin
  125. if pole[x1,y1-i] = pr then dec(aPr[i]) else
  126. if pole[x1,y1-i+6] = pr then dec(aPr[i]);
  127. end;}
  128. end;
  129. end;
  130. Procedure AnalX(x1,y1:byte;c:boolean);
  131. var pr,cz1,j,i : byte;
  132. begin
  133. if c then cz1 := cz else cz1:=3-cz;
  134. if c then pr := 3-cz else pr := cz;
  135. for i := 5 downto 1 do
  136. if (pole[x1-i+1,y1]<>pr) and
  137. (pole[x1-i+2,y1]<>pr) and
  138. (pole[x1-i+3,y1]<>pr) and (x1-i+1>0) and (x1+5-i<n+1) and
  139. (pole[x1-i+4,y1]<>pr) and
  140. (pole[x1-i+5,y1]<>pr) then
  141. begin
  142. tmp := true;
  143. for j := 1 to 5 do if pole[x1-i+j,y1] = cz1 then
  144. if c then inc(ar[5+i]) else inc(aPr[5+i]);
  145. { if not c then
  146. begin
  147. if pole[x1-i,y1] = pr then dec(aPr[5+i]) else
  148. if pole[x1-i+6,y1] = pr then dec(aPr[5+i]);
  149. end;}
  150. end;
  151. end;
  152. Procedure AnalYX(x1,y1:byte;c:boolean);
  153. var pr,cz1,j,i : byte;
  154. begin
  155. if c then cz1 := cz else cz1:=3-cz;
  156. if c then pr := 3-cz else pr := cz;
  157. for i := 5 downto 1 do
  158. if (pole[x1-i+1,y1-i+1]<>pr) and
  159. (pole[x1-i+2,y1-i+2]<>pr) and
  160. (pole[x1-i+3,y1-i+3]<>pr) and (x1-i+1>0) and (x1+5-i<n+1) and
  161. (pole[x1-i+4,y1-i+4]<>pr) and (y1-i+1>0) and (y1+5-i<n+1) and
  162. (pole[x1-i+5,y1-i+5]<>pr) then
  163. begin
  164. tmp := true;
  165. for j := 1 to 5 do if pole[x1-i+j,y1-i+j] = cz1 then
  166. if c then inc(ar[10+i]) else inc(aPr[10+i]);
  167. { if not c then
  168. begin
  169. if pole[x1-i,y1-i] = pr then dec(aPr[10+i]) else
  170. if pole[x1-i+6,y1-i+6] = pr then dec(aPr[10+i]);
  171. end;}
  172. end;
  173. end;
  174. Procedure Anal_YX(x1,y1:byte;c:boolean);
  175. var pr,cz1,j,i : byte;
  176. begin
  177. if c then cz1 := cz else cz1:=3-cz;
  178. if c then pr := 3-cz else pr := cz;
  179. for i := 5 downto 1 do
  180. if (pole[x1+i-1,y1-i+1]<>pr) and
  181. (pole[x1+i-2,y1-i+2]<>pr) and
  182. (pole[x1+i-3,y1-i+3]<>pr) and (x1+i-1<n+1) and (x1-5+i>0) and
  183. (pole[x1+i-4,y1-i+4]<>pr) and (y1-i+1>0) and (y1+5-i<n+1) and
  184. (pole[x1+i-5,y1-i+5]<>pr) then
  185. begin
  186. tmp := true;
  187. for j := 1 to 5 do if pole[x1+i-j,y1-i+j] = cz1 then
  188. if c then inc(ar[15+i]) else inc(aPr[15+i]);
  189. { if not c then
  190. begin
  191. if pole[x1+i,y1-i] = pr then dec(aPr[15+i]) else
  192. if pole[x1+i-6,y1-i+6] = pr then dec(aPr[15+i]);
  193. end;}
  194. end;
  195. end;
  196. Begin
  197. randomize;
  198. pr := 3 - cz;
  199. found := false;
  200. for i := 1 to 20 do ar[i] := 0;
  201. for i := 1 to 20 do apr[i] := 0;
  202. FoundPR := false;
  203. MAXP := 0;
  204. MAXprP := 0;
  205. KON := false;
  206. for y1 := 1 to n do
  207. for x1 := 1 to n do
  208. if Pole[x1,y1] = cz then
  209. begin
  210. if not kon then begin
  211. tmp := false;
  212. AnalX(x1,y1,true);
  213. AnalY(x1,y1,true);
  214. AnalYX(x1,y1,true);
  215. Anal_YX(x1,y1,true);
  216. if tmp then
  217. begin
  218. max := 1;
  219. for i := 2 to 20 do if ar[i] > ar[max] then max := i;
  220. if (ar[max] > MAXP) or ((ar[max] = MAXP) and (Random(1)=1)) then
  221. begin
  222. MAXP := ar[max];
  223. MAXI := max;
  224. MAXX := x1;
  225. MAXY := y1;
  226. if maxp > 3 then begin KON := true; FoundPr := false; end;
  227. found := true;
  228. end;
  229. for i := 1 to 20 do ar[i] := 0;
  230. end;
  231. end;
  232. end else if pole[x1,y1] = PR then if not foundPR then
  233. begin
  234. if not kon then begin
  235. tmp := false;
  236. AnalX(x1,y1,false);
  237. AnalY(x1,y1,false);
  238. AnalYX(x1,y1,false);
  239. Anal_YX(x1,y1,false);
  240. if tmp then
  241. begin
  242. max := 0;
  243. for i := 1 to 20 do if apr[i] > 2 then max := i;
  244. if max <> 0 then
  245. if (apr[max] > MAXprP) or ((apr[max]=MAXprP) and (random(1)=1)) then
  246. begin
  247. MAXprP := apr[max];
  248. MAXprI := max;
  249. MAXprX := x1;
  250. MAXprY := y1;
  251. foundPR := true;
  252. Found := true;
  253. end;
  254. for i := 1 to 20 do apr[i] := 0;
  255. end;
  256. end;
  257. end;
  258. if not found then
  259. repeat
  260. x:=random(n)+1;
  261. y:=random(n)+1;
  262. until Pole[x,y] = 0
  263. else if foundPR then
  264. begin
  265. if (maxprI in [1..5]) then FindY(maxprx,maxpry,maxprI) else
  266. if (maxprI in [6..10]) then FindX(maxprx,maxpry,maxprI-5) else
  267. if (maxprI in [11..15]) then FindYX(maxprx,maxpry,maxprI-10) else
  268. if (maxprI in [16..20]) then Find_YX(maxprx,maxpry,maxprI-15)
  269. end
  270. else
  271. if (maxI<6) then FindY(MAXx,MAXy,maxI) else
  272. if (maxI in [6..10]) then FindX(MAXx,MAXy,maxI-5) else
  273. if (maxI in [11..15]) then FindYX(MAXx,MAXy,maxI-10) else
  274. Find_YX(MAXx,MAXy,maxI-15);
  275. End;