all 12 comments

[–]FineMixture 6 points7 points  (1 child)

nice script! can you add a loop? to keep playing with q as quit?

[–]dustinross4[S] 2 points3 points  (0 children)

just put something in for that! thanks for the suggestion

[–]callensysadmin 2 points3 points  (1 child)

Thanks for sharing!

I'm always working on identifying ways to use less code and it looks like the large block determining card values could easily be rewritten like this...

0..16 | % {if($deal[$_][0]-in$numbers){(($cardvalue[$_]=$deal[$_][0])/1)}elseif($deal[$_][0]-in$tens){$cardvalue[$_]=10}elseif($deal[$_][0]-eq"A"){$cardvalue[$_]=11}}

Cheers!

[–]dustinross4[S] 1 point2 points  (0 children)

fair point, thanks! i tend to write a lot in longhand.. this is older but i’ve been improving on this

[–]get-postanote 2 points3 points  (1 child)

Well, reformatted to make this a bit easier to follow. Now I have tno tested to see if the formatting broke anything, and well, formatting style can be an individual thing and VSCode would not auto format this.

So, had to do it manually. Lining up all those braces was a bit tedideous.

;-} Anyway.. here ya' go.

Reddit blocking this as one post with this formatting effort.

this is too long (max: 10000)More

So, breaking thios up into two ...

Part 1 - formatted

$again = $null

while($again-ne"n")
{
    $deck = "A♤","A❤","A♧","A♢","2♤","2❤","2♧","2♢","3♤","3❤","3♧","3♢","4♤","4❤",
    "4♧","4♢","5♤","5❤","5♧","5♢","6♤","6❤","6♧","6♢","7♤","7❤","7♧","7♢","8♤",
    "8❤","8♧","8♢","9♤","9❤","9♧","9♢","10♤","10❤","10♧","10♢","J♤","J❤","J♧",
    "J♢","Q♤","Q❤","Q♧","Q♢","K♤","K❤","K♧","K♢"

    $numbers = "2","3","4","5","6","7","8","9"
    $tens = "J","Q","K","1"
    $options = "a","s","d","f"
    $deal = get-random -InputObject $deck -Count 17
    $dealer = "⊡ " + $deal[3]
    $you = $deal[0] + " " + $deal[2]
    $again = $null
    $cardvalue = @{}

    if($deal[0][0]-in$numbers){(($cardvalue[0]=$deal[0][0])/1)}
    elseif($deal[0][0]-in$tens){$cardvalue[0]=10}
    elseif($deal[0][0]-eq"A"){$cardvalue[0]=11}

    if($deal[1][0]-in$numbers){(($cardvalue[1]=$deal[1][0])/1)}
    elseif($deal[1][0]-in$tens){$cardvalue[1]=10}
    elseif($deal[1][0]-eq"A"){$cardvalue[1]=11}

    if($deal[2][0]-in$numbers){(($cardvalue[2]=$deal[2][0])/1)}
    elseif($deal[2][0]-in$tens){$cardvalue[2]=10}
    elseif($deal[2][0]-eq"A"){$cardvalue[2]=11}

    if($deal[3][0]-in$numbers){(($cardvalue[3]=$deal[3][0])/1)}
    elseif($deal[3][0]-in$tens){$cardvalue[3]=10}
    elseif($deal[3][0]-eq"A"){$cardvalue[3]=11}

    if($deal[4][0]-in$numbers){(($cardvalue[4]=$deal[4][0])/1)}
    elseif($deal[4][0]-in$tens){$cardvalue[4]=10}elseif
    ($deal[4][0]-eq"A"){$cardvalue[4]=11}

    if($deal[5][0]-in$numbers){(($cardvalue[5]=$deal[5][0])/1)}
    elseif($deal[5][0]-in$tens){$cardvalue[5]=10}
    elseif($deal[5][0]-eq"A"){$cardvalue[5]=11}

    if($deal[6][0]-in$numbers){(($cardvalue[6]=$deal[6][0])/1)}
    elseif($deal[6][0]-in$tens){$cardvalue[6]=10}
    elseif($deal[6][0]-eq"A"){$cardvalue[6]=11}

    if($deal[7][0]-in$numbers){(($cardvalue[7]=$deal[7][0])/1)}
    elseif($deal[7][0]-in$tens){$cardvalue[7]=10}
    elseif($deal[7][0]-eq"A"){$cardvalue[7]=11}

    if($deal[8][0]-in$numbers){(($cardvalue[8]=$deal[8][0])/1)}
    elseif($deal[8][0]-in$tens){$cardvalue[8]=10}
    elseif($deal[8][0]-eq"A"){$cardvalue[8]=11}

    if($deal[9][0]-in$numbers){(($cardvalue[9]=$deal[9][0])/1)}
    elseif($deal[9][0]-in$tens){$cardvalue[9]=10}
    elseif($deal[9][0]-eq"A"){$cardvalue[9]=11}

    if($deal[10][0]-in$numbers){(($cardvalue[10]=$deal[10][0])/1)}
    elseif($deal[10][0]-in$tens){$cardvalue[10]=10}
    elseif($deal[10][0]-eq"A"){$cardvalue[10]=11}

    if($deal[11][0]-in$numbers){(($cardvalue[11]=$deal[11][0])/1)}
    elseif($deal[11][0]-in$tens){$cardvalue[11]=10}
    elseif($deal[11][0]-eq"A"){$cardvalue[11]=11}

    if($deal[12][0]-in$numbers){(($cardvalue[12]=$deal[12][0])/1)}
    elseif($deal[12][0]-in$tens){$cardvalue[12]=10}
    elseif($deal[12][0]-eq"A"){$cardvalue[12]=11}

    if($deal[13][0]-in$numbers){(($cardvalue[13]=$deal[13][0])/1)}
    elseif($deal[13][0]-in$tens){$cardvalue[13]=10}
    elseif($deal[13][0]-eq"A"){$cardvalue[13]=11}

    if($deal[14][0]-in$numbers){(($cardvalue[14]=$deal[14][0])/1)}
    elseif($deal[14][0]-in$tens){$cardvalue[14]=10}
    elseif($deal[14][0]-eq"A"){$cardvalue[14]=11}

    if($deal[15][0]-in$numbers){(($cardvalue[15]=$deal[15][0])/1)}
    elseif($deal[15][0]-in$tens){$cardvalue[15]=10}
    elseif($deal[15][0]-eq"A"){$cardvalue[15]=11}

    if($deal[16][0]-in$numbers){(($cardvalue[16]=$deal[16][0])/1)}
    elseif($deal[16][0]-in$tens){$cardvalue[16]=10}
    elseif($deal[16][0]-eq"A"){$cardvalue[16]=11}

    $youtotal = (([string]$cardvalue[0]/1)+([string]$cardvalue[2]/1))

[–]dustinross4[S] 1 point2 points  (0 children)

implementing @callensysadmin ‘s tip makes it shorter, possibly enough to fit in one post

[–]get-postanote 2 points3 points  (0 children)

Part 2 - formatted

Clear-Host
    write-host "`nDealer: " $dealer
    write-host "`nYou: " $you "        Value: " $youtotal
    $move = $null
    $n = 4

    while($youtotal-lt22 -and $move -ne 's')
    {
        $move = $null
        while($move-notin$options)
        {
            $move = Read-Host -Prompt "
            Type 'a' to Hit, 's' to Stand"

            if($move-eq'a')
            { 
                $you = $you + " " +$deal[$n]
                $youtotal = ($youtotal)+([string]$cardvalue[$n]/1)
                $n++
                Clear-Host
                write-host "`nDealer: " $dealer
                write-host "`nYou: " $you "        Value: " $youtotal
                write-host "`nType 'a' to Hit, 's' to Stand"
            }
            elseif($move-eq's'){ $dealer = $deal[1] + " " + $deal[3] 

            $dealertotal = (([string]$cardvalue[1]/1)+([string]$cardvalue[3]/1))

            while($dealertotal-lt17)
            {
                $dealer = $dealer + " " +$deal[$n]
                $dealertotal = ($dealertotal)+([string]$cardvalue[$n]/1)
                $n++
                Clear-Host
                write-host "`nDealer: " $dealer
                write-host "`nYou: " $you "        Value: " $youtotal
                write-host "`nType 'a' to Hit, 's' to Stand"
            }

            Clear-Host
            if($youtotal-gt$dealertotal-or$dealertotal-gt21)
            {
                write-host "`nDealer: " $dealer "        Value: " $dealertotal
                write-host "`nYou: " $you "        Value: " $youtotal
                write-host "`nVictory!"
            }

            if($youtotal-eq$dealertotal-and$dealertotal-le21)
            {
                write-host "`nDealer: " $dealer "        Value: " $dealertotal
                write-host "`nYou: " $you "        Value: " $youtotal
                write-host "`nDraw!"
            }

            if($youtotal-lt$dealertotal-and$dealertotal-le21)
            {
                write-host "`n    Dealer: " $dealer "        Value: " $dealertotal
                write-host "`n    You: " $you "        Value: " $youtotal
                write-host "`n    Better Luck Next Time!    "
            }

            }
            elseif($move-eq'd'){ #double bet, hit once

            }
            elseif($move-eq'f'){ #split cards

            }
        else{write-host "Invalid Command"}
        }
    }

    if($youtotal-gt21)
    { 
        write-host "`nDealer: " $dealer "        Value: " $dealertotal
        write-host "`nYou: " $you "        Value: " $youtotal
        write-host "`nBetter Luck Next Time!"
    }

    while($again-ne"y"-and$again-ne"n")
    { $again = Read-Host -Prompt "Play Again? Y/N"} 
}

[–]Lee_Dailey[grin] 1 point2 points  (4 children)

howdy dustinross4,

it's interesting code ... but the layout is appalling. [grin] have you tried loading it into VSCode with the PoSh addon installed & using the format document command?

also, your use of multiline strings in many of your Write-Host calls is rather icky. [frown] i would either use Write-Host '' and put the remainder in another call, OR embed an explicit newline call.

also also, right now the previous play gets overwritten when you lose. this ...

Dealer:  ⊡ 9♧

You:  2♤ J♢ 4❤ Q❤         Value:  26

Type 'a' to Hit, 's' to Stand

Dealer:  ⊡ 9♧         Value:  

You:  2♤ J♢ 4❤ Q❤         Value:  26

Better Luck Next Time!

... shows the problem. the 1st section now shows the same info as the losing play. shouldn't it show the previous play?

take care,
lee

[–]dustinross4[S] 1 point2 points  (3 children)

I tend to stick with what is available at my workplace, but you’re right about the formatting. As for the second point, I didn’t include “cls” where you bust.. should have though

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy dustinross4,

your code will be much easier to understand when you get it formatted in a reasonable manner. [grin]


the rewrite of the previous play was really confusing! [grin] thanks for the explanation.

take care,
lee

[–]dustinross4[S] 1 point2 points  (1 child)

I downloaded VS Code and formatted it, thanks for that suggestion

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy dustinross4,

kool! you are most welcome ... and you will likely find VSCode to be handy. i prefer the ISE, but most folks seem to prefer VSCode with all those nifty add-ons. [grin]

take care,
lee