Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.pullEvent = os.pullEventRaw
- print ("Please enter a lower number for number of digits")
- local default_length = 4
- local cheat = false
- local function numberToTable(num)
- if not num or not tostring(num) then
- error("Input should be a number")
- end
- local str = tostring(num)
- local tbl = {}
- for i=1,#str do
- table.insert(tbl,string.sub(str,i,i))
- end
- return tbl
- end
- local function layoutRead(str)
- --local w,h = term.getSize()
- --print(string.rep("=",w))
- print(str)
- local num = read()
- --write(string.rep("=",w))
- return num
- end
- local function getNumberToGuess(length)
- length = tonumber(length) or default_length
- if not type(length) == "number" then
- error("Length of number to guess should be a number.")
- end
- local num = math.random(math.pow(10,length-1), math.pow(10,length)-1)
- if cheat then
- print("Number to guess is: " .. tonumber(num))
- end
- return numberToTable(num)
- end
- local function getCorrectCount(correct, num)
- local toCompare = numberToTable(num)
- if(#toCompare ~= #correct) then
- return nil
- end
- local count = 0
- for i=1,#toCompare do
- if(toCompare[i] == correct[i]) then
- count = count + 1
- end
- end
- return count
- end
- local function printIntro()
- end
- local function checkAnswer(correct)
- local count = nil
- local guess
- while not count do
- guess = layoutRead("Please enter a guess: ")
- count = getCorrectCount(correct, guess)
- if not count then
- write("The number should be of length " .. tostring(#correct) .. ". ")
- end
- end
- print(tostring(count) .. " of the ciphers were correct.")
- return count == #correct
- end
- local function main()
- math.randomseed(os.time()*os.clock())
- write("How long should the number be? ")
- local length = read()
- local numberToGuess = getNumberToGuess(length)
- printIntro()
- local tries = 0
- while not checkAnswer(numberToGuess) do
- tries = tries + 1
- end
- print("You won with " .. tostring(tries) .. " tries!")
- sleep(3)
- shell.run("menu")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement