-- GAME LIBRARY START -- Running = true GameOver = false 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 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 GenerateRandomNumber(minimum, maximum, show) number = math.random(minimum, maximum) if show == true then print(number) end return number end -- GAME LIBRARY END -- Digits = {0,0,0} Score = 0 Attempt = 0 Guess = 0 Guessed = false function MakeIntFromArray(arrayIn) return (arrayIn[1] * 100) + (arrayIn[2] * 10) + arrayIn[3] end function MakeArrayFromInt(valIn) retArray = {0,0,0} retArray[1] = math.floor(valIn / 100) retArray[2] = math.floor((valIn - (retArray[1] * 100)) / 10) retArray[3] = valIn - (retArray[1] * 100) - (retArray[2] * 10) return retArray end function AskForGuess() Guess = GetIntInput("GUESS #" ..Attempt) if Guess < 0 then print("PLEASE GUESS A NON-NEGATIVE NUMBER.") elseif Guess < 100 or Guess > 999 then print("PLEASE GUESS A NUMBER BETWEEN 100 AND 999.") end end function DisplayTheRules() print("I WILL THINK OF A THREE-DIGIT NUMBER. YOU WILL TRY TO") print("GUESS MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:") print(" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION") print(" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION") print(" BAGELS - NO DIGITS ARE CORRECT") print("") print("YOU GET TWENTY TRIES BEFORE YOU LOSE.") print("") end function DisplayGameOver() number = MakeIntFromArray(Digits) print("OH WELL, THAT'S TWENTY GUESSES.") print("MY NUMBER WAS " ..number.. ".") end function CreateTheNumber() Digits[1] = GenerateRandomNumber(1, 9, false) gotDigit = false while not gotDigit do Digits[2] = GenerateRandomNumber(0, 9 , false) if(Digits[2] ~= Digits[1]) then gotDigit = true end end gotDigit = false while not gotDigit do Digits[3] = GenerateRandomNumber(0, 9, false) if(Digits[3] ~= Digits[1] and Digits[3] ~= Digits[2]) then gotDigit = true end end end function CheckGuess() temp = MakeArrayFromInt(Guess) vals = {0,0,0} if temp[1] == Digits[1] then vals[1] = 1 end if temp[2] == Digits[2] then vals[2] = 1 end if temp[3] == Digits[3] then vals[3] = 1 end if MakeIntFromArray(vals) == 111 then return true end if vals[1] == 0 then if temp[1] == Digits[2] or temp[1] == Digits[3] then vals[1] = 2 end end if vals[2] == 0 then if temp[2] == Digits[1] or temp[2] == Digits[3] then vals[2] = 2 end end if vals[3] == 0 then if temp[3] == Digits[1] or temp[3] == Digits[2] then vals[3] = 2 end end if MakeIntFromArray(vals) == 0 then print("BAGELS") else display = "" if vals[1] == 1 then display = "FERMI " elseif vals[1] == 2 then display = "PICO " end if vals[2] == 1 then display = display .. "FERMI " elseif vals[2] == 2 then display = display .. "PICO " end if vals[3] == 1 then display = display .. "FERMI" elseif vals[3] == 2 then display = display .. "PICO" end print(display) end return false end print("*** BAGELS : LUA ***") if AskToSeeTheRules() then DisplayTheRules() end while Running == true do Attempt = 1 CreateTheNumber() print("OK, I HAVE A NUMBER IN MIND.") GameOver = false while GameOver == false do AskForGuess() Guessed = CheckGuess() if Guessed then print("YOU GOT IT!!!") Score = Score + 1 GameOver = true else Attempt = Attempt + 1 if Attempt > 20 then DisplayGameOver() GameOver = true end end end print("*** GAME OVER ***") Running = AskToPlayAgain() end print("A " ..Score.. " POINT BAGELS BUFF!") print("HOPE YOU HAD FUN. BYE!")