# Advanced Event 10: Blackjack!
# http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent10.mspx
# -ShowValue is an aid in testing the script
param([switch]$showValues,$dealerColor=$host.ui.RawUI.ForegroundColor,$playerColor=$host.ui.RawUI.ForegroundColor)
# Define cards and their values, prepare for double-value cards (the Ace), but I did not implement the
# logic for playing with it
$cards="Hearts","Diamonds","Spades","Clubs" | % {
$suite=$_
"Ace,11,1","Two,2","Three,3","Four,4","Five,5","Six,6","Seven,7","Eight,8","Nine,9","Ten,10","Jack,10","Queen,10","King,10" | % {
$card="" | select Name,Rank,Suite,Value,Value2
$Card.Rank,$Card.Value,$Card.Value2=$_.split(",")
$card.Name=$Card.Rank + " of $suite"
$card.Suite=$suite
$card
}
}
# Shuffle (using sort-random) and place in stack - love the ability to use .net :D
[Collections.Stack] $cards=$cards | sort {(new-object random).next()}
# Show a hand
function ShowCards([switch]$dealer) {
begin {
""
if ($dealer.ispresent) {
$color=$dealerColor
write-host -foreground $color "Dealer's cards:"
}
else {
$color=$playerColor
write-host -foreground $color "Your cards:"
}
$sum=0
}
process {
if ($showValues.ispresent) {
write-host -foreground $color ("{0} ({1})" -f $_.name,$_.value)
}
else {
write-host -foreground $color $_.name
}
$sum+=$_.value
}
end {
if ($showValues.ispresent) {
write-host -foreground $color ("Total {0}" -f $sum)
}
}
}
# Stay or hit function
function Ask{
write-host -nonewline "Stay (s) or hit (h) ?"
do {
$key=$host.ui.rawui.ReadKey("noecho,includekeydown").character
} until ($key -eq "s" -or $key -eq "h")
write-host $key
$key
}
# Sum up the hand
function value{
begin {
$sum=0
}
process {
$sum+=$_.value
}
end {
$sum
}
}
$player=@()
$dealer=@()
# Start dealing
$player+=$cards.pop()
$player+=$cards.pop()
$player | ShowCards
$playervalue=$player | value
# And the dealer
$dealer+=$cards.pop()
$dealer | ShowCards -dealer
# Second card is hidden
$dealer+=$cards.pop()
$dealervalue=$dealer | value
# Player's turn
# Continue dealing until gt 21 or 'stay'
while ((ask) -eq "h") {
$player+=$cards.pop()
$player | ShowCards
$playervalue=$player | value
if ($playervalue -ge 21) {
break
}
}
"You have $playervalue"
""
$dealer | ShowCards -dealer
""
if ($playervalue -gt 21) {
"Over 21. Sorry you loose"
}
elseif ($playervalue -eq 21) {
"Black Jack. You win"
}
elseif ($dealervalue -ge $playervalue) {
"Dealer has $dealervalue. Sorry you loose"
}
else {
# Dealer playes, must continue until player is defeated, black jack or exceeds 21
while ($dealervalue -lt $playervalue) {
$dealer+=$cards.pop()
$dealer | ShowCards -dealer
$dealervalue=$dealer | value
}
if ($dealervalue -gt 21) {
"Dealer has over 21. You win"
}
elseif ($dealervalue -eq 21) {
"Dealer has Black Jack. Sorry you loose"
}
elseif ($dealervalue -ge $playervalue) {
"Dealer has $dealervalue. Sorry you loose"
}
else {
"Dealer has $dealervalue. You win"
}
}
Monday, March 03, 2008
2008 Scripting Games, Solution 10
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment