Advertisement
ksyshshot

Lab.6.2

Apr 27th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.42 KB | Source Code | 0 0
  1.     POSSIBLE_MOVES: Array[0..7] of array[0..1] of integer = ((-1, -2), (-2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2));
  2.  
  3. type
  4.     ArrDesk = Array[0..7] of Array[0..7] of Integer;
  5.  
  6. var
  7.     FullDesk: ArrDesk;
  8.  
  9. function CanStep(X, Y: Integer): Boolean;
  10. begin
  11.     if (X >= 0) and (Y >= 0) and (X < DIMENSION) and (Y < DIMENSION) then
  12.         CanStep := true
  13.     else
  14.         CanStep := false;
  15. end;
  16.  
  17. function IsCorrectMove(X, Y: Integer): Boolean;
  18. begin
  19.     if (CanStep(X, Y)) then
  20.         if (FullDesk[X][Y] = 0) then
  21.             IsCorrectMove := true;
  22.     IsCorrectMove := false;
  23.  
  24. end;
  25.  
  26. procedure InitializeArrDesk(var Desk: ArrDesk);
  27. var
  28.     I, J: Integer;
  29. begin
  30.     for I := 0 to High(Desk) do
  31.         for J := 0 to High(Desk) do
  32.             Desk[I][J] := 0;
  33. end;
  34.  
  35. function FindMoves(X, Y, Counter: Integer): Boolean;
  36. var
  37.     I, NextX, NextY: Integer;
  38.     CanContinue: Boolean;
  39. begin
  40.     FullDesk[X][Y] := Counter;
  41.     if (Counter > (DIMENSION * DIMENSION - 1)) then
  42.         FindMoves := true;
  43.     for I := 0 to (DIMENSION - 1) do
  44.     begin
  45.         NextX := X + POSSIBLE_MOVES[I][0];
  46.         NextY := Y + POSSIBLE_MOVES[I][1];
  47.         CanContinue := IsCorrectMove(NextX, NextY);
  48.         if (CanContinue) then
  49.             if (FindMoves(NextX, NextY, (Counter + 1))) then
  50.                 FindMoves := true;
  51.     end;
  52.     FullDesk[X][Y] := 0;
  53.     Dec(Counter);
  54.     FindMoves := false;
  55. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement