Rolcam

Computercraft Tutorial - Section 3 - Test

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