How to perform multiple 5‑card draws from a shuffled 52‑card deck in Pascal

1 Answer

0 votes
program CardDraws;

{
A standard deck of 52 playing cards consists of four suits:
spades (♠), hearts (♥), diamonds (♦), and clubs (♣).

Each suit contains 13 ranks:
Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.

Cards of spades and clubs are black, while hearts and diamonds are red.

Color is informational only — it does not affect shuffling or random selection.
}

{
    This program performs multiple draws of 5 random cards from a 52‑card deck
    WITHOUT duplicates across draws.

    It uses:
    - Arrays of strings to store ranks, suits, and the deck
    - Fisher–Yates shuffle for efficient randomization
    - Randomize + Random() for randomness
    - Functions for clean structure

    Algorithm:
    1. Build a full deck (52 cards)
    2. For each draw:
       a. Shuffle the remaining deck
       b. Draw 5 cards
       c. Remove them from the deck
}

const
  DECK_SIZE = 52;
  DRAW_SIZE = 5;

type
  TString = string[20];
  TDeck = array[1..DECK_SIZE] of TString;

procedure BuildDeck(var deck: TDeck; var deckCount: Integer);
var
  ranks: array[1..13] of TString;
  suits: array[1..4] of TString;
  i, j, idx: Integer;
begin
  { Initialize ranks }
  ranks[1]  := '2';
  ranks[2]  := '3';
  ranks[3]  := '4';
  ranks[4]  := '5';
  ranks[5]  := '6';
  ranks[6]  := '7';
  ranks[7]  := '8';
  ranks[8]  := '9';
  ranks[9]  := '10';
  ranks[10] := 'J';
  ranks[11] := 'Q';
  ranks[12] := 'K';
  ranks[13] := 'A';

  { Initialize suits }
  suits[1] := 'Clubs';
  suits[2] := 'Diamonds';
  suits[3] := 'Hearts';
  suits[4] := 'Spades';

  idx := 1;
  for i := 1 to 4 do
    for j := 1 to 13 do
    begin
      deck[idx] := ranks[j] + ' of ' + suits[i];
      Inc(idx);
    end;

  deckCount := DECK_SIZE;
end;

procedure ShuffleDeck(var deck: TDeck; deckCount: Integer);
var
  i, j: Integer;
  temp: TString;
begin
  for i := deckCount downto 2 do
  begin
    j := Random(i) + 1;  { Random returns 0..i-1 }
    temp := deck[i];
    deck[i] := deck[j];
    deck[j] := temp;
  end;
end;

procedure DrawFive(var deck: TDeck; var deckCount: Integer; drawNumber: Integer);
var
  i: Integer;
begin
  ShuffleDeck(deck, deckCount);

  WriteLn('Draw #', drawNumber, ':');
  for i := 1 to DRAW_SIZE do
    WriteLn('  ', deck[i]);
  WriteLn;

  { Remove drawn cards by shifting the deck }
  for i := DRAW_SIZE + 1 to deckCount do
    deck[i - DRAW_SIZE] := deck[i];

  deckCount := deckCount - DRAW_SIZE;
end;

var
  deck: TDeck;
  deckCount: Integer;
  draws, i: Integer;

begin
  Randomize;

  { Number of draws }
  draws := 3;

  BuildDeck(deck, deckCount);

  for i := 1 to draws do
  begin
    if deckCount < DRAW_SIZE then
    begin
      WriteLn('Not enough cards left for draw #', i, '.');
      Break;
    end;

    DrawFive(deck, deckCount, i);
  end;
end.


{
run:

Draw #1:
  K of Clubs
  9 of Clubs
  2 of Diamonds
  4 of Clubs
  2 of Hearts

Draw #2:
  6 of Diamonds
  6 of Hearts
  8 of Spades
  7 of Diamonds
  9 of Spades

Draw #3:
  5 of Diamonds
  A of Spades
  J of Spades
  2 of Spades
  4 of Spades

}

 



answered 8 hours ago by avibootz

Related questions

...