Guest User

chess algorithm

a guest
May 11th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.78 KB | None | 0 0
  1. {*****************************
  2.  * created by Kirill Halo    *
  3.  * https://vk.com/itmentor   *
  4.  *****************************}
  5.  
  6. program Chess;
  7. uses crt, graphABC;
  8.  
  9. const FLAG_ATTACK = -1;
  10. const LocationFigures : array[0..7,0..7] of integer =((0,0,0,0,0,0,0,0),
  11.                                                       (0,0,0,0,0,0,0,0),
  12.                                                       (0,0,1,0,0,0,0,0),
  13.                                                       (0,0,0,0,0,0,0,0),
  14.                                                       (0,0,0,0,0,0,0,0),
  15.                                                       (0,0,0,0,0,1,1,0),
  16.                                                       (0,0,0,0,0,0,0,0),
  17.                                                       (0,0,0,0,0,0,0,0));
  18.  
  19.  
  20. type TMatr = array[0..7, 0..7] of integer;
  21.  
  22. var
  23.     PlacesAttackFigures : TMatr;
  24.         xCenter : integer;
  25.         yCenter : integer;
  26.      WidthBoard : integer;
  27.     HeightBoard : integer;
  28.      XCellCount : integer;
  29.      YCellCount : integer;
  30.       WidthCell : integer;
  31.      HeightCell : integer;
  32.      CountCell  : integer;
  33.  
  34.  
  35.  
  36.  
  37. {Инициализация начальных параметров}
  38. procedure InitializationInitialParameters;
  39. begin
  40.    xCenter := WindowWidth div 2;
  41.    yCenter := WindowHeight div 2;
  42.    setPixel(xCenter, yCenter, clGray);
  43.    WidthBoard := 300;
  44.    HeightBoard := 300;
  45.    XCellCount := 8;
  46.    YCellCount := 8;
  47.    WidthCell := WidthBoard div XCellCount;
  48.    HeightCell := HeightBoard div YCellCount;
  49.    CountCell := XCellCount * YCellCount;
  50. end;
  51.  
  52.  
  53.  
  54.  
  55. { Заполнение матрицы занятых(ударных) мест}
  56. procedure FillPlacesAttackFigures(var PlacesAttackFigures : TMatr;
  57.                                        RowCount, ColCount : integer);
  58. var ix, iy : integer;
  59.     attack_x, attack_y : integer;
  60. begin
  61.     for iy := 0 to RowCount - 1 do
  62.       for ix := 0 to ColCount - 1 do
  63.          PlacesAttackFigures[iy, ix] := 0;
  64.            
  65.     for iy := 0 to RowCount - 1 do
  66.       for ix := 0 to ColCount - 1 do
  67.         begin
  68.            if(LocationFigures[iy, ix] = 1) then
  69.               begin
  70.                  for attack_y := 0 to RowCount - 1 do
  71.                     PlacesAttackFigures[attack_y, ix] := FLAG_ATTACK;
  72.                  for attack_x := 0 to ColCount - 1 do
  73.                     PlacesAttackFigures[iy, attack_x] := FLAG_ATTACK;
  74.               end;
  75.         end;
  76. end;
  77.  
  78.  
  79.  
  80.  
  81. {Метод, возвращается число свободных мест}
  82. function GetCountFreePlaces(PlacesAttackFigures : TMatr;
  83.                              RowCount, ColCount : integer):integer;
  84. var ix, iy : integer;
  85.      count : integer;
  86. begin
  87.     count := 0;
  88.     for iy := 0 to RowCount - 1 do
  89.       for ix := 0 to ColCount - 1 do
  90.          if(PlacesAttackFigures[iy, ix] <> FLAG_ATTACK) then inc(count);
  91.     result := count;
  92. end;
  93.  
  94.  
  95.  
  96.  
  97. {Рисование одной ячейки
  98.  1 - цвет серый
  99.  0 - цвет белый
  100.  FLAG_ATTACK - цвет красный}
  101. procedure DrawingSingleCell(x, y : integer; c : integer);
  102. begin
  103.     if (c = 1) then
  104.        SetBrushColor(clGray)
  105.     else if (c = 0) then
  106.        SetBrushColor(clWhite)
  107.     else if (c = FLAG_ATTACK) then
  108.        SetBrushColor(RGB(122,0,0))
  109.     else
  110.        write('Error!');
  111.     Rectangle(x, y, x + WidthCell, y + HeightCell);
  112. end;
  113.  
  114.  
  115.  
  116.  
  117. {Рисование ладьи в клетке с координатой (x;y) }
  118. procedure DrawingRookInCell(x, y : integer);
  119. var d:integer;
  120. begin
  121.     SetBrushColor(clBlack);
  122.     d := 10;
  123.     Rectangle(x + d, y + d, x + 4 + d, y + 4 + d );
  124.     Rectangle(x + 8 + d, y + d, x + 12 + d, y + 4 + d);
  125.     Rectangle(x + 16 + d, y + d, x + 20 + d, y + 4 + d);
  126.     Rectangle(x + d, y + 4 + d, x + 20 + d, y + 10 + d);
  127.     Rectangle(x + 4 + d, y + 10 + d, x + 16 + d, y + 20 + d);
  128.     Rectangle(x + d, y + 20 + d, x + 20 + d, y + 26 + d);
  129. end;
  130.  
  131.  
  132.  
  133.  
  134. {Рисование доски}
  135. procedure DrawBoard;
  136. var x, y : integer;
  137.     x0, y0 : integer;
  138.     k, iy, ix : integer;
  139. begin
  140.    //Setbrushstyle(bsClear);
  141.    SetPenColor(clBlack);
  142.    Rectangle(xCenter - WidthBoard div 2, yCenter - HeightBoard div 2,
  143.              xCenter + WidthBoard div 2, yCenter + HeightBoard div 2);
  144.  
  145.    x0 := xCenter - WidthBoard div 2;
  146.    y0 := yCenter - HeightBoard div 2;
  147.  
  148.    for k := 0 to CountCell - 1 do
  149.    begin
  150.       iy := k div XCellCount; ix := k mod XCellCount;
  151.       x := x0 + ix * WidthCell + 2;
  152.       y := y0 + iy * HeightCell + 2;
  153.      
  154.       if(PlacesAttackFigures[iy, ix] = FLAG_ATTACK ) then
  155.           DrawingSingleCell(x, y, FLAG_ATTACK)           //Запрещенные ячейки
  156.       else
  157.           DrawingSingleCell(x, y, (ix + iy + 1) mod 2); //цвет меняется через остаток по модулю 2
  158.      
  159.    end;
  160.  
  161. end;
  162.  
  163.  
  164.  
  165.  
  166. {Расстановка фигур, согласно матрице}
  167. procedure PlacementOfFigures(arr : TMatr; RowCount, ColCount : integer);
  168. var ix, iy : integer;
  169.     x0, y0 : integer;
  170.     x, y : integer;
  171. begin
  172.    x0 := xCenter - WidthBoard div 2;
  173.    y0 := yCenter - HeightBoard div 2;
  174.     for ix := 0 to RowCount - 1 do
  175.       for iy := 0 to ColCount - 1 do
  176.         begin
  177.            if(arr[iy, ix] = 1) then
  178.               begin
  179.                  x := x0 + ix * WidthCell;
  180.                  y := y0 + iy * HeightCell;
  181.                  DrawingRookInCell(x, y);
  182.               end;
  183.         end;
  184. end;
  185.  
  186.  
  187.  
  188.  
  189. {Main}
  190. begin
  191.     InitializationInitialParameters;
  192.     FillPlacesAttackFigures(PlacesAttackFigures, XCellCount, YCellCount);
  193.     DrawBoard;
  194.     PlacementOfFigures(LocationFigures, YCellCount, XCellCount);
  195.     writeln('Число свободных мест: ', GetCountFreePlaces(PlacesAttackFigures,
  196.                                                       XCellCount, YCellCount));
  197. end.
Advertisement
Add Comment
Please, Sign In to add comment