From e0a417bf8ac5d0695b2ff7fce726a94a9ece40fb Mon Sep 17 00:00:00 2001 From: JoeStrout Date: Wed, 19 Jul 2023 18:14:30 -0700 Subject: [PATCH] Finished the blackjack port to MiniScript. --- .../10_Blackjack/MiniScript/blackjack.ms | 175 ++++++++++-------- 1 file changed, 99 insertions(+), 76 deletions(-) diff --git a/00_Alternate_Languages/10_Blackjack/MiniScript/blackjack.ms b/00_Alternate_Languages/10_Blackjack/MiniScript/blackjack.ms index bbbb9257..98bb0b90 100644 --- a/00_Alternate_Languages/10_Blackjack/MiniScript/blackjack.ms +++ b/00_Alternate_Languages/10_Blackjack/MiniScript/blackjack.ms @@ -4,37 +4,34 @@ print " "*15 + "Creative Computing Morristown, New Jersey" print; print; print fna = function(q); return q - 11*(q >= 22); end function -P = [[]]*15 // P(i,j) is the jth card in hand i -Q = [0]*15 // Q(i) is the total value of hand i -C = [] // the deck being dealt from -D = [] // the discard pile -T = [0]*8 // total $ for each player -S = [0]*7 // total $ won/lost this hand for each player -B = [0]*15 // bet for each hand -Z = [0]*7 // insurance for each player +hands = [[]]*15 // hands(i,j) is the jth card in hand i (P) +handValue = [0]*15 // total value of each hand (Q) +deck = [] // the deck being dealt from (C) +playerMoney = [0]*8 // total $ for each player (T) +roundWinnings = [0]*7 // total $ won/lost this hand for each player (S) +betPerHand = [0]*15 // bet for each hand (B) reshuffle = function print "Reshuffling" - globals.C = D - globals.D = [] - C.shuffle + globals.deck = range(1,13)*4 + deck.shuffle end function // Function to draw a card from the deck. getCard = function // line 100 - if not C then reshuffle - return C.pop + if not deck then reshuffle + return deck.pop end function // 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 // 11-21...Soft 11-21 // 22-32...Hard 11-21 // 33+ or -1...Busted evalHand = function(handNum) // line 300 result = 0 - for card in P[handNum] + for card in hands[handNum] result = addCardToTotal(result, card) end for return result @@ -42,6 +39,7 @@ end function // Function to add a card into a total hand value. addCardToTotal = function(total, card) // line 500 + if total < 0 then return total // (already busted) x1 = card; if x1 > 10 then x1 = 10 q1 = total + x1 if total < 11 then @@ -79,6 +77,7 @@ getNumber = function(prompt, minVal=0, maxVal=500) while true result = input(prompt).val if result == floor(result) and minVal <= result <= maxVal then return result + print "Please enter a number from " + minVal + " to " + maxVal end while end function @@ -94,117 +93,143 @@ end function getBets = function print "Bets:" for i in range(0, numPlayers-1) - B[i] = getNumber("# " + (i+1) + "? ", 1, 500) + betPerHand[i] = getNumber("# " + (i+1) + "? ", 1, 500) end for end function playOneRound = function - globals.Z = [0] * numPlayers - globals.S = [0] * numPlayers - if C.len < (numPlayers+1) * 2 then reshuffle + globals.roundWinnings = [0] * numPlayers + if deck.len < (numPlayers+1) * 2 then reshuffle getBets print "PLAYER ", "" for i in range(0, numPlayers-1) print i+1, " " - P[i] = [] + hands[i] = [] end for print "DEALER" - P[numPlayers] = [] + hands[numPlayers] = [] for row in [1,2] print " ", "" for i in range(0, numPlayers) - P[i].push getCard - if row == 1 or i < numPlayers then printCard P[i][-1] + hands[i].push getCard + if row == 1 or i < numPlayers then printCard hands[i][-1] end for print end for + dealerCard0 = hands[numPlayers][0] + dealerCard1 = hands[numPlayers][1] // 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" for i in range(0, numPlayers-1) - Z[i] = getNumber("# " + (i+1) + "? ", 0, B[i]/2) - S[i] = Z[i] * (3 * (P[numPlayers][1] >= 10) - 1) // ?!? + insurance = getNumber("# " + (i+1) + "? ", 0, betPerHand[i]/2) + roundWinnings[i] = insurance * (3 * (dealerCard1 >= 10) - 1) end for end if // Test for dealer blackjack - dealerCard0 = P[numPlayers][0] - dealerCard1 = P[numPlayers][1] if (dealerCard0==1 and dealerCard1 > 9) or (dealerCard0 > 9 and dealerCard1==1) then print; print "Dealer has a " + cardNames[dealerCard1] + " in the hole for Blackjack" for i in range(0, numPlayers) - Q[i] = evalHand(i) + handValue[i] = evalHand(i) end for else // no dealer blackjack + if dealerCard0 == 1 or dealerCard0 >= 10 then + print; print "No dealer Blackjack." + end if // now play the hands for i in range(0, numPlayers-1) playHand i 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 // there are any player hands with cards left. anyLeft = false 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 if not anyLeft then - print "Dealer had a " + cardNames[P[numPlayers][1]] + " concealed." + print "Dealer had a " + cardNames[hands[numPlayers][1]] + " concealed." else // Play dealer's hand. - dispTotal = displayTotal(Q[numPlayers]) - print "Dealer has a " + cardNames[P[numPlayers][1]] + " concealed" + + dispTotal = displayTotal(handValue[numPlayers]) + print "Dealer has a " + cardNames[hands[numPlayers][1]] + " concealed" + " for a total of " + dispTotal + "." - while Q[numPlayers] > 0 and dispTotal <= 16 - if P[numPlayers].len == 2 then print "Draws ", "" + while handValue[numPlayers] > 0 and dispTotal <= 16 card = getCard + if hands[numPlayers].len == 2 then print "Draws ", "" print cardNames[card], " " - P[numPlayers].push card - Q[numPlayers] = evalHand(numPlayers) - dispTotal = displayTotal(Q[numPlayers]) + hands[numPlayers].push card + handValue[numPlayers] = evalHand(numPlayers) + dispTotal = displayTotal(handValue[numPlayers]) 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 end if end if tallyResults end function -playHand = function(handNum) - while P[handNum] - choice = getOption("Player " + (handNum % 8 + 1) + "? ", ["H", "S", "D", "/"]) +playHand = function(handNum, prompt=null, allowSplit=true) + if not prompt then prompt = "Player " + (handNum % 8 + 1) + while hands[handNum] + options = ["H", "S", "D"] + ["/"] * allowSplit + choice = getOption(prompt + "? ", options) if choice == "S" then // player wants to stand - Q[handNum] = evalHand(handNum) - if Q[handNum] == 21 then + handValue[handNum] = evalHand(handNum) + if handValue[handNum] == 21 and hands[handNum].len == 2 then print "Blackjack" - S[handNum] += 1.5 * B[handNum] - B[handNum] = 0 + roundWinnings[handNum] += 1.5 * betPerHand[handNum] + betPerHand[handNum] = 0 discardHand handNum end if break - else if choice == "D" then // player wants to double down - print "Not yet supported!" - else if choice == "H" then // player wants to hit - Q[handNum] = evalHand(handNum) + else if choice == "D" or choice == "H" then // hit or double down + handValue[handNum] = evalHand(handNum) + if choice == "D" then betPerHand[handNum] *= 2 card = getCard - print "Received a " + cardNames[card] - P[handNum].push card - Q[handNum] = evalHand(handNum) - if Q[handNum] < 0 then - print "...busted" + print "Received a " + cardNames[card], " " + hands[handNum].push card + handValue[handNum] = evalHand(handNum) + if handValue[handNum] < 0 then + print "...busted", "" discardHand handNum end if - else if choice == "/" then // player wants to split - print "Not yet supported!" + print + 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 + allowSplit = false end while end function discardHand = function(handNum) - while P[handNum] - D.push P[handNum].pop - end while - Q[handNum] = 0 + hands[handNum] = [] + handValue[handNum] = 0 end function tallyResults = function @@ -212,31 +237,28 @@ tallyResults = function for i in range(0, numPlayers-1) playerHandATotal = displayTotal(evalHand(i)) playerHandBTotal = displayTotal(evalHand(i+8)) - // calculate S[i], which is the $ won/lost for player i - S[i] = S[i] + B[i]*sign(playerHandATotal - dealerTotal) + B[i+8]*sign(playerHandBTotal - dealerTotal) - B[i+8] = 0 + // calculate roundWinnings[i], which is the $ won/lost for player i + roundWinnings[i] = roundWinnings[i] + betPerHand[i]*sign(playerHandATotal - dealerTotal) + betPerHand[i+8]*sign(playerHandBTotal - dealerTotal) + betPerHand[i+8] = 0 s = "Player " + (i+1) + " " - s += ["loses", "pushes", "wins"][sign(S[i])+1] - if S[i] != 0 then s += " " + abs(S[i]) - T[i] += S[i] - T[numPlayers] -= S[i] - s = (s + " "*20)[:20] + " Total = " + T[i] + s += ["loses", "pushes", "wins"][sign(roundWinnings[i])+1] + if roundWinnings[i] != 0 then s += " " + abs(roundWinnings[i]) + playerMoney[i] += roundWinnings[i] + playerMoney[numPlayers] -= roundWinnings[i] + s = (s + " "*25)[:25] + "Total = " + playerMoney[i] print s discardHand i discardHand i+8 end for - print "Dealer's total = " + T[numPlayers] + print "Dealer's total = " + playerMoney[numPlayers] print end function // Main program starts here // (Line 1500) // Initialize -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 +cardNames = "n A 2 3 4 5 6 7 8 9 10 J Q K".split -yesNo = getYesNo("Do you want instructions? ") -if yesNo[0] == "Y" then +if getYesNo("Do you want instructions? ") == "Y" then 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 "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 "down is again permitted. In order to collect for" print "blackjack, the initial response should be 'S'." + print end if numPlayers = getNumber("Number of players? ", 1, 7) print