Added "Shuffle" to sqids_showcase

This commit is contained in:
Daniele Teti 2024-05-23 11:02:00 +02:00
parent 43478dbdbf
commit cf08f46555
2 changed files with 31 additions and 1 deletions

View File

@ -54,16 +54,18 @@ object MainForm: TMainForm
Top = 104 Top = 104
Width = 449 Width = 449
Height = 45 Height = 45
Max = 20
TabOrder = 1 TabOrder = 1
OnChange = TrackBarMinLengthChange OnChange = TrackBarMinLengthChange
end end
object btnShuffle: TButton object btnShuffle: TButton
Left = 463 Left = 462
Top = 40 Top = 40
Width = 75 Width = 75
Height = 29 Height = 29
Caption = 'Shuffle' Caption = 'Shuffle'
TabOrder = 2 TabOrder = 2
OnClick = btnShuffleClick
end end
object PageControl1: TPageControl object PageControl1: TPageControl
Left = 8 Left = 8

View File

@ -32,6 +32,7 @@ type
procedure btnEncodeClick(Sender: TObject); procedure btnEncodeClick(Sender: TObject);
procedure TrackBarMinLengthChange(Sender: TObject); procedure TrackBarMinLengthChange(Sender: TObject);
procedure btnDecodeClick(Sender: TObject); procedure btnDecodeClick(Sender: TObject);
procedure btnShuffleClick(Sender: TObject);
private private
{ Private declarations } { Private declarations }
public public
@ -73,6 +74,33 @@ begin
EditSqidsOutput.Text := lSqids.Encode(lIntegers); EditSqidsOutput.Text := lSqids.Encode(lIntegers);
end; end;
function GetScrambled(const Alphabet: String): String;
var
I: Integer;
lIdx1: Integer;
lSize: Integer;
lIdx2: Integer;
lTmp: Char;
begin
Randomize;
Result := Alphabet;
lSize := Length(Result);
for I := 1 to 100 do
begin
lIdx1 := Random(lSize) + 1;
lIdx2 := Random(lSize) + 1;
lTmp := Result[lIdx1];
Result[lIdx1] := Result[lIdx2];
Result[lIdx2] := lTmp;
end;
end;
procedure TMainForm.btnShuffleClick(Sender: TObject);
begin
EditAlphabet.Text := GetScrambled(EditAlphabet.Text);
end;
procedure TMainForm.FormCreate(Sender: TObject); procedure TMainForm.FormCreate(Sender: TObject);
begin begin
PageControl1.ActivePageIndex := 0; PageControl1.ActivePageIndex := 0;