XO.PAS 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. uses CRT;
  2. Const
  3. n = 20;
  4. NameX = 'Rod2';
  5. NameO = 'Rod';
  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 = 5*25*2;
  15. DelX = round(TimeT/Cell/2);
  16. DelO = round(TimeT/360);
  17. Type TPole = array [-4..n+4,-4..n+4] of byte;
  18. TurnProc = procedure(cx:byte;var x,y:byte);
  19. PointType = record
  20. x,y : byte;
  21. end;
  22. Var
  23. Pole : TPole;
  24. TP : Tpole;
  25. Ex : boolean;
  26. Win : byte;
  27. xc,yc : byte;
  28. count : 0..n*n+1;
  29. WinX,WinO,Games,i,j : word;
  30. ENDS : array [1..5] of pointtype;
  31. TurnX : TurnProc;
  32. TurnO : TurnProc;
  33. TurnT : turnProc;
  34. {$I Rod.inc}
  35. {$I GRISHA1.inc}
  36. {---------------------------}
  37. Function IsWin(x,y:byte):byte;
  38. var
  39. x1,y1 : byte;
  40. k1,k2,l1,l2 : byte;
  41. res : byte;
  42. begin
  43. res := 0;
  44. if x-4<1 then k1 := 1 else k1 := x - 4;
  45. if x+4>n then k2 := n else k2 := x + 4;
  46. if y-4<1 then l1 := 1 else l1 := y - 4;
  47. if y+4>n then l2 := n else l2 := y + 4;
  48. for x1 := k1 to k2 do
  49. for y1 := l1 to l2 do
  50. if (pole[x1,y1]<>0) then begin
  51. if ((pole[x1,y1]=pole[x1,y1-2]) and
  52. (pole[x1,y1]=pole[x1,y1-1]) and
  53. (pole[x1,y1]=pole[x1,y1+1]) and
  54. (pole[x1,y1]=pole[x1,y1+2])) then res := pole[x1,y1];
  55. if ((pole[x1,y1]=pole[x1-2,y1]) and
  56. (pole[x1,y1]=pole[x1-1,y1]) and
  57. (pole[x1,y1]=pole[x1+1,y1]) and
  58. (pole[x1,y1]=pole[x1+2,y1])) then res := pole[x1,y1];
  59. if ((pole[x1,y1]=pole[x1-2,y1-2]) and
  60. (pole[x1,y1]=pole[x1-1,y1-1]) and
  61. (pole[x1,y1]=pole[x1+1,y1+1]) and
  62. (pole[x1,y1]=pole[x1+2,y1+2])) then res := pole[x1,y1];
  63. if ((pole[x1,y1]=pole[x1+2,y1-2]) and
  64. (pole[x1,y1]=pole[x1+1,y1-1]) and
  65. (pole[x1,y1]=pole[x1-1,y1+1]) and
  66. (pole[x1,y1]=pole[x1-2,y1+2])) then res := pole[x1,y1];
  67. end;
  68. IsWin := Res;
  69. end;{IsWin}
  70. {---------------------------}
  71. Function IsLeg(x,y:byte):boolean;
  72. var res:boolean; i,j : byte;
  73. begin
  74. res := true;
  75. if not (x in [1..n]) then
  76. res := false;
  77. if not (y in [1..n]) then
  78. res := false;
  79. for i := 1 to n do for j := 1 to n do if pole[i,j] <> tp[i,j] then
  80. res := false;
  81. if tp[x,y] <> 0 then
  82. res := false;
  83. pole := tp;
  84. IsLeg:=res;
  85. end;{IsLeg}
  86. {-------------------------}
  87. Procedure GameOver(c : byte);
  88. Var
  89. sc,s1 : string;
  90. Begin
  91. Case c of
  92. 1: inc(Winx);
  93. 2: inc(WinO);
  94. 3: inc(WinO);
  95. 4: inc(Winx);
  96. end;
  97. Ex := true;
  98. End;{GameOver}
  99. {------------------------}
  100. const
  101. GA = 1000;
  102. BEGIN
  103. TurnX:=GRISHA1;
  104. TurnO:=RodNewTurn;
  105. ClrScr;
  106. Write('WinX:');
  107. for games := 1 to ga do begin
  108. for i := 1 to n do for j:= 1 to n do pole[i,j] :=0;
  109. ex := false;
  110. count := 0;
  111. repeat
  112. tp := pole;
  113. TurnX(1,xc,yc);
  114. if not isLeg(xc,yc) then GameOver(3)
  115. else begin
  116. inc(count);
  117. Pole[xc,yc] := 1;
  118. win := iswin(xc,yc);
  119. if (count=n*n) then GameOver(5) else begin
  120. if Win <> 0 then GameOver(1) else
  121. begin
  122. tp := pole;
  123. TurnO(2,xc,yc);
  124. if not isLeg(xc,yc) then gameover(4)
  125. else
  126. begin
  127. Pole[xc,yc] := 2;
  128. Inc(count);
  129. win := iswin(xc,yc);
  130. if (count=n*n) then GameOver(5) else begin
  131. if Win <> 0 then GameOver(2);
  132. end;
  133. end;
  134. end;
  135. end;
  136. end;
  137. until Ex;
  138. TurnT := TurnX;
  139. TurnX := TurnO;
  140. TurnO := TurnT;
  141. WinX := WinX xor WinO;
  142. WinO := WinX xor WinO;
  143. WinX := WinX xor WinO;
  144. end;
  145. Write(WinX:4,' WinO: ',WinO:4);
  146. WriteLn;
  147. WriteLn(WinX / ga * 100:2:0,'%');
  148. ReadKey;
  149. END.