Last active
October 2, 2021 08:43
-
-
Save sdwheeler/73d8e55fb4049a4dd3155b3f9ae65b1e to your computer and use it in GitHub Desktop.
Poker hand example using Update-List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
sdwheeler
commented
Nov 9, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment