Advertisement
ksyshshot

Lab.6.2

Apr 28th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.99 KB | Source Code | 0 0
  1. procedure InitializeChessDesk(var Arr: TArrDesk);
  2. var
  3.     I, J: Integer;
  4. begin
  5.     for I := 0 to High(Arr) do
  6.         for J := 0 to High(Arr) do
  7.             Arr[I][J] := 0;
  8. end;
  9.  
  10. function MakeWay(Desk: TArrDesk; var Way: TWay; CurrX, CurrY: Integer): TArrDesk;
  11. var
  12.     I, J, K: Integer;
  13.     CurrNX, CurrNY, CurrCount: Integer;
  14.     PossX, PossY, PossCount: Integer;
  15. begin
  16.     PossX := 0;
  17.     PossY := 0;
  18.     CurrCount := MAX_POSSIBLE_STEPS;
  19.     PossCount := MAX_POSSIBLE_STEPS;
  20.     Desk[CurrX][CurrY] := 1;
  21.     Way[1][0] := CurrX;
  22.     Way[1][1] := CurrY;
  23.     for I := 2 to (DIMENSION * DIMENSION) do
  24.     begin
  25.         for J := 0 to (MAX_POSSIBLE_STEPS - 1) do
  26.         begin
  27.             if (CurrX + Possible_moves[J][0] >= MIN_POSSIBLE_STEPS)
  28.             and (CurrX + Possible_moves[J][0] < MAX_POSSIBLE_STEPS) and
  29.             (CurrY + Possible_moves[J][1] >= MIN_POSSIBLE_STEPS)
  30.             and (CurrY + Possible_moves[J][1] < MAX_POSSIBLE_STEPS)
  31.             and (Desk[CurrX + Possible_moves[J][0]][CurrY + Possible_moves[J][1]] = 0) then
  32.             begin
  33.                 CurrCount := 0;
  34.                 CurrNX := CurrX + Possible_moves[J][0];
  35.                 CurrNY := CurrY + Possible_moves[J][1];
  36.                 for K := 0 to (MAX_POSSIBLE_STEPS - 1) do
  37.                 begin
  38.                     if (CurrNX + Possible_moves[K][0] >= MIN_POSSIBLE_STEPS) and (CurrNX + Possible_moves[K][0] < MAX_POSSIBLE_STEPS) and
  39.                     (CurrNY + Possible_moves[K][1] >= MIN_POSSIBLE_STEPS) and (CurrNY + Possible_moves[K][1] < MAX_POSSIBLE_STEPS)
  40.                     and (Desk[CurrNX + Possible_moves[K][0]][CurrNY + Possible_moves[K][1]] = 0) then
  41.                     begin
  42.                         Inc(CurrCount);
  43.                     end;
  44.                 end;
  45.             end;
  46.             if PossCount > CurrCount then
  47.             begin
  48.                 PossX := CurrNX;
  49.                 PossY := CurrNY;
  50.                 PossCount := CurrCount;
  51.             end;
  52.         end;
  53.         CurrX := PossX;
  54.         CurrY := PossY;
  55.         CurrNX := 0;
  56.         CurrNY := 0;
  57.         CurrCount := MAX_POSSIBLE_STEPS;
  58.         PossCount := MAX_POSSIBLE_STEPS;
  59.         Desk[CurrX][CurrY] := I;
  60.         Way[I][0] := CurrX;
  61.         Way[I][1] := CurrY;
  62.     end;
  63.     MakeWay := Desk;
  64. end;
  65.  
  66. procedure DrawCellDesignation(var Desk: TImage);
  67. var
  68.     I: Integer;
  69.     J: Char;
  70. begin
  71.     J := #97;               //'a'..'h' = #97..#104
  72.     with Desk.Canvas do
  73.     begin
  74.         MoveTo(0, 0);
  75.         Pen.Color := clBlack;
  76.         Brush.Color := clWhite;
  77.         for I := 0 to (DIMENSION - 1) do
  78.         begin
  79.             Rectangle((I * X_CHANGE), 0, ((I + 1) * X_CHANGE), Y_CHANGE);
  80.             TextOut((I * X_CHANGE + TEXT_MARGIN_X_HOR), TEXT_MARGIN_Y_HOR, J);
  81.             Rectangle((DIMENSION * X_CHANGE), (Y_CHANGE + X_CHANGE * I), (DIMENSION * X_CHANGE + Y_CHANGE), (Y_CHANGE + X_CHANGE * (I + 1)));
  82.             TextOut((DIMENSION * X_CHANGE + TEXT_MARGIN_X_VER), (Y_CHANGE + X_CHANGE * I + TEXT_MARGIN_X_HOR), IntToStr(I + 1));
  83.             Inc(J);
  84.         end;
  85.     end;
  86. end;
  87.  
  88. procedure DrawCellLine(FirstColor: TColor; var Desk: TImage; CurrY: Integer);
  89. var
  90.     I: Integer;
  91.     CurrColor: TColor;
  92. begin
  93.     CurrColor := FirstColor;
  94.     with Desk.Canvas do
  95.     begin
  96.         for I := 0 to (DIMENSION - 1) do
  97.         begin
  98.             Brush.Color := CurrColor;
  99.             Rectangle((I * CELL_CHANGE), CurrY, ((I + 1) * CELL_CHANGE), (CurrY + CELL_CHANGE));
  100.             ChangeColor(CurrColor);
  101.         end;
  102.     end;
  103. end;
  104.  
  105. procedure DrawWay(Desk: TImage; I, X, Y: Integer);
  106. var
  107.     Numb: String;
  108. begin
  109.     Numb := IntToStr(I);
  110.     Desk.Canvas.TextOut((CELL_CHANGE * X + NUMBERS_MARGIN_HOR), (Y_CHANGE + Y * CELL_CHANGE + NUMBERS_MARGIN_VER), Numb);
  111. end;
  112.  
  113. procedure DrawChessDesk(ChessDesk: TImage);
  114. var
  115.     I: Integer;
  116. begin
  117.     with ChessDesk.Canvas do
  118.     begin
  119.         Pen.Color := clBlack;
  120.         Brush.Color := clWhite;
  121.         Font.Color := clBlack;
  122.         Font.Size := 9;
  123.         Font.Style := [];
  124.         DrawCellDesignation(ChessDesk);
  125.         for I := 0 to DIMENSION - 1 do
  126.         begin
  127.             if I mod 2 = 1 then
  128.                 DrawCellLine(clBlack, ChessDesk, (Y_CHANGE + I * (CELL_CHANGE)))
  129.             else
  130.                 DrawCellLine(clWhite, ChessDesk, (Y_CHANGE + I * (CELL_CHANGE)));
  131.         end;
  132.     end;
  133. end;
  134.  
  135. procedure TForm_6_2.ButtonFindMovesClick(Sender: TObject);
  136. var
  137.     IsCorrect: Boolean;
  138.     Error: String;
  139.     StartX, StartY: Integer;
  140. begin
  141.     ButtonFindMoves.Enabled := false;
  142.     IsCorrect := true;
  143.     Error := '';
  144.     try
  145.         StartX := Ord(EditStartLetter.Text[1]) - CHARACTER_CODE_MIN;
  146.         if (StartX < MIN_NUMBER) or (StartX > (DIMENSION - 1)) then
  147.         begin
  148.             IsCorrect := false;
  149.             Error := 'Введена недопустимая буква стартовой позиции. ';
  150.         end;
  151.         StartY := StrToInt(EditStartDigit.Text) - 1;
  152.         if (StartY < MIN_NUMBER) or (StartY > (DIMENSION - 1)) then
  153.         begin
  154.             IsCorrect := false;
  155.             Error := Error + 'Введена недопустимая цифра стартовой позиции. ';
  156.         end;
  157.     except
  158.         IsCorrect := false;
  159.         Error := 'Найдено некорректно введённое значение! ' + Error;
  160.     end;
  161.     if (IsCorrect) then
  162.     begin
  163.         Counter := 1;
  164.         DrawChessDesk(ChessDesk);
  165.         InitializeChessDesk(Answer);
  166.         Answer := MakeWay(Answer, Way, StartX, StartY);
  167.         ChessDesk.Canvas.Font.Color := clHotLight;
  168.         ChessDesk.Canvas.Brush.Style := bsClear;
  169.         ChessDesk.Canvas.Font.Size := 10;
  170.         ChessDesk.Canvas.Font.Style := [fsBold];
  171.         Timer.Enabled := true;
  172.         ButtonSaveFile.Enabled := true;
  173.     end
  174.     else
  175.     begin
  176.         UnitError.FormError.LabelError.Caption := 'Ошибка! ' +  Error;
  177.         UnitError.FormError.ShowModal;
  178.         UnitError.FormError.LabelError.Caption := '';
  179.     end;
  180. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement