Skip to content

Instantly share code, notes, and snippets.

@sdwheeler
Last active October 2, 2021 08:43
Show Gist options
  • Save sdwheeler/73d8e55fb4049a4dd3155b3f9ae65b1e to your computer and use it in GitHub Desktop.
Save sdwheeler/73d8e55fb4049a4dd3155b3f9ae65b1e to your computer and use it in GitHub Desktop.
Poker hand example using Update-List
class Cards {
[System.Collections.Generic.List[string]]$cards
[string]$name
Cards([string]$_name) {
$this.name = $_name
$this.cards = [System.Collections.Generic.List[string]]::new()
}
NewDeck() {
#$suits = "`u{2663}","`u{2666}","`u{2665}","`u{2660}"
$_values = 'A',2,3,4,5,6,7,8,9,10,'J','Q','K'
$_suits = [char]0x2663,[char]0x2666,[char]0x2665,[char]0x2660
$_deck = foreach ($s in $_suits){ foreach ($v in $_values){ "$v$s"} }
$this | Update-List -Property cards -Add $_deck | Out-Null
}
Show() {
Write-Host
Write-Host $this.name ": " $this.cards[0..12]
if ($this.cards.count -gt 13) {
Write-Host (' ' * ($this.name.length+3)) $this.cards[13..25]
}
if ($this.cards.count -gt 26) {
Write-Host (' ' * ($this.name.length+3)) $this.cards[26..38]
}
if ($this.cards.count -gt 39) {
Write-Host (' ' * ($this.name.length+3)) $this.cards[39..51]
}
}
Shuffle() { $this.cards = Get-Random $this.cards -Count 52 }
Sort() { $this.cards.Sort() }
}
$player1 = [Cards]::new('Player 1')
$player2 = [Cards]::new('Player 2')
$deck = [Cards]::new('Deck')
$deck.NewDeck()
$deck.Shuffle()
$deck.Show()
# Deal two hands
for ($x=0; $x -lt 10; $x+=2) {
$player1 | Update-List -Property cards -Add $deck.cards[$x] | Out-Null
$player2 | Update-List -Property cards -Add $deck.cards[$x+1] | Out-Null
}
$deck | Update-List -Property cards -Remove ([string[]]$player1.cards) | Out-Null
$deck | Update-List -Property cards -Remove ([string[]]$player2.cards) | Out-Null
$player1.Show()
$player2.Show()
$deck.Show()
@sdwheeler
Copy link
Author

sdwheeler commented Nov 9, 2019

Deck :  4♥ Q♣ 3♦ 7♠ 2♣ 2♠ 4♣ 10♣ 6♦ Q♠ 6♠ 3♥ 7♥
        J♥ 3♠ 2♦ Q♥ 9♦ 8♠ K♣ A♥ Q♦ 7♣ 6♣ 10♦ 10♥
        8♦ K♦ 9♣ J♠ 6♥ 9♥ 5♥ A♣ 8♣ 4♠ 4♦ 5♠ K♠
        7♦ J♣ A♠ A♦ K♥ 5♣ 3♣ J♦ 5♦ 8♥ 9♠ 10♠ 2♥

Player 1 :  4♥ 3♦ 2♣ 4♣ 6♦

Player 2 :  Q♣ 7♠ 2♠ 10♣ Q♠

Deck :  6♠ 3♥ 7♥ J♥ 3♠ 2♦ Q♥ 9♦ 8♠ K♣ A♥ Q♦ 7♣
        6♣ 10♦ 10♥ 8♦ K♦ 9♣ J♠ 6♥ 9♥ 5♥ A♣ 8♣ 4♠
        4♦ 5♠ K♠ 7♦ J♣ A♠ A♦ K♥ 5♣ 3♣ J♦ 5♦ 8♥
        9♠ 10♠ 2♥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment