Advertisement
Yevrag35

Imperfect Shuffling

Oct 22nd, 2020
2,497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $deck = @(
  2.   '♥A','♥2','♥3','♥4','♥5','♥6','♥7','♥8','♥9','♥10','♥J','♥Q','♥K',
  3.   '♣A','♣2','♣3','♣4','♣5','♣6','♣7','♣8','♣9','♣10','♣J','♣Q','♣K',
  4.   '♦A','♦2','♦3','♦4','♦5','♦6','♦7','♦8','♦9','♦10','♦J','♦Q','♦K',
  5.   '♠A','♠2','♠3','♠4','♠5','♠6','♠7','♠8','♠9','♠10','♠J','♠Q','♠K'
  6. )
  7.  
  8. Function Cut-Deck($Deck) {
  9.  
  10.     $splitAt = Get-Random -Minimum 19 -Maximum 34   # So split between index 19 and 33 of the deck.
  11.  
  12.     [pscustomobject]@{
  13.         LeftHand = [System.Collections.Generic.Stack[object]]$Deck[0..($splitAt - 1)]
  14.         RightHand = [System.Collections.Generic.Stack[object]]$Deck[$splitAt..51]
  15.     }
  16. }
  17.  
  18. Function Shuffle-Deck($TwoPiles) {
  19.  
  20.     $singlePile = New-Object 'System.Collections.Generic.List[object]'(52)
  21.  
  22.     while ($TwoPiles.LeftHand.Count -gt 0 -or $TwoPiles.RightHand.Count -gt 0) {
  23.  
  24.         if ($TwoPiles.LeftHand.Count -gt 0) {
  25.            
  26.             $singlePile.Add($TwoPiles.LeftHand.Pop())   # release bottom card
  27.  
  28.             if ($TwoPiles.LeftHand.Count -gt 0 -and -not (Get-Random -Maximum 6)) {     # ~17% chance of occurring -
  29.                                                                                         # my left hand isn't what it used to be...
  30.                 # My hands are a little unsteady... one extra card was let go.
  31.                 $singlePile.Add($TwoPiles.LeftHand.Pop())
  32.             }
  33.         }
  34.  
  35.         if ($TwoPiles.RightHand.Count -gt 0) {
  36.            
  37.             $singlePile.Add($TwoPiles.RightHand.Pop())  # release bottom card
  38.  
  39.             if ($TwoPiles.RightHand.Count -gt 0 -and -not (Get-Random -Maximum 10)) {   # 10% chance of occurring.
  40.  
  41.                 # My hands are a little unsteady... one extra card was let go.
  42.                 $singlePile.Add($TwoPiles.RightHand.Pop())
  43.             }
  44.         }
  45.     }
  46.  
  47.     return $singlePile
  48. }
  49.  
  50. $numberOfShuffles = 4
  51.  
  52. for ($i = 0; $i -lt $numberOfShuffles; $i++) {
  53.  
  54.     $splitDeck = Cut-Deck -Deck $deck
  55.     $deck = Shuffle-Deck -TwoPiles $splitDeck
  56. }
  57. $deck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement