Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Welcome to the Card Game. It looks like we are playing Blackjack :P")
- --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
- math.randomseed(os.time())
- math.random(); math.random(); math.random()
- sF = string.format
- mR = math.random
- local tFaces = {10,Jack,Queen,King}
- local tSuits = {Clubs,Spades,Hearts,Diamonds}
- local tPHand = {}
- local tDHand = {}
- local nPScore = 0
- local nDScore = 0
- local bDStay = false
- local bPStay = false
- function fDraw() --Returns 1 random card value
- return mR(2,11)
- end
- function fPrintCard(sPlayer,nCardNumber) --Print card player just drew, input is name of Table (tDHand or tPHand) and Count of table
- local sFace = tFaces[mR(1,4)]
- local sSuit = tSuits[mR(1,4)]
- if sPlayer == "tDHand" then
- if tDHand[nCardNumber] == 10 then
- print("Dealer drew a "..sFace.." of "..sSuit..".")
- elseif tDHand[nCardNumber] == 11 then
- print("Dealer drew an Ace of "..sSuit..".")
- else
- print("Dealer drew a(n) "..tDHand[nCardNumber].." of "..sSuit..".")
- end
- else
- if tPHand[nCardNumber] == 10 then
- print("You drew a "..sFace.." of "..sSuit..".")
- elseif tPHand[nCardNumber] == 11 then
- print("You drew an Ace of "..sSuit..".")
- else
- print("You drew a(n) "..tPHand[nCardNumber].." of "..sSuit..".")
- end
- end
- end
- function fFirstDraw() --Initial draw for dealer and player
- tDHand{fDraw(),fDraw()}
- nDScore = tDHand[1] + tDHand[2]
- fPrintCard(tDHand,2)
- tPHand{fDraw(),fDraw()}
- nPScore = tPHand[1] + tPHand[2]
- fPrintCard(tPHand,1)
- fPrintCard(tPHand,2)
- end
- function fBust(sPlayer) --When either player or dealer bust
- end
- function fWin(sPlayer) --When either player or dealer wins
- end
- function fHit(sPlayer) --Hit function for dealer or player, input is name of Table (tDHand or tPHand), Returns nScore
- table.insert(sPlayer,mR(2,11))
- fPrintCard(sPlayer,#sPlayer)
- nScore = 0 --Calculate score
- for i = 1,#sPlayer do
- nScore = nScore + sPlayer[i]
- end
- if nScore > 21 then --Check for Aces causing bust
- for i=1,#sPlayer do
- if sPlayer[i] == 11 then
- sPlayer[i] = 1
- nScore = nScore - 10
- end
- end
- end
- if nScore > 21 and sPlayer == "tDHand" then --Check if hit caused Bust
- fBust(dealer)
- elseif nScore > 21 and sPlayer == "tPHand" then
- fBust(you)
- end
- return nScore
- end
- function fDealerAI() --Dealer decides to hit or stay
- nDScore = 0 --Calculate Dealer Score
- for i = 1,#tDHand do
- nDScore = nDScore + tDHand[1]
- end
- if nDScore > 17 then bDStay = true else bDStay = false end
- return bDStay
- end
- function fStartGame()
- print("Dealer draws her first card face down.")
- fFirstDraw()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement