--- --- Bagels --- Ported by Joe Nellis. --- Text displayed is altered slightly from the original program to allow for --- more (or less) than three digits to be guessed. Change the difficulty to --- the number of digits you wish in the secret code. --- --- difficult is number of digits to use local difficulty = 3 print [[ BAGELS CREATIVE COMPUTING MORRISTOWN, NEW JERSEY ]] function getInput(prompt) io.write(prompt) io.flush() local input = io.read("l") if not input then --- test for EOF print("GOODBYE") os.exit(0) end return input end local needsRules = getInput("WOULD YOU LIKE THE RULES? (YES OR NO) ") print() if needsRules:match("[yY].*") then print(string.format( [[ I AM THINKING OF A %u DIGIT NUMBER. TRY TO GUESS MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS: PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION BAGELS - NO DIGITS ]], difficulty)) end function play(numDigits, score) local digits = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" } --- secret number must not have duplicate digits --- randomly swap numDigits at the head of this list to create secret number for i = 1, numDigits do local j = math.random(1, 10) digits[i], digits[j] = digits[j], digits[i] end print "O.K. I HAVE A NUMBER IN MIND." for guessNum = 1, 20 do :: GUESS_AGAIN :: --- 0) then print("A", score, "POINT BAGELS BUFF!!") end print "HOPE YOU HAD FUN. BYE." end end play(difficulty, 0) --- default is numDigits=3, score=0