' 21.vbs
' A Psuedo-Blackjack Program by Peter Pike

Option Explicit

' // CONSTANTS /////////////////////
 

const Max = 21
const Card1Min = 3
const Card1max = 7
const Card2Min = 1
const Card2Max = 11
const Bet = 1
 

' // VARIABLES /////////////////////
' //// Card variables

dim PotCard, NewCard

' //// User and dealer variables

dim UserTotal, DealerTotal, UserBankroll

' //// Message variables

dim strMessage, intInput, intLoopControl, strErrorMessage

' //// Boolean control

dim Valid, UserNotDone, UserBusts, DealerNotDone, DealerBusts, Ties

 

' // Functions ////////////////////////////////////////////////////////////////

' //// Draw a type 1 Card

Function Type1
     Type1 = Int((Card1max - Card1Min + 1) * (Rnd) + Card1Min)
End Function


' //// Draw a type 2 card

Function Type2
     Type2 = Int((Card2max - Card2Min + 1) * (Rnd) + Card2Min)
End Function

 

'///////////////////////////////////////////////////////////////////////

 
Randomize
UserBankroll = 10

' // Begin Main Program Loop //////////////////////////////////////////

intLoopControl = 1
strErrorMessage = ""
DO
' // Input main menu //////////////////////////////////////////////////

     DO
    strMessage = strErrorMessage & vbCrLf & vbCrLf
    strMessage = strMessage & "You have $" & UserBankroll & ".  A bet costs $" & Bet & vbCrLf
    strMessage = strMessage & "Enter 1 to place your bet." & vbCrLf
    strMessage = strMessage & "Enter 0 to Exit."

    intInput = InputBox(strMessage, "Make your selection:", 1)

    strErrorMessage = "INVALID FORMAT.  MUST BE A NUMBER."

    Valid = IsNumeric(intInput)
     LOOP WHILE Valid = FALSE

strErrorMessage = ""
strMessage = ""

IF intInput = 0 THEN 
    intLoopControl = 0
ELSE
    ' Play Round
    UserNotDone = TRUE
    UserBusts = FALSE
    DealerNotDone = TRUE    
    DealerBusts = FALSE
    

    PotCard = Type2
    UserTotal = PotCard
    DealerTotal = PotCard

    DO
    
            DO
        strMessage = strErrorMessage & vbCrLf & vbCrLf
        strMessage = strMessage & "Your current hand score = " & UserTotal & vbCrLf
        strMessage = strMessage & "Enter 1 to choose type 1 card ( " & Card1Min & " - " & Card1Max & ")" & vbCrLf
        strMessage = strMessage & "Enter 2 to choose type 2 card ( " & Card2Min & " - " & Card2Max & ")" & vbCrLf
        strMessage = strMessage & "Enter 0 to Hold." & vbCrLf
    
        intINput = InputBox(strMessage, "Choose your action:", 0)

        Valid = IsNumeric(intInput)

        IF Valid THEN
            IF (intInput < 0) OR (intInput > 2) THEN
                Valid = FALSE
            End If
        End If

        strErrorMessage = "INVALID FORMAT.  MUST BE A NUMBER BETWEEN 0 AND 2."
            LOOP WHILE Valid = False
    
    strErrorMessage = ""
    strMessage = ""
    NewCard = 0

    Select Case intInput
        Case 0
            UserNotDone = FALSE
        Case 1
            NewCard = Type1
        Case 2
            NewCard = Type2
    End Select
    
    UserTotal = UserTotal + NewCard

    If UserTotal > Max THEN 
        UserNotDone = FALSE
        UserBusts = TRUE
    End IF

    LOOP WHILE UserNotDone


    IF NOT UserBusts THEN
        DO    
        NewCard = Type2
        DealerTotal = DealerTotal + NewCard

        IF DealerTotal > Max THEN
            DealerNotDone = FALSE
            DealerBusts = TRUE
        ELSEIF DealerTotal >= UserTotal THEN
            DealerNotDone = FALSE
            DealerBusts = FALSE
        ELSE
            DealerNotDone = TRUE
        END IF

        LOOP WHILE DealerNotDone = TRUE    
    END IF


    ' // Calculate Score


    dim UserWins

    UserWins = 1

    IF (UserBusts = TRUE) THEN UserWins = 0 
    IF ((DealerBusts = FALSE) AND (DealerTotal > UserTotal)) THEN UserWins = 0 
    IF ((DealerBusts = FALSE) AND (UserBusts = FALSE) AND (UserTotal = DealerTotal)) THEN UserWins = 2 

    strMessage = "Your score: " & UserTotal & vbCrLF
    strMessage = strMessage & "Dealer Score: " & DealerTotal & vbCrLF


    Select Case UserWins
        Case 0
          strMessage = strMessage & "You have lost $" & Bet
          UserBankroll = UserBankroll - Bet
        Case 1
          strMessage = strMessage & "You have won $" & Bet
          UserBankroll = UserBankroll + Bet
        Case 2
          strMessage = strMessage & "You have tied."
    End Select


    Wscript.Echo(strMessage)

END IF

LOOP WHILE ((intLoopControl <> 0) AND (UserBankroll > 0))

wscript.echo("Your final bankroll is: $" & UserBankroll)

