Running = true GameOver = false Wallet = 0 Cards = {} math.randomseed(os.time()) function IsAnInt(input) return tonumber(input, 10) ~= nil end function GetIntInput(prompt) while true do print(prompt .. " ") ans = io.read(); if IsAnInt(ans) then return tonumber(ans, 10) end print("YOU DID NOT ENTER AN INTEGER.") end end function IsEmpty(string) return string == nil or string == '' end function AskQuestion(prompt) while true do print(prompt .. "?") ans = io.read() if IsEmpty(ans) then print("PLEASE ANSWER THE QUESTION.") else return string.upper(ans) end end end function AskForBet() while true do ans = GetIntInput("WHAT IS YOUR BET?") if ans < 0 then print("PLEASE BET A NON-NEGATIVE AMOUNT.") elseif ans > Wallet then print("SORRY MY FRIEND, BUT YOU BET TOO MUCH.") print("YOU HAVE ONLY " ..Wallet.. " DOLLARS TO BET.") else return ans; end end end function AskToPlayAgain() while true do ans = AskQuestion("DO YOU WANT TO PLAY AGAIN") if ans == "YES" or ans == "Y" then return true elseif ans == "NO" or ans == "N" then return false else print("PLEASE ANSWER \"YES\" OR \"NO\"") end end end function AskToSeeTheRules() while true do ans = AskQuestion("DO YOU WANT TO SEE THE RULES") if ans == "YES" or ans == "Y" then return true elseif ans == "NO" or ans == "N" then return false else print("PLEASE ANSWER \"YES\" OR \"NO\"") end end end function DisplayCard(card) if card == 11 then print("JACK") elseif card == 12 then print("QUEEN") elseif card == 13 then print("KING") elseif card == 14 then print("ACE") else print(card) end end function DisplayTheRules() print("ACEY DUCEY IS PLAYED IN THE FOLLOWING MANNER:") print("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP") print("YOU HAVE THE OPTION TO BET OR NOT BET DEPENDING") print("ON WHETHER OR NOT YOU FEEL THE NEXT CARD WILL") print("HAVE A VALUE BETWEEN THE FIRST TWO.") print("") print("IF YOU DO NOT WANT TO BET, INPUT A 0 (ZERO).") end function GenerateRandomNumber(minimum, maximum, show) number = math.random(minimum, maximum) if show == true then print(number) end return number end function SelectCard() while true do card = GenerateRandomNumber(2, 14, false) if card ~= Cards[0] and card ~= Cards[1] and card ~= Cards[2] then DisplayCard(card) return card end end end print("*** ACEY DUCEY : LUA ***") if AskToSeeTheRules() then DisplayTheRules() end while Running == true do Wallet = 100 Cards = {0,0,0} GameOver = false while GameOver == false do print("YOU NOW HAVE " ..Wallet.. " DOLLARS.") Cards[0] = SelectCard() Cards[1] = SelectCard() Bet = AskForBet() if Bet > 0 then Cards[2] = SelectCard() if (Cards[2] > Cards[0] and Cards[2] < Cards[1]) or (Cards[2] > Cards[1] and Cards[2] < Cards[0]) then print("YOU WIN!!!") Wallet = Wallet + Bet else print("SORRY, YOU LOSE.") Wallet = Wallet - Bet end if Wallet < 1 then print("SORRY FRIEND, YOU ARE BROKE.") GameOver = true end else GameOver = true end end print("*** GAME OVER ***") Running = AskToPlayAgain() end print("OK, HOPE YOU HAD FUN.") print("THANKS FOR PLAYING!")