I am trying to make a code for shuffling a deck of 52 cards no jokers. I have generated a deck and I plan on randomly inputing all the values into another array as my shuffling. How should I do that?

``````program game;

var
Deck    : Array [1..4,1..13] of integer;
suit_var, card_var : Integer;
card: integer;

Shuffle : Array [1..4, 1 ..13] of integer;
suit2_var, card2_var : Integer;

begin
for suit_var := 1 to 4 do
begin
for card_var := 1 to 13 do
for card:= 1 to 13 do

Deck[suit_var] [card_var] :=card;
end;

for suit_var := 1 to 4 do
begin
for card_var := 1 to 13 do
begin

if suit_var=1 then
write('diamond');

if suit_var=2 then
write('clubs');

if suit_var=3 then
write('hearts');

if suit_var=4 then
write(',');

if card_var=11 then
write('Jack');

if card_var=12 then
write('Queen');

if card_var=13 then
write('King');

if card_var=1 then
write('Ace');

if card_var=2 then
write('Two');

if card_var=3 then
write('Three');

if card_var=4 then
write('Four');

if card_var=5 then
write('Five');

if card_var=6 then
write('Six');

if card_var=7 then
write('Seven');

if card_var=8 then
write('Eight');

if card_var=9 then
write('Nine');

if card_var=10 then
write('Ten');
write('    ');
end;

end;
end.``````

Here is some code I wrote for you to think about things in another way.
The shuffle is trying to simulate an actual shuffle. It is kind of neet to
see where cards move to within the deck during a shuffle.

Project1.dpr

``````program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
DeckOfCards in 'DeckOfCards.pas';
{\$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.``````

Unit1.pas

``````unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DeckOfCards;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Deck : TCardDeck;
public
Procedure ShowDeck;
end;
var
Form1: TForm1;
implementation
{\$R *.dfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Deck := TCardDeck.Create(2, True);
//  Deck.TraceList := ListBox2.Items;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ShowDeck;
end;
procedure TForm1.ShowDeck;
var
i : Integer;
Card : TPlayingCards;
Name : STring;
begin
ListBox1.Clear;
for i := 0 to Deck.Count-1 do
begin
Card := Deck.Card[i];
Name := Deck.CardName[Card];
ListBox1.Items.Append(Name);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Deck.Shuffle(100);
ShowDeck;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Deck.Cut;
ShowDeck;
end;
end.``````

Unit1.dfm

``````object Form1: TForm1
Left = 288
Top = 199
Width = 680
Height = 489
Caption = 'Form1'
Color = clWindow
Ctl3D = False
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnShow = FormShow
DesignSize = (
664
453)
PixelsPerInch = 96
TextHeight = 13
object ListBox1: TListBox
Left = 0
Top = 48
Width = 664
Height = 405
Anchors = [akLeft, akTop, akRight, akBottom]
BorderStyle = bsNone
Columns = 4
ItemHeight = 13
TabOrder = 0
end
object Button1: TButton
Left = 24
Top = 11
Width = 75
Height = 25
Caption = 'Shuffle'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 112
Top = 11
Width = 75
Height = 25
Caption = 'Cut'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 2
OnClick = Button2Click
end
end``````

DeckOfCards.pas

``````unit DeckOfCards;

interface
uses
Windows, Messages, SysUtils, Variants, Classes;
type
TPlayingCards =
( pcAS, pc2S, pc3S, pc4S, pc5S, pc6S, pc7S, pc8S, pc9S, pc10S, pcJS, pcQS, pcKS
, pcAH, pc2H, pc3H, pc4H, pc5H, pc6H, pc7H, pc8H, pc9H, pc10H, pcJH, pcQH, pcKH
, pcAD, pc2D, pc3D, pc4D, pc5D, pc6D, pc7D, pc8D, pc9D, pc10D, pcJD, pcQD, pcKD
, pcAC, pc2C, pc3C, pc4C, pc5C, pc6C, pc7C, pc8C, pc9C, pc10C, pcJC, pcQC, pcKC
, pcJ1, pcJ2);
TCardArray = array of TPlayingCards;
TCardDeck = class
private
FDeck : TCardArray;
function GetCard(Index: Integer): TPlayingCards;
function GetCardName(Card: TPlayingCards): String;
function GetCardCount: Integer;
procedure Trace(Frmt : String; args : array of const);
public
TraceList : TStrings;
property Count : Integer Read GetCardCount;
property CardName[Card : TPlayingCards] : String read GetCardName;
property Card[Index : Integer] : TPlayingCards read GetCard;
procedure Cut;
procedure Shuffle(ShuffleCount : Integer);
constructor Create(NumberOfDecks : Integer; IncludeJokers : Boolean);
end;
implementation
uses
TypInfo;
{ TCardDeck }
constructor TCardDeck.Create(NumberOfDecks : Integer; IncludeJokers : Boolean);
var
i, x : Integer;
c : TPlayingCards;
begin
if IncludeJokers then
begin
SetLength(FDeck, 54 * NumberOfDecks);
x := 0;
for i := 1 to NumberofDecks do
begin
for c := pcAS to pcJ2 do
begin
FDeck[x] := c;
Inc(x);
end;
end;
end
else
begin
SetLength(FDeck, 52 * NumberOfDecks);
x := 0;
for i := 1 to NumberofDecks do
begin
for c := pcAS to pcKC do
begin
FDeck[x] := c;
Inc(x);
end;
end;
end;
//  Randomize;
end;
function TCardDeck.GetCard(Index: Integer): TPlayingCards;
begin
Result := FDeck[Index Mod Count];
end;
function TCardDeck.GetCardCount: Integer;
begin
Result := Length(FDeck);
end;
function TCardDeck.GetCardName(Card: TPlayingCards): String;
begin
case Card of
pcAS, pcAH, pcAD, pcAC: Result := 'Ace of ';
pc2S, pc2H, pc2D, pc2C: Result := 'Duce of ';
pc3S, pc3H, pc3D, pc3C: Result := 'Three of ';
pc4S, pc4H, pc4D, pc4C: Result := 'Four of ';
pc5S, pc5H, pc5D, pc5C: Result := 'Five of ';
pc6S, pc6H, pc6D, pc6C: Result := 'Six of ';
pc7S, pc7H, pc7D, pc7C: Result := 'Seven of ';
pc8S, pc8H, pc8D, pc8C: Result := 'Eight of ';
pc9S, pc9H, pc9D, pc9C: Result := 'Nine of ';
pc10S, pc10H, pc10D, pc10C: Result := 'Ten of ';
pcJS, pcJH, pcJD, pcJC: Result := 'Jack of ';
pcQS, pcQH, pcQD, pcQC: Result := 'Queen of ';
pcKS, pcKH, pcKD, pcKC: Result := 'King of ';
pcJ1, pcJ2: Result := 'Joker';
end;
case Card of
pcAS, pc2S, pc3S, pc4S, pc5S, pc6S, pc7S, pc8S, pc9S, pc10S, pcJS, pcQS, pcKS: Result := Result+'Spades';
pcAH, pc2H, pc3H, pc4H, pc5H, pc6H, pc7H, pc8H, pc9H, pc10H, pcJH, pcQH, pcKH: Result := Result+'Hearts';
pcAD, pc2D, pc3D, pc4D, pc5D, pc6D, pc7D, pc8D, pc9D, pc10D, pcJD, pcQD, pcKD: Result := Result+'Diamonds';
pcAC, pc2C, pc3C, pc4C, pc5C, pc6C, pc7C, pc8C, pc9C, pc10C, pcJC, pcQC, pcKC: Result := Result+'Clubs';
pcJ1, pcJ2: Result := 'Joker';
end;
end;
procedure TCardDeck.Cut;
var
i, j, k : Integer;
x : Integer;
Temp : TCardArray;
Split : Integer;
Left : Boolean;
begin
SetLength(Temp, Count);
for i := 0 to Count-1 do
Temp[i] := FDeck[i];
Split := (Count div 2) + (Random(10)-5);
k := 0;
for i := Split to Count-1 do
begin
FDeck[k] := Temp[i];
Inc(k);
end;
for i := 0 to Split-1 do
begin
FDeck[k] := Temp[i];
Inc(k);
end;
SetLength(Temp, 0);
end;
procedure TCardDeck.Shuffle(ShuffleCount: Integer);
var
i, j, k : Integer;
x : Integer;
Temp : TCardArray;
Split : Integer;
Left : Boolean;
begin
SetLength(Temp, Count);
while ShuffleCount > 0 do
begin
for i := 0 to Count-1 do
Temp[i] := FDeck[i];
Split := (Count div 2) + (Random(10)-5);
i := 0;
j := Split;
k := 0;
Left := Random(1) = 1;
while k < Count do
begin
x := Random(3)+1;
if Left then
begin
while (x > 0) and (i < Split) do
begin
Trace('k:%d i:%d %s', [k, i, Copy(GetEnumName(TypeInfo(TPlayingCards), Integer(Temp[i])), 3, 10)]);
FDeck[k] := Temp[i];
Dec(x);
Inc(i);
Inc(k);
end;
end
else
begin
while (x > 0)  and (j < Count) do
begin
Trace('k:%d j:%d %s', [k, j, Copy(GetEnumName(TypeInfo(TPlayingCards), Integer(Temp[j])), 3, 10)]);
FDeck[k] := Temp[j];
Dec(x);
Inc(j);
Inc(k);
end;
end;
Left := Not Left;
end;
Dec(ShuffleCount);
end;
SetLength(Temp, 0);
end;
procedure TCardDeck.Trace(Frmt: String; args: array of const);
begin
//  TraceList.Append(Format(Frmt, args));
end;

end.``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.