How to perform multiple 5‑card draws from a shuffled 52‑card deck in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

'
' 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:
' - List(Of String) for the deck
' - Fisher–Yates shuffle for efficient randomization
' - Random for high‑quality randomness
'
' 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
'

Module CardDraws

    Function BuildDeck() As List(Of String)
        Dim ranks As String() = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
        Dim suits As String() = {"Clubs", "Diamonds", "Hearts", "Spades"}

        Dim deck As New List(Of String)(52)

        For Each suit In suits
            For Each rank In ranks
                deck.Add($"{rank} of {suit}")
            Next
        Next

        Return deck
    End Function

    Sub ShuffleDeck(deck As List(Of String), rng As Random)
        ' Fisher–Yates shuffle
        For i As Integer = deck.Count - 1 To 1 Step -1
            Dim j As Integer = rng.Next(i + 1)
            Dim temp As String = deck(i)
            deck(i) = deck(j)
            deck(j) = temp
        Next
    End Sub

    Sub DrawFive(deck As List(Of String), drawNumber As Integer, rng As Random)
        ' Shuffle the remaining deck before this draw
        ShuffleDeck(deck, rng)

        Console.WriteLine($"Draw #{drawNumber}:")
        For i As Integer = 0 To 4
            Console.WriteLine("  " & deck(i))
        Next
        Console.WriteLine()

        ' Remove drawn cards
        deck.RemoveRange(0, 5)
    End Sub

    Sub Main()
        ' Number of draws
        Dim draws As Integer = 3

        Dim deck As List(Of String) = BuildDeck()
        Dim rng As New Random()

        ' Perform multiple draws (no duplicates)
        For i As Integer = 1 To draws
            If deck.Count < 5 Then
                Console.WriteLine($"Not enough cards left for draw #{i}.")
                Exit For
            End If

            DrawFive(deck, i, rng)
        Next
    End Sub

End Module


'
' run:
'
' Draw #1:
'  8 of Hearts
'  Q of Hearts
'  5 of Spades
'  7 of Hearts
'  A of Diamonds

' Draw #2:
'  K of Hearts
'  4 of Diamonds
'  K of Spades
'  8 of Diamonds
'  J of Hearts
'
' Draw #3:
'  Q of Spades
'  9 of Diamonds
'  9 of Clubs
'  7 of Clubs
'  6 of Spades
'

 



answered 11 hours ago by avibootz

Related questions

...