Guest User

codeBreak

a guest
Jun 24th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.25 KB | None | 0 0
  1. --This is working sort of. the code seems to always be ccce, never anything else
  2. --and when guessing the code it only displays as CCCC
  3.  
  4. term.clear()
  5. print("*** Code Breaker Game ***")
  6. print("How to play: ")
  7. print("Code is four letters a to f (all lowercase)")
  8. print("A correct letter is marked by O")
  9. print("An Incorrect letter is masked by X")
  10. print("Select difficulty")
  11. print("1-Normal")
  12. print('2-Hard')
  13. while true do
  14.   local d = read()
  15.   if d == "1" then
  16.     trials = 6
  17.     break
  18.   end
  19.   if d == "2" then
  20.    trials = 5
  21.    break
  22.  end
  23.  print('Invalid Number!')
  24. end
  25. length = 4
  26. print("You have " ..trials.. " trials.")
  27. print("")
  28. math.randomseed( os.time() )
  29. code = ""
  30. for i = 1, length do
  31.   local c = string.byte('a') + math.random(5)
  32.   code = code .. string.char( c )
  33. end
  34. trial = 1
  35.  
  36. while true do
  37.   local guess = read("Code - "..trial.."/"..trials.."?")
  38.   mask = ""
  39.   for i = 1, length do
  40.     s = code:sub(i, i)
  41.     d = guess:sub(i, i)
  42.     if s == d then
  43.       mask = mask .. "O"
  44.     else
  45.       mask = mask .. 'X'
  46.     end
  47.   end
  48.   print(guess .. " -> " .. mask)
  49.     if guess == code then
  50.       print("You won!")
  51.       break
  52.     elseif trial == trials then
  53.       print("You lose!")
  54.       break
  55.     end
  56.     trial = trial + 1
  57. end
Advertisement
Add Comment
Please, Sign In to add comment