Advertisement
johnnic431

Morse Teacher ComputerCraft

Oct 18th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. --Morse code "Teacher" program
  2.  
  3. morse={"._","_...","_._.","_..",".",".._.","__.","....","..",".___","_._","._..","__","_.","___",".__.","__._","._.","...","_",".._","..._",".__","_.._","_.__","__..",".____","..___","...__","...._",".....","_....","__...","___..","____.","_____"}
  4. letters={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"}
  5.  
  6. function hws()
  7.     if fs.exists(".morsestats") and not fs.isDir(".morsestats") then
  8.         r=fs.open(".morsestats","r")
  9.         y=r.readLine()
  10.         if tonumber(y) then
  11.             hs=tonumber(y)
  12.         else
  13.             hs=0
  14.         end
  15.         r.close()
  16.     else
  17.         hs=0
  18.     end
  19.     return hs
  20. end
  21.  
  22. function clear()
  23.     shell.run("clear")
  24. end
  25.  
  26. function menu(hs)
  27.     clear()
  28.     tab={"Learn","Test","Exit"," "," ","Highscore: "..hs,"By NNet Inc.(Sylvyrfysh) (C)2013. Full terms of use can be found at nnet.org/programs/tc/morse"}
  29.     for t,y in pairs(tab) do
  30.         print(y)
  31.     end
  32.     while true do
  33.         e={os.pullEvent("mouse_click")}
  34.         if e[4]>3 then
  35.            
  36.         else
  37.             return tab[e[4]]
  38.         end
  39.     end
  40. end
  41.  
  42. canRun=true
  43. while canRun do
  44.     notBroken=true
  45.     hs=hws()
  46.     dothing=menu(hs)
  47.     if dothing=="Learn" then
  48.         prnts={}
  49.         for t=1,#morse do
  50.             table.insert(prnts,string.gsub(morse[t],"_","-").." = "..letters[t])
  51.         end
  52.         e,v=term.getSize()
  53.         x=1
  54.         while true do
  55.             clear()
  56.             for t=1,v-1 do
  57.                 print(prnts[x])
  58.                 x=x+1
  59.             end
  60.             write("Exit")
  61.             p={os.pullEvent()}
  62.             if p[1]=="mouse_scroll" then
  63.                 if p[2]==1 then
  64.                     x=19
  65.                 else
  66.                     x=1
  67.                 end
  68.             elseif p[1]=="mouse_click" and p[4]==v then
  69.                 break
  70.             end
  71.         end
  72.     elseif dothing=="Test" then
  73.         clear()
  74.         term.setCursorPos(1,1)
  75.         print("Enter highest letter, or 'all' for all letters: ")
  76.         u=read()
  77.         if u=="all" then
  78.             highest=#morse
  79.         else
  80.             for t,y in pairs(letters) do
  81.                 if y==u then
  82.                     highest=t
  83.                     break
  84.                 end
  85.                 highest=#morse
  86.             end
  87.         end
  88.         clear()
  89.         print("Press '-' for a long tap, or '.' for a short tap.")
  90.         write("Loading.")
  91.         for o=1,5 do
  92.             write(".")
  93.             sleep(1)
  94.         end
  95.         score=0
  96.         while true do
  97.             n=math.random(1,highest)
  98.             clear()
  99.             print(morse[n].." is: Or enter 'exit' to exit.")
  100.             print("Score: "..score)
  101.             term.setCursorPos(1,3)
  102.             g=read()
  103.             if g=="exit" then
  104.                 break
  105.             else
  106.                 if g==letters[n] then
  107.                     score=score+1
  108.                     if score>hs then
  109.                         if notBroken then
  110.                             print("NEW HIGHSCORE!")
  111.                         end
  112.                         r=fs.open(".morsestats","w")
  113.                         r.write(score)
  114.                         r.close()
  115.                         notBroken=false
  116.                     end
  117.                     print("Great!")
  118.                     sleep(2)
  119.                    
  120.                 else
  121.                     print("Oops! That letter was "..letters[n])
  122.                     sleep(2)
  123.                 end
  124.             end
  125.         end
  126.     elseif dothing=="Exit" then
  127.         canRun=false
  128.         clear()
  129.         print("Thanks for using Morse by NNet(Sylvyrfysh)!")
  130.     end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement