From 882117758dde6347cd9d65da37e2e978a454bb73 Mon Sep 17 00:00:00 2001 From: Joe Nellis Date: Wed, 13 Apr 2022 02:57:15 -0700 Subject: [PATCH] Bagels in Lua. --- 05_Bagels/lua/Bagels.lua | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 05_Bagels/lua/Bagels.lua diff --git a/05_Bagels/lua/Bagels.lua b/05_Bagels/lua/Bagels.lua new file mode 100644 index 00000000..57c35297 --- /dev/null +++ b/05_Bagels/lua/Bagels.lua @@ -0,0 +1,121 @@ +--- +--- 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 +