Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This is working sort of. the code seems to always be ccce, never anything else
- --and when guessing the code it only displays as CCCC
- term.clear()
- print("*** Code Breaker Game ***")
- print("How to play: ")
- print("Code is four letters a to f (all lowercase)")
- print("A correct letter is marked by O")
- print("An Incorrect letter is masked by X")
- print("Select difficulty")
- print("1-Normal")
- print('2-Hard')
- while true do
- local d = read()
- if d == "1" then
- trials = 6
- break
- end
- if d == "2" then
- trials = 5
- break
- end
- print('Invalid Number!')
- end
- length = 4
- print("You have " ..trials.. " trials.")
- print("")
- math.randomseed( os.time() )
- code = ""
- for i = 1, length do
- local c = string.byte('a') + math.random(5)
- code = code .. string.char( c )
- end
- trial = 1
- while true do
- local guess = read("Code - "..trial.."/"..trials.."?")
- mask = ""
- for i = 1, length do
- s = code:sub(i, i)
- d = guess:sub(i, i)
- if s == d then
- mask = mask .. "O"
- else
- mask = mask .. 'X'
- end
- end
- print(guess .. " -> " .. mask)
- if guess == code then
- print("You won!")
- break
- elseif trial == trials then
- print("You lose!")
- break
- end
- trial = trial + 1
- end
Advertisement
Add Comment
Please, Sign In to add comment