ROD3.INC 11 KB

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