Finished the blackjack port to MiniScript.

This commit is contained in:
JoeStrout
2023-07-19 18:14:30 -07:00
parent 67f0527692
commit e0a417bf8a

View File

@@ -4,37 +4,34 @@ print " "*15 + "Creative Computing Morristown, New Jersey"
print; print; print print; print; print
fna = function(q); return q - 11*(q >= 22); end function fna = function(q); return q - 11*(q >= 22); end function
P = [[]]*15 // P(i,j) is the jth card in hand i hands = [[]]*15 // hands(i,j) is the jth card in hand i (P)
Q = [0]*15 // Q(i) is the total value of hand i handValue = [0]*15 // total value of each hand (Q)
C = [] // the deck being dealt from deck = [] // the deck being dealt from (C)
D = [] // the discard pile playerMoney = [0]*8 // total $ for each player (T)
T = [0]*8 // total $ for each player roundWinnings = [0]*7 // total $ won/lost this hand for each player (S)
S = [0]*7 // total $ won/lost this hand for each player betPerHand = [0]*15 // bet for each hand (B)
B = [0]*15 // bet for each hand
Z = [0]*7 // insurance for each player
reshuffle = function reshuffle = function
print "Reshuffling" print "Reshuffling"
globals.C = D globals.deck = range(1,13)*4
globals.D = [] deck.shuffle
C.shuffle
end function end function
// Function to draw a card from the deck. // Function to draw a card from the deck.
getCard = function // line 100 getCard = function // line 100
if not C then reshuffle if not deck then reshuffle
return C.pop return deck.pop
end function end function
// Function to evaluate the given hand. Total is usually put into // Function to evaluate the given hand. Total is usually put into
// Q(handNum). Totals have the following meaning: // handValue(handNum). Totals have the following meaning:
// 2-10...Hard 2-10 // 2-10...Hard 2-10
// 11-21...Soft 11-21 // 11-21...Soft 11-21
// 22-32...Hard 11-21 // 22-32...Hard 11-21
// 33+ or -1...Busted // 33+ or -1...Busted
evalHand = function(handNum) // line 300 evalHand = function(handNum) // line 300
result = 0 result = 0
for card in P[handNum] for card in hands[handNum]
result = addCardToTotal(result, card) result = addCardToTotal(result, card)
end for end for
return result return result
@@ -42,6 +39,7 @@ end function
// Function to add a card into a total hand value. // Function to add a card into a total hand value.
addCardToTotal = function(total, card) // line 500 addCardToTotal = function(total, card) // line 500
if total < 0 then return total // (already busted)
x1 = card; if x1 > 10 then x1 = 10 x1 = card; if x1 > 10 then x1 = 10
q1 = total + x1 q1 = total + x1
if total < 11 then if total < 11 then
@@ -79,6 +77,7 @@ getNumber = function(prompt, minVal=0, maxVal=500)
while true while true
result = input(prompt).val result = input(prompt).val
if result == floor(result) and minVal <= result <= maxVal then return result if result == floor(result) and minVal <= result <= maxVal then return result
print "Please enter a number from " + minVal + " to " + maxVal
end while end while
end function end function
@@ -94,117 +93,143 @@ end function
getBets = function getBets = function
print "Bets:" print "Bets:"
for i in range(0, numPlayers-1) for i in range(0, numPlayers-1)
B[i] = getNumber("# " + (i+1) + "? ", 1, 500) betPerHand[i] = getNumber("# " + (i+1) + "? ", 1, 500)
end for end for
end function end function
playOneRound = function playOneRound = function
globals.Z = [0] * numPlayers globals.roundWinnings = [0] * numPlayers
globals.S = [0] * numPlayers if deck.len < (numPlayers+1) * 2 then reshuffle
if C.len < (numPlayers+1) * 2 then reshuffle
getBets getBets
print "PLAYER ", "" print "PLAYER ", ""
for i in range(0, numPlayers-1) for i in range(0, numPlayers-1)
print i+1, " " print i+1, " "
P[i] = [] hands[i] = []
end for end for
print "DEALER" print "DEALER"
P[numPlayers] = [] hands[numPlayers] = []
for row in [1,2] for row in [1,2]
print " ", "" print " ", ""
for i in range(0, numPlayers) for i in range(0, numPlayers)
P[i].push getCard hands[i].push getCard
if row == 1 or i < numPlayers then printCard P[i][-1] if row == 1 or i < numPlayers then printCard hands[i][-1]
end for end for
print print
end for end for
dealerCard0 = hands[numPlayers][0]
dealerCard1 = hands[numPlayers][1]
// Test for insurance // Test for insurance
if P[numPlayers][0] == 1 and getYesNo("Any insurance? ") == "Y" then if dealerCard0 == 1 and getYesNo("Any insurance? ") == "Y" then
print "Insurance Bets" print "Insurance Bets"
for i in range(0, numPlayers-1) for i in range(0, numPlayers-1)
Z[i] = getNumber("# " + (i+1) + "? ", 0, B[i]/2) insurance = getNumber("# " + (i+1) + "? ", 0, betPerHand[i]/2)
S[i] = Z[i] * (3 * (P[numPlayers][1] >= 10) - 1) // ?!? roundWinnings[i] = insurance * (3 * (dealerCard1 >= 10) - 1)
end for end for
end if end if
// Test for dealer blackjack // Test for dealer blackjack
dealerCard0 = P[numPlayers][0]
dealerCard1 = P[numPlayers][1]
if (dealerCard0==1 and dealerCard1 > 9) or if (dealerCard0==1 and dealerCard1 > 9) or
(dealerCard0 > 9 and dealerCard1==1) then (dealerCard0 > 9 and dealerCard1==1) then
print; print "Dealer has a " + cardNames[dealerCard1] + " in the hole for Blackjack" print; print "Dealer has a " + cardNames[dealerCard1] + " in the hole for Blackjack"
for i in range(0, numPlayers) for i in range(0, numPlayers)
Q[i] = evalHand(i) handValue[i] = evalHand(i)
end for end for
else else
// no dealer blackjack // no dealer blackjack
if dealerCard0 == 1 or dealerCard0 >= 10 then
print; print "No dealer Blackjack."
end if
// now play the hands // now play the hands
for i in range(0, numPlayers-1) for i in range(0, numPlayers-1)
playHand i playHand i
end for end for
Q[numPlayers] = evalHand(numPlayers) // (evaluate dealer hand) handValue[numPlayers] = evalHand(numPlayers) // (evaluate dealer hand)
// Test for playing the dealer's hand... we only do so if // Test for playing the dealer's hand... we only do so if
// there are any player hands with cards left. // there are any player hands with cards left.
anyLeft = false anyLeft = false
for i in range(0, numPlayers-1) for i in range(0, numPlayers-1)
if P[i] or P[i+8] then anyLeft = true if hands[i] or hands[i+8] then anyLeft = true
end for end for
if not anyLeft then if not anyLeft then
print "Dealer had a " + cardNames[P[numPlayers][1]] + " concealed." print "Dealer had a " + cardNames[hands[numPlayers][1]] + " concealed."
else else
// Play dealer's hand. // Play dealer's hand.
dispTotal = displayTotal(Q[numPlayers]) dispTotal = displayTotal(handValue[numPlayers])
print "Dealer has a " + cardNames[P[numPlayers][1]] + " concealed" + print "Dealer has a " + cardNames[hands[numPlayers][1]] + " concealed" +
" for a total of " + dispTotal + "." " for a total of " + dispTotal + "."
while Q[numPlayers] > 0 and dispTotal <= 16 while handValue[numPlayers] > 0 and dispTotal <= 16
if P[numPlayers].len == 2 then print "Draws ", ""
card = getCard card = getCard
if hands[numPlayers].len == 2 then print "Draws ", ""
print cardNames[card], " " print cardNames[card], " "
P[numPlayers].push card hands[numPlayers].push card
Q[numPlayers] = evalHand(numPlayers) handValue[numPlayers] = evalHand(numPlayers)
dispTotal = displayTotal(Q[numPlayers]) dispTotal = displayTotal(handValue[numPlayers])
end while end while
if P[numPlayers].len > 2 then print " ---Total is " + dispTotal if hands[numPlayers].len > 2 then
if handValue[numPlayers] < 0 then print " ---Busted" else print " ---Total is " + dispTotal
end if
print print
end if end if
end if end if
tallyResults tallyResults
end function end function
playHand = function(handNum) playHand = function(handNum, prompt=null, allowSplit=true)
while P[handNum] if not prompt then prompt = "Player " + (handNum % 8 + 1)
choice = getOption("Player " + (handNum % 8 + 1) + "? ", ["H", "S", "D", "/"]) while hands[handNum]
options = ["H", "S", "D"] + ["/"] * allowSplit
choice = getOption(prompt + "? ", options)
if choice == "S" then // player wants to stand if choice == "S" then // player wants to stand
Q[handNum] = evalHand(handNum) handValue[handNum] = evalHand(handNum)
if Q[handNum] == 21 then if handValue[handNum] == 21 and hands[handNum].len == 2 then
print "Blackjack" print "Blackjack"
S[handNum] += 1.5 * B[handNum] roundWinnings[handNum] += 1.5 * betPerHand[handNum]
B[handNum] = 0 betPerHand[handNum] = 0
discardHand handNum discardHand handNum
end if end if
break break
else if choice == "D" then // player wants to double down else if choice == "D" or choice == "H" then // hit or double down
print "Not yet supported!" handValue[handNum] = evalHand(handNum)
else if choice == "H" then // player wants to hit if choice == "D" then betPerHand[handNum] *= 2
Q[handNum] = evalHand(handNum)
card = getCard card = getCard
print "Received a " + cardNames[card] print "Received a " + cardNames[card], " "
P[handNum].push card hands[handNum].push card
Q[handNum] = evalHand(handNum) handValue[handNum] = evalHand(handNum)
if Q[handNum] < 0 then if handValue[handNum] < 0 then
print "...busted" print "...busted", ""
discardHand handNum discardHand handNum
end if end if
else if choice == "/" then // player wants to split print
print "Not yet supported!" if choice == "D" then break
else if choice == "/" then // split
card1 = hands[handNum][0]; if card1 > 10 then card1 = 10
card2 = hands[handNum][1]; if card2 > 10 then card2 = 10
if card1 != card2 then
print "Splitting not allowed."
continue
end if
hand2 = handNum + 8
hands[hand2] = [hands[handNum].pop]
betPerHand[hand2] = betPerHand[handNum]
card = getCard
print "First hand receives a " + cardNames[card]
hands[handNum].push card
card = getCard
print "Second hand receives a " + cardNames[card]
hands[hand2].push card
if card1 != 1 then
// Now play the two hands
playHand handNum, "Hand 1", false
playHand hand2, "Hand 2", false
end if
break
end if end if
allowSplit = false
end while end while
end function end function
discardHand = function(handNum) discardHand = function(handNum)
while P[handNum] hands[handNum] = []
D.push P[handNum].pop handValue[handNum] = 0
end while
Q[handNum] = 0
end function end function
tallyResults = function tallyResults = function
@@ -212,31 +237,28 @@ tallyResults = function
for i in range(0, numPlayers-1) for i in range(0, numPlayers-1)
playerHandATotal = displayTotal(evalHand(i)) playerHandATotal = displayTotal(evalHand(i))
playerHandBTotal = displayTotal(evalHand(i+8)) playerHandBTotal = displayTotal(evalHand(i+8))
// calculate S[i], which is the $ won/lost for player i // calculate roundWinnings[i], which is the $ won/lost for player i
S[i] = S[i] + B[i]*sign(playerHandATotal - dealerTotal) + B[i+8]*sign(playerHandBTotal - dealerTotal) roundWinnings[i] = roundWinnings[i] + betPerHand[i]*sign(playerHandATotal - dealerTotal) + betPerHand[i+8]*sign(playerHandBTotal - dealerTotal)
B[i+8] = 0 betPerHand[i+8] = 0
s = "Player " + (i+1) + " " s = "Player " + (i+1) + " "
s += ["loses", "pushes", "wins"][sign(S[i])+1] s += ["loses", "pushes", "wins"][sign(roundWinnings[i])+1]
if S[i] != 0 then s += " " + abs(S[i]) if roundWinnings[i] != 0 then s += " " + abs(roundWinnings[i])
T[i] += S[i] playerMoney[i] += roundWinnings[i]
T[numPlayers] -= S[i] playerMoney[numPlayers] -= roundWinnings[i]
s = (s + " "*20)[:20] + " Total = " + T[i] s = (s + " "*25)[:25] + "Total = " + playerMoney[i]
print s print s
discardHand i discardHand i
discardHand i+8 discardHand i+8
end for end for
print "Dealer's total = " + T[numPlayers] print "Dealer's total = " + playerMoney[numPlayers]
print print
end function end function
// Main program starts here // (Line 1500) // Main program starts here // (Line 1500)
// Initialize // Initialize
cardNames = "N A 2 3 4 5 6 7 8 9 10 J Q K".split cardNames = "n A 2 3 4 5 6 7 8 9 10 J Q K".split
inputs = "H,S,D,/,"
D = range(1,13) * 4
yesNo = getYesNo("Do you want instructions? ") if getYesNo("Do you want instructions? ") == "Y" then
if yesNo[0] == "Y" then
print "This is the game of 21. As many as 7 players may play the" print "This is the game of 21. As many as 7 players may play the"
print "game. On each deal, bets will be asked for, and the" print "game. On each deal, bets will be asked for, and the"
print "players' bets should be typed in. The cards will then be" print "players' bets should be typed in. The cards will then be"
@@ -249,6 +271,7 @@ if yesNo[0] == "Y" then
print "'H', unless the cards were split, in which case doubling" print "'H', unless the cards were split, in which case doubling"
print "down is again permitted. In order to collect for" print "down is again permitted. In order to collect for"
print "blackjack, the initial response should be 'S'." print "blackjack, the initial response should be 'S'."
print
end if end if
numPlayers = getNumber("Number of players? ", 1, 7) numPlayers = getNumber("Number of players? ", 1, 7)
print print