Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- procedure InitializeChessDesk(var Arr: TArrDesk);
- var
- I, J: Integer;
- begin
- for I := 0 to High(Arr) do
- for J := 0 to High(Arr) do
- Arr[I][J] := 0;
- end;
- function MakeWay(Desk: TArrDesk; var Way: TWay; CurrX, CurrY: Integer): TArrDesk;
- var
- I, J, K: Integer;
- CurrNX, CurrNY, CurrCount: Integer;
- PossX, PossY, PossCount: Integer;
- begin
- PossX := 0;
- PossY := 0;
- CurrCount := MAX_POSSIBLE_STEPS;
- PossCount := MAX_POSSIBLE_STEPS;
- Desk[CurrX][CurrY] := 1;
- Way[1][0] := CurrX;
- Way[1][1] := CurrY;
- for I := 2 to (DIMENSION * DIMENSION) do
- begin
- for J := 0 to (MAX_POSSIBLE_STEPS - 1) do
- begin
- if (CurrX + Possible_moves[J][0] >= MIN_POSSIBLE_STEPS)
- and (CurrX + Possible_moves[J][0] < MAX_POSSIBLE_STEPS) and
- (CurrY + Possible_moves[J][1] >= MIN_POSSIBLE_STEPS)
- and (CurrY + Possible_moves[J][1] < MAX_POSSIBLE_STEPS)
- and (Desk[CurrX + Possible_moves[J][0]][CurrY + Possible_moves[J][1]] = 0) then
- begin
- CurrCount := 0;
- CurrNX := CurrX + Possible_moves[J][0];
- CurrNY := CurrY + Possible_moves[J][1];
- for K := 0 to (MAX_POSSIBLE_STEPS - 1) do
- begin
- if (CurrNX + Possible_moves[K][0] >= MIN_POSSIBLE_STEPS) and (CurrNX + Possible_moves[K][0] < MAX_POSSIBLE_STEPS) and
- (CurrNY + Possible_moves[K][1] >= MIN_POSSIBLE_STEPS) and (CurrNY + Possible_moves[K][1] < MAX_POSSIBLE_STEPS)
- and (Desk[CurrNX + Possible_moves[K][0]][CurrNY + Possible_moves[K][1]] = 0) then
- begin
- Inc(CurrCount);
- end;
- end;
- end;
- if PossCount > CurrCount then
- begin
- PossX := CurrNX;
- PossY := CurrNY;
- PossCount := CurrCount;
- end;
- end;
- CurrX := PossX;
- CurrY := PossY;
- CurrNX := 0;
- CurrNY := 0;
- CurrCount := MAX_POSSIBLE_STEPS;
- PossCount := MAX_POSSIBLE_STEPS;
- Desk[CurrX][CurrY] := I;
- Way[I][0] := CurrX;
- Way[I][1] := CurrY;
- end;
- MakeWay := Desk;
- end;
- procedure DrawCellDesignation(var Desk: TImage);
- var
- I: Integer;
- J: Char;
- begin
- J := #97; //'a'..'h' = #97..#104
- with Desk.Canvas do
- begin
- MoveTo(0, 0);
- Pen.Color := clBlack;
- Brush.Color := clWhite;
- for I := 0 to (DIMENSION - 1) do
- begin
- Rectangle((I * X_CHANGE), 0, ((I + 1) * X_CHANGE), Y_CHANGE);
- TextOut((I * X_CHANGE + TEXT_MARGIN_X_HOR), TEXT_MARGIN_Y_HOR, J);
- Rectangle((DIMENSION * X_CHANGE), (Y_CHANGE + X_CHANGE * I), (DIMENSION * X_CHANGE + Y_CHANGE), (Y_CHANGE + X_CHANGE * (I + 1)));
- TextOut((DIMENSION * X_CHANGE + TEXT_MARGIN_X_VER), (Y_CHANGE + X_CHANGE * I + TEXT_MARGIN_X_HOR), IntToStr(I + 1));
- Inc(J);
- end;
- end;
- end;
- procedure DrawCellLine(FirstColor: TColor; var Desk: TImage; CurrY: Integer);
- var
- I: Integer;
- CurrColor: TColor;
- begin
- CurrColor := FirstColor;
- with Desk.Canvas do
- begin
- for I := 0 to (DIMENSION - 1) do
- begin
- Brush.Color := CurrColor;
- Rectangle((I * CELL_CHANGE), CurrY, ((I + 1) * CELL_CHANGE), (CurrY + CELL_CHANGE));
- ChangeColor(CurrColor);
- end;
- end;
- end;
- procedure DrawWay(Desk: TImage; I, X, Y: Integer);
- var
- Numb: String;
- begin
- Numb := IntToStr(I);
- Desk.Canvas.TextOut((CELL_CHANGE * X + NUMBERS_MARGIN_HOR), (Y_CHANGE + Y * CELL_CHANGE + NUMBERS_MARGIN_VER), Numb);
- end;
- procedure DrawChessDesk(ChessDesk: TImage);
- var
- I: Integer;
- begin
- with ChessDesk.Canvas do
- begin
- Pen.Color := clBlack;
- Brush.Color := clWhite;
- Font.Color := clBlack;
- Font.Size := 9;
- Font.Style := [];
- DrawCellDesignation(ChessDesk);
- for I := 0 to DIMENSION - 1 do
- begin
- if I mod 2 = 1 then
- DrawCellLine(clBlack, ChessDesk, (Y_CHANGE + I * (CELL_CHANGE)))
- else
- DrawCellLine(clWhite, ChessDesk, (Y_CHANGE + I * (CELL_CHANGE)));
- end;
- end;
- end;
- procedure TForm_6_2.ButtonFindMovesClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- Error: String;
- StartX, StartY: Integer;
- begin
- ButtonFindMoves.Enabled := false;
- IsCorrect := true;
- Error := '';
- try
- StartX := Ord(EditStartLetter.Text[1]) - CHARACTER_CODE_MIN;
- if (StartX < MIN_NUMBER) or (StartX > (DIMENSION - 1)) then
- begin
- IsCorrect := false;
- Error := 'Введена недопустимая буква стартовой позиции. ';
- end;
- StartY := StrToInt(EditStartDigit.Text) - 1;
- if (StartY < MIN_NUMBER) or (StartY > (DIMENSION - 1)) then
- begin
- IsCorrect := false;
- Error := Error + 'Введена недопустимая цифра стартовой позиции. ';
- end;
- except
- IsCorrect := false;
- Error := 'Найдено некорректно введённое значение! ' + Error;
- end;
- if (IsCorrect) then
- begin
- Counter := 1;
- DrawChessDesk(ChessDesk);
- InitializeChessDesk(Answer);
- Answer := MakeWay(Answer, Way, StartX, StartY);
- ChessDesk.Canvas.Font.Color := clHotLight;
- ChessDesk.Canvas.Brush.Style := bsClear;
- ChessDesk.Canvas.Font.Size := 10;
- ChessDesk.Canvas.Font.Style := [fsBold];
- Timer.Enabled := true;
- ButtonSaveFile.Enabled := true;
- end
- else
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal;
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement