Rolcam

Computercraft Tutorial - Section 2 - Test

Jun 14th, 2021 (edited)
581
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --This is part of a Computercraft Programming Teaching Program
  2.  
  3. for i = 1, #tSides do
  4.   monitor = peripheral.wrap(tSides[i])
  5.   if monitor then
  6.         side = tSides[i]
  7.         break
  8.   end
  9. end
  10.  
  11. -- Clears the monitor screen
  12. function reset()
  13.     term.redirect(peripheral.wrap(side))
  14.     term.clear()
  15.     term.restore()
  16. end
  17.  
  18.  
  19. loop = 0
  20. rep = 0
  21. prog = 0
  22.  
  23. -- Generates test 1
  24. function gen1()
  25.     print("Generating test program 1...")
  26.     e = fs.open("test", "w")
  27.     e.writeLine("[            ] <----- ")
  28.     e.writeLine("term.setCursorPos(1,1)")
  29.     e.writeLine("print(\"Hello World\")")
  30.     e.close()
  31.     print("Program Generated")
  32.     sleep(3)
  33. end
  34.  
  35. -- Generates test 2
  36. function gen2()
  37.     print("Generating test program 2...")
  38.     e = fs.open("test", "w")
  39.     e.writeLine("e = 5")
  40.     e.writeLine("while e > 0 do")
  41.     e.writeLine("term.clear()")
  42.     e.writeLine("term.setCursorPos(1,1)")
  43.     e.writeLine("e = e - 1")
  44.     e.writeLine("eTime = os.clock()")
  45.     e.writeLine("print(eTime)")
  46.     e.writeLine("[            ] <----- ")
  47.     e.writeLine("end")
  48.     e.close()
  49.     print("Program Generated")
  50.     sleep(3)
  51. end
  52.  
  53. -- Generates test 3
  54. function gen3()
  55.     print("Generating test program 3...")
  56.     e = fs.open("test", "w")
  57.     e.writeLine("term.clear()")
  58.     e.writeLine("term.setCursorPos(1,1)")
  59.     e.writeLine("Print(\"Hello\")")
  60.     if prog == 0 then
  61.         e.writeLine("[        ] <----- ")
  62.     else
  63.         e.writeLine("[sleep(3)]")
  64.     end
  65.     e.writeLine("print(\"World\")")
  66.     if prog == 1 then
  67.         e.writeLine("[           ] <----- ")
  68.     else
  69.         e.writeLine("[           ]")
  70.     end
  71.     e.writeLine("end")
  72.     e.close()
  73.     print("Program Generated")
  74.     sleep(3)
  75. end
  76.  
  77.  
  78. term.clear()
  79. term.setCursorPos(1,1)
  80. term.setTextColor(colors.purple)
  81. print("Lesson 2 Test - Basic Commands \n ")
  82. print("This is a basic knowledge test")
  83. print("You will be asked to complete a program by inserting a line of code")
  84. print("You aren't timed or scored and you have unlimited attempts \n ")
  85. print("Press the enter key when ready")
  86. read()
  87. gen1()
  88. loop = 1
  89. reset()
  90. -- Test 1 of 3
  91. while loop == 1 do
  92.     rep = 4
  93.     term.clear()
  94.     term.setCursorPos(1,1)
  95.     term.setTextColor(colors.purple)
  96.     print("Test 1 of 3\n ")
  97.     test = fs.open("test", "r")
  98.     while rep > 0 do
  99.         print(test.readLine())
  100.         rep = rep - 1
  101.         sleep(0.1)
  102.     end
  103.     print(" \nClear the screen")
  104.     print("Type \"help\" for a help or \"skip\" to skip")
  105.     input = read()
  106.     if input == "term.clear()" then
  107.         term.setTextColor(colors.lime)
  108.         print("Good Job, that's the right command!")
  109.         sleep(2)
  110.         loop = 0
  111.     elseif input == "clear" then
  112.         print("You're close, but not quite.")
  113.         print("That works on the terminal, but not in a program")
  114.         print("Try Again")
  115.         sleep(5)
  116.     elseif input == "help" then
  117.         print("Check the monitor for the lesson")
  118.         shell.run("section2")
  119.         sleep(2)
  120.     elseif input == "skip" then
  121.         print("Skipping Test")
  122.         loop = 0
  123.         sleep(2)
  124.     else
  125.         print("Not quite. Try again")
  126.         sleep(2)
  127.     end
  128. end
  129. gen2()
  130. loop = 1
  131. reset()
  132. -- Test 2 of 3
  133. while loop == 1 do
  134.     rep = 9
  135.     term.clear()
  136.     term.setCursorPos(1,1)
  137.     term.setTextColor(colors.purple)
  138.     print("Test 2 of 3\n ")
  139.     test = fs.open("test", "r")
  140.     while rep > 0 do
  141.         print(test.readLine())
  142.         rep = rep - 1
  143.         sleep(0.1)
  144.     end
  145.     print(" \nMake the program sleep for 2 seconds")
  146.     print("Type \"help\" for a help or \"skip\" to skip")
  147.     input = read()
  148.     if input == "sleep(2)" then
  149.         term.setTextColor(colors.lime)
  150.         print("Good Job, that's the right command!")
  151.         sleep(2)
  152.         loop = 0
  153.     elseif input == "help" then
  154.         print("Check the monitor for the lesson")
  155.         shell.run("section2")
  156.         sleep(2)
  157.     elseif input == "skip" then
  158.         print("Skipping Test")
  159.         loop = 0
  160.         sleep(2)
  161.     else
  162.         print("That's not quite right. Try again")
  163.         sleep(2)
  164.     end
  165. end
  166. gen3()
  167. loop = 1
  168. reset()
  169. -- Test 3 of 3
  170. while loop == 1 do
  171.     rep = 7
  172.     term.clear()
  173.     term.setCursorPos(1,1)
  174.     term.setTextColor(colors.purple)
  175.     print("Test 3 of 3\n ")
  176.     test = fs.open("test", "r")
  177.     while rep > 0 do
  178.         print(test.readLine())
  179.         rep = rep - 1
  180.         sleep(0.1)
  181.     end
  182.     if prog == 0 then
  183.         print("Make the program sleep for 3 seconds")
  184.         input = read()
  185.         if input == "sleep(3)" then
  186.             term.setTextColor(colors.lime)
  187.             print("Good Job, that's the right command!")
  188.             sleep(2)
  189.             prog = 1
  190.             reset()
  191.             gen3()
  192.         elseif input == "help" then
  193.             print("Check the monitor for the lesson")
  194.             shell.run("section2")
  195.             sleep(2)
  196.         elseif input == "skip" then
  197.             print("Skipping...")
  198.             prog = 1
  199.             gen3()
  200.             reset()
  201.             sleep(2)
  202.         else
  203.             print("That's not quite right. Try again")
  204.             sleep(2)
  205.         end
  206.     else
  207.         print("Make the computer shutdown")
  208.         input = read()
  209.         if input == "os.shutdown()" then
  210.             term.setTextColor(colors.lime)
  211.             print("Good Job, that's the right command!")
  212.             sleep(2)
  213.             loop = 0
  214.         elseif input == "os.shutdown" then
  215.             print("Close, but not quite. Try again")
  216.             sleep(3)
  217.         elseif input == "shutdown" then
  218.             print("That works in the terminal, but not in the program.")
  219.             print("Close, but not quite. Try again")
  220.             sleep(4)
  221.         elseif input == "help" then
  222.             print("Check the monitor for the lesson")
  223.             shell.run("section2")
  224.             sleep(2)
  225.         elseif input == "skip" then
  226.             print("Skipping Test")
  227.             loop = 0
  228.             sleep(2)
  229.         else
  230.             print("That's not quite right. Try again")
  231.             sleep(2)
  232.         end
  233.     end        
  234. end
  235. term.clear()
  236. term.setCursorPos(1,1)
  237. term.setTextColor(colors.lime)
  238. print("Congrats, you have competed all three tests")
  239. sleep(3)
  240.        
RAW Paste Data