$cards = "ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks"
function resolve-cardSign([string]$str){
switch($str.substring(1,1)){
"d" {"Diamonds"; break}
"c" {"Clubs"; break}
"s" {"Spades"; break}
"h" {"Hearts"; break}
}
}
function resolve-cardName([string]$str){
switch($str.substring(0,1)){
"a" {"Ace"; break}
"2" {"Two"; break}
"3" {"Three"; break}
"4" {"Four"; break}
"5" {"Five"; break}
"6" {"Six"; break}
"7" {"Seven"; break}
"8" {"Eight"; break}
"9" {"Nine"; break}
"t" {"Ten"; break}
"j" {"Jack"; break}
"q" {"Queen"; break}
"k" {"King"; break}
}
}
function resolve-cardValue([string]$str){
switch($str.substring(0,1)){
"a" {11; break}
{$_ -match "[tjqk]"} {10; break}
default {$_}
}
}
function shuffle-deck{
$max=$deck.count-1
for ($i = 0; $i -le $max;$i++) {
$r = $rnd.next($max)
$deck[$i],$deck[$r] = $script:deck[$r],$script:deck[$i]
}
}
function deal-user($num){
1..$num | foreach {
$r = $rnd.next($deck.count)
$cardName = "{0} of {1}" -f (resolve-cardName $deck[$r]),(resolve-cardSign $deck[$r])
$script:userCards+=$cardName
$script:user+= resolve-cardValue $deck[$r]
[void]$deck.removeAt($r)
}
}
function deal-dealer($num){
1..$num | foreach {
$r = $rnd.next($deck.count)
$cardName = "{0} of {1}" -f (resolve-cardName $deck[$r]),(resolve-cardSign $deck[$r])
$script:poshCards+=$cardName
$script:posh+= resolve-cardValue $deck[$r]
[void]$deck.removeAt($r)
}
}
function print-hands([switch]$reveal){
"`nYour cards:"
"-----------"
$script:userCards
"`nDealer's cards:"
"---------------"
if($reveal){
$script:poshCards
} else {
"?"
$script:poshCards[1..($script:poshCards.length)]
}
"`n"
}
#########
cls
$rnd = New-Object System.Random (get-date).millisecond
$user=0
$posh=0
$userCards=@()
$poshCards=@()
$deck = new-object system.collections.arraylist
$cards | foreach { [void]$deck.add($_) }
shuffle-deck
deal-user 2
deal-dealer 2
$host.ui.RawUI.WindowTitle = "user: $user, posh $posh"
print-hands
# while user score are less then 21 and user wants another card
while(($user -lt 21) -and ((read-host "Stay (s) or hit (h) ?") -eq "h")){
deal-user 1
$host.ui.RawUI.WindowTitle = "user: $user, posh $posh"
"`nYou have $user`n"
if($user -eq 21) {print-hands -reveal; "You win."; return}
if($user -gt 21) {
print-hands -reveal
"`nOver 21. Sorry, you lose."
return
}
if(($user -eq 21) -and ($posh -eq 21)) {
print-hands -reveal
"`nYou have $user,`nThe dealer has $posh. Ties (pushes) go the dealer. Sorry, you lose."
return
}
$script:userCards
#$script:userCards; "You win."; return
}
"You have $user`n"
# if dealer's hand is greather than users hand then the dealer wins
if(($posh -gt $user) -and ($posh -le 21)) {
print-hands -reveal
"`nYou have $user.`nThe dealer has $posh. Sorry, you lose."; return
}
while($posh -lt $user) {deal-dealer 1}
# final results
if($posh -eq $user) {
"`nYou have $user,`nThe dealer has $posh. Ties (pushes) go the dealer. Sorry, you lose."
return
}
print-hands -reveal
if(($posh -gt $user) -and ($posh -le 21)){
"`nThe dealer has $posh. You have $user, You lose."
} else {
"`nThe dealer has $posh. You have $user, You win."
}