Advertisement
Steelsouls

Steelsouls_Blackjack_0.9

Jan 19th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. print("Welcome to the Card Game. It looks like we are playing Blackjack :P")
  2.  
  3. --Tables are for each player's cards, Score is the number score for each player's hand, Suits/FaceCards are just for flavor (for now) - will be used in a later version to limit game to an actual number of decks (1-7). Oh and betting will hopefully be added at somepoint.  Oh oh and maybe a GUI! That would be sweet
  4.  
  5. math.randomseed(os.time())
  6. math.random(); math.random(); math.random()
  7. sF = string.format
  8. mR = math.random
  9. local tFaces = {10,Jack,Queen,King}
  10. local tSuits = {Clubs,Spades,Hearts,Diamonds}
  11. local tPHand = {}
  12. local tDHand = {}
  13. local nPScore = 0
  14. local nDScore = 0
  15. local bDStay = false
  16. local bPStay = false
  17.  
  18. function fDraw() --Returns 1 random card value
  19.   return mR(2,11)
  20. end
  21.  
  22. function fPrintCard(sPlayer,nCardNumber) --Print card player just drew, input is name of Table (tDHand or tPHand) and Count of table
  23.   local sFace = tFaces[mR(1,4)]
  24.   local sSuit = tSuits[mR(1,4)]
  25.   if sPlayer == "tDHand" then
  26.     if tDHand[nCardNumber] == 10 then
  27.       print("Dealer drew a "..sFace.." of "..sSuit..".")
  28.     elseif tDHand[nCardNumber] == 11 then
  29.       print("Dealer drew an Ace of "..sSuit..".")
  30.     else
  31.       print("Dealer drew a(n) "..tDHand[nCardNumber].." of "..sSuit..".")
  32.     end
  33.   else
  34.     if tPHand[nCardNumber] == 10 then
  35.       print("You drew a "..sFace.." of "..sSuit..".")
  36.     elseif tPHand[nCardNumber] == 11 then
  37.       print("You drew an Ace of "..sSuit..".")
  38.     else
  39.       print("You drew a(n) "..tPHand[nCardNumber].." of "..sSuit..".")
  40.     end
  41.   end
  42. end
  43.  
  44. function fFirstDraw() --Initial draw for dealer and player
  45.   tDHand{fDraw(),fDraw()}
  46.   nDScore = tDHand[1] + tDHand[2]
  47.   fPrintCard(tDHand,2)
  48.   tPHand{fDraw(),fDraw()}
  49.   nPScore = tPHand[1] + tPHand[2]
  50.   fPrintCard(tPHand,1)
  51.   fPrintCard(tPHand,2)
  52. end
  53.  
  54. function fBust(sPlayer) --When either player or dealer bust
  55.  
  56. end
  57.  
  58. function fWin(sPlayer) --When either player or dealer wins
  59.  
  60. end
  61.  
  62.  
  63. function fHit(sPlayer) --Hit function for dealer or player, input is name of Table (tDHand or tPHand), Returns nScore
  64.   table.insert(sPlayer,mR(2,11))
  65.   fPrintCard(sPlayer,#sPlayer)
  66.   nScore = 0 --Calculate score
  67.   for i = 1,#sPlayer do
  68.     nScore = nScore + sPlayer[i]
  69.   end
  70.   if nScore > 21 then --Check for Aces causing bust
  71.     for i=1,#sPlayer do
  72.       if sPlayer[i] == 11 then
  73.         sPlayer[i] = 1
  74.         nScore = nScore - 10
  75.       end
  76.     end
  77.   end
  78.   if nScore > 21 and sPlayer == "tDHand" then --Check if hit caused Bust
  79.     fBust(dealer)
  80.   elseif nScore > 21 and sPlayer == "tPHand" then
  81.     fBust(you)
  82.   end
  83.   return nScore
  84. end
  85.  
  86. function fDealerAI() --Dealer decides to hit or stay
  87.   nDScore = 0 --Calculate Dealer Score
  88.   for i = 1,#tDHand do
  89.     nDScore = nDScore + tDHand[1]
  90.   end
  91.   if nDScore > 17 then bDStay = true else bDStay = false end
  92.   return bDStay
  93. end
  94.  
  95. function fStartGame()
  96.   print("Dealer draws her first card face down.")
  97.   fFirstDraw()
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement