MY_XO.PAS 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. uses GRAPH,CRT,VGA;
  2. Const
  3. n = 10;
  4. NameX = 'Hel';
  5. NameO = 'Hel2';
  6. ColorX = LightBlue;
  7. ColorO = LightRed;
  8. PoleY1 = 110;
  9. PoleL = 460-PoleY1;
  10. Cell = PoleL / n;
  11. PoleX1 = round(320 - (Cell*n) / 2);
  12. PoleX2 = round(320 + (Cell*n) / 2);
  13. PoleY2 = round(PoleY1+n*cell);
  14. TimeT = 15*25*2;
  15. DelX = round(TimeT/Cell/2);
  16. DelO = round(TimeT/360);
  17. Type TPole = array [1..n,1..n] of byte;
  18. Var
  19. Pole : TPole;
  20. TP : Tpole;
  21. Ex : boolean;
  22. Win : byte;
  23. xc,yc : byte;
  24. count : 0..n*n+1;
  25. ENDS : array [1..5] of pointtype;
  26. {$I Turn1.inc}
  27. {$I Turn2.inc}
  28. Type TurnProc = procedure(cx:byte;var x,y:byte);
  29. const
  30. TurnX : TurnProc=Turn1;
  31. TurnO : TurnProc=Turn2;
  32. {---------------------------}
  33. Procedure Init;
  34. Var
  35. i,j : integer;
  36. Begin
  37. for i := -4 to n+4 do for j:= -4 to n+4 do pole[i,j] :=0;
  38. InitVGA;
  39. SetTextStyle(TriplexFont,HORIZDIR,4);
  40. SetTextJustify(CenterText,BottomText);
  41. SetFillStyle(XHATCHFILL,Blue);
  42. Bar(0,0,GetMaxX,GetMaxY);
  43. SetFillStyle(SolidFill,Black);
  44. Bar(10,10,630,470);
  45. SetCOlor(White);
  46. OutTextXY(320,40,'�¨â¢  ¯à®£à ¬¬.');
  47. OutTextXY(320,40+TextHeight('�')+5,'Šà¥á⨪¨-�®«¨ª¨.');
  48. SetColor(ColorX);
  49. SetTextJustify(LeftText,BottomText);
  50. OutTextXY(10,100,NameX);
  51. SetLineStyle(SolidLn,0,ThickWidth);
  52. Line(30,20,70,60);
  53. Line(70,20,30,60);
  54. SetColor(ColorO);
  55. Arc(590,40,0,360,20);
  56. SetTextJustify(RightText,BottomText);
  57. OutTextXY(630,100,NameO);
  58. SetLineStyle(SolidLn,0,NormWidth);
  59. SetColor(LightGray);
  60. { Rectangle(PoleX1,PoleY1,PoleX2,PoleY2);}
  61. For i := 0 to n do begin
  62. Line(round(PoleX1+Cell*i),PoleY1,round(PoleX1+Cell*i),PoleY2);
  63. Line(PoleX1,round(PoleY1+Cell*i),PoleX2,round(PoleY1+Cell*i));
  64. end;
  65. End;{Init}
  66. {--------------------------}
  67. Procedure DrawTurn(cn,x,y:byte);
  68. Var
  69. i,size : integer;
  70. Begin
  71. if cn = 1 then
  72. begin
  73. SetColor(ColorX);
  74. size := round(cell-2)-1;
  75. MoveTo(round(PoleX1+(x-1)*Cell+1),round(PoleY1+(y-1)*Cell+1));
  76. For i := 1 to size do begin LineRel(1,1);delay(delX);end;
  77. MoveTo(round(PoleX1+x*Cell-1),round(PoleY1+(y-1)*Cell+1));
  78. For i := 1 to size do begin LineRel(-1,1);delay(delX);end;
  79. Pole[x,y] := 1;
  80. end
  81. else
  82. begin
  83. setColor(ColorO);
  84. For i := 1 to 360 do begin
  85. Arc(round(PoleX1+(x-1)*Cell+(Cell / 2)),round(PoleY1+(y-1)*Cell+(Cell / 2))
  86. ,0,i,round((cell-2) / 2-1));
  87. delay(DelO);
  88. end;
  89. Pole[x,y] := 2;
  90. end;
  91. End;{DrawTurn}
  92. {---------------------------}
  93. Function IsWin(x,y:byte):byte;
  94. var
  95. x1,y1 : byte;
  96. k1,k2,l1,l2 : byte;
  97. res : byte;
  98. begin
  99. res := 0;
  100. if x-4<1 then k1 := 1 else k1 := x - 4;
  101. if x+4>n then k2 := n else k2 := x + 4;
  102. if y-4<1 then l1 := 1 else l1 := y - 4;
  103. if y+4>n then l2 := n else l2 := y + 4;
  104. for x1 := k1 to k2 do
  105. for y1 := l1 to l2 do
  106. if (pole[x1,y1]<>0) then begin
  107. if ((pole[x1,y1]=pole[x1,y1-2]) and
  108. (pole[x1,y1]=pole[x1,y1-1]) and
  109. (pole[x1,y1]=pole[x1,y1+1]) and
  110. (pole[x1,y1]=pole[x1,y1+2])) then
  111. begin
  112. res := pole[x1,y1];
  113. ENDS[1].x := x1;
  114. ENDS[1].y := y1-2;
  115. ENDS[2].x := x1;
  116. ENDS[2].y := y1-1;
  117. ENDS[3].x := x1;
  118. ENDS[3].y := y1;
  119. ENDS[4].x := x1;
  120. ENDS[4].y := y1+1;
  121. ENDS[5].x := x1;
  122. ENDS[5].y := y1+2;
  123. end;
  124. if ((pole[x1,y1]=pole[x1-2,y1]) and
  125. (pole[x1,y1]=pole[x1-1,y1]) and
  126. (pole[x1,y1]=pole[x1+1,y1]) and
  127. (pole[x1,y1]=pole[x1+2,y1])) then
  128. begin
  129. res := pole[x1,y1];
  130. ENDS[1].x := x1-2;
  131. ENDS[1].y := y1;
  132. ENDS[2].x := x1-1;
  133. ENDS[2].y := y1;
  134. ENDS[3].x := x1;
  135. ENDS[3].y := y1;
  136. ENDS[4].x := x1+1;
  137. ENDS[4].y := y1;
  138. ENDS[5].x := x1+2;
  139. ENDS[5].y := y1;
  140. end;
  141. if ((pole[x1,y1]=pole[x1-2,y1-2]) and
  142. (pole[x1,y1]=pole[x1-1,y1-1]) and
  143. (pole[x1,y1]=pole[x1+1,y1+1]) and
  144. (pole[x1,y1]=pole[x1+2,y1+2])) then
  145. begin
  146. res := pole[x1,y1];
  147. ENDS[1].x := x1-2;
  148. ENDS[1].y := y1-2;
  149. ENDS[2].x := x1-1;
  150. ENDS[2].y := y1-1;
  151. ENDS[3].x := x1;
  152. ENDS[3].y := y1;
  153. ENDS[4].x := x1+1;
  154. ENDS[4].y := y1+1;
  155. ENDS[5].x := x1+2;
  156. ENDS[5].y := y1+2;
  157. end;
  158. if ((pole[x1,y1]=pole[x1+2,y1-2]) and
  159. (pole[x1,y1]=pole[x1+1,y1-1]) and
  160. (pole[x1,y1]=pole[x1-1,y1+1]) and
  161. (pole[x1,y1]=pole[x1-2,y1+2])) then
  162. begin
  163. res := pole[x1,y1];
  164. ENDS[1].x := x1+2;
  165. ENDS[1].y := y1-2;
  166. ENDS[2].x := x1+1;
  167. ENDS[2].y := y1-1;
  168. ENDS[3].x := x1;
  169. ENDS[3].y := y1;
  170. ENDS[4].x := x1-1;
  171. ENDS[4].y := y1+1;
  172. ENDS[5].x := x1-2;
  173. ENDS[5].y := y1+2;
  174. end;
  175. end;
  176. IsWin := Res;
  177. end;{IsWin}
  178. {---------------------------}
  179. Function IsLeg(x,y:byte):boolean;
  180. var res:boolean; i,j : byte;
  181. begin
  182. res := true;
  183. if not (x in [1..n]) then
  184. res := false;
  185. if not (y in [1..n]) then
  186. res := false;
  187. for i := 1 to n do for j := 1 to n do if pole[i,j] <> tp[i,j] then
  188. res := false;
  189. if tp[x,y] <> 0 then
  190. res := false;
  191. pole := tp;
  192. IsLeg:=res;
  193. end;{IsLeg}
  194. Procedure DrawWin;
  195. var i : byte;
  196. Begin
  197. SetColor(LightGreen);
  198. for i := 1 to 5 do Rectangle(round(PoleX1+(ENDS[i].x-1)*cell),
  199. round(PoleY1+(ENDS[i].y-1)*cell),
  200. round(PoleX1+(ENDS[i].x)*cell),
  201. round(PoleY1+(ENDS[i].y)*cell));
  202. End;
  203. {-------------------------}
  204. Procedure GameOver(c : byte);
  205. Var
  206. sc,s1 : string;
  207. Begin
  208. SetTextStyle(TriplexFont,HorizDir,3);
  209. SetTextJustify(1,1);
  210. str(count,sc);
  211. Case c of
  212. 1: begin
  213. DrawWin;
  214. SetColor(11);
  215. sc := '�®¡¥¤¨«  ª®¬ ­¤  '+NameX+' ­  '+sc+' 室ã';
  216. OutTextXY(320,300,sc);
  217. Ex := true;
  218. end;
  219. 2: begin
  220. DrawWin;
  221. SetColor(11);
  222. sc := '�®¡¥¤¨«  ª®¬ ­¤  '+NameO+' ­  '+sc+' 室ã';
  223. OutTextXY(320,300,sc);
  224. Ex := true;
  225. end;
  226. 3: begin
  227. sc := '�®¡¥¤¨«  ª®¬ ­¤  '+NameO;
  228. s1 := '¢á«¥¤á⢨¥ â¥å. ¯®à ¦¥­¨ï ª®¬ ­¤ë '+NameX;
  229. SetColor(11);
  230. OutTextXY(320,300,sc);
  231. OutTextXY(320,320,s1);
  232. Ex := true;
  233. end;
  234. 4: begin
  235. sc := '�®¡¥¤¨«  ª®¬ ­¤  '+NameX;
  236. s1 := '¢á«¥¤á⢨¥ â¥å. ¯®à ¦¥­¨ï ª®¬ ­¤ë '+NameO;
  237. SetColor(11);
  238. OutTextXY(320,300,sc);
  239. OutTextXY(320,320,s1);
  240. Ex := true;
  241. end;
  242. 5: begin
  243. sc := '�¨çìï!';
  244. SetColor(11);
  245. OutTextXY(320,300,sc);
  246. Ex := true;
  247. end;
  248. end;
  249. SetTextStyle(TriplexFont,HorizDir,8);
  250. SetColor(Yellow);
  251. OutTextXY(320,200,'ˆ£à  Žª®­ç¥­ !');
  252. End;{GameOver}
  253. {------------------------}
  254. BEGIN
  255. Init;
  256. ex := false;
  257. count := 0;
  258. repeat
  259. if KeyPressed then begin if ReadKey = #27 then GameOver(5) end else begin
  260. tp := pole;
  261. TurnX(1,xc,yc);
  262. if not isLeg(xc,yc) then GameOver(3)
  263. else begin
  264. inc(count);
  265. DrawTurn(1,xc,yc);
  266. win := iswin(xc,yc);
  267. if (count=n*n) then GameOver(5) else begin
  268. if Win <> 0 then GameOver(1) else
  269. begin
  270. tp := pole;
  271. TurnO(2,xc,yc);
  272. if not isLeg(xc,yc) then gameover(4)
  273. else
  274. begin
  275. DrawTurn(2,xc,yc);
  276. Inc(count);
  277. win := iswin(xc,yc);
  278. if (count=n*n) then GameOver(5) else begin
  279. if Win <> 0 then GameOver(2);
  280. end;
  281. end;
  282. end;
  283. end;
  284. end;
  285. end;
  286. until Ex;
  287. ReadKey;
  288. CloseGraph;
  289. END.