Advertisement
Guest User

ComputerCraft Tutorial

a guest
Jan 14th, 2012
9,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 48.41 KB | None | 0 0
  1. -- BaseLine Variables
  2. CurrentChapter = 1
  3. CurrentSection = 1
  4. ChapterTitles = {
  5.  "How to Create a Program",
  6.  "How to Display and clear Text",
  7.  "How to Use and Display Variables",
  8.  "How to get User Input",
  9.  "How to use IF statements",
  10.  "How to use loops",
  11.  "How to use redstone",
  12.  "How to use redpower bundles",
  13.  "How to use events",
  14.  "How to use functions",
  15.  "Extra Tips and Extra Functions"
  16. }
  17. Chapter = {
  18.                  [1] = {
  19.                  "Key Points in this Chapter:\n1. Learning how to create a program.\n2. Learning how to save that program.\n3. Learning how to run that program.",
  20.                  "1.1 - Learning how to create a program.\n\nOk so first things first right? We gotta learn how to create our first program. Creating a program is very simple in CC.\n\nedit programname\n\nedit means we want to create or edit program, and programname is the name of the program we wish to edit or create.",
  21.                  "1.2 - Learning how to save that program.\n\nSo now your inside the editing feature of CC and you can move around almost like a notepad. We want to press the [Control] key which will bring up a menu at the bottom of your screen. Pressing the [Left] and [Right] arrow keys will change your selection in this menu. [SAVE] will save the program. [QUIT] will quit editing the program. By pressing [ENTER] we can choose our selection.",
  22.                  "1.3 - Learning how to run that program.\n\nWe've created our little program, but how do we run it? Well thats simple. We type the program name into our terminal and press [ENTER], but remember all things in LUA are case-sensitive. Your program named \"Hello\" is not the same as your program named \"hello\".",
  23.                  "1.4 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  24.                  "SIM"               
  25.                  },
  26.                  [2] = {
  27.                  "Key Points in this Chapter:\n1. How to use print\n2. How to use write\n3. How to clear the screen.\n4. How to use the cursor position.\n5. How to clear a specific line.",
  28.                  "2.1 - How to use print.\n\nTo show text to the user is a simple task. We do so by the following.\n\nprint (\"Hello User\")\n\nprint means to display the text to the user, and Hello User is the text we wish to display.",
  29.                  "2.2 - How to use write.\n\nWhen you print text, the program automatically adds a linebreak after your print command, sometimes you may not wish to do this.\n\nwrite (\"Hello\")\nprint (\"User\")\n\nThese two lines would make the exact same appearance as the print line we made previously. The reason is because the write command does not generate a linebreak and we can use this to keep our current position and continue writing on the same line.",
  30.                  "2.3 - How to clear the screen.\nQuite often you'll want to clear the screen automatically in your program.\n\nterm.clear()\n\nUsing this line we can clear the screen for our user to remove anything we don't want cluttering up the screen.",
  31.                  "2.4 - How to use the cursor position.\nThe cursor position is a very powerful thing. For example, when you clear the screen, the cursor still stays on it's previous line. Meaning that after you clear the screen, your next print statement very well may appear at the bottom of the screen.",
  32.                  "2.4 - How to use the cursor position.\nTo remedy this problem we've been given the command.\n\nterm.setCursorPos(1,1)\n\nThe first 1 in our statment is the horizontal position and the second 1 on our statement is the vertical posistion.",
  33.                  "2.4 - How to use the cursor position.\nBy using the term.setCursorPos(1,1) directly after a term.clear(), we can make sure that the next text we show the user appears at the top of his screen.\n\nRemember, lua is case sensitive. term.setcursorpos(1,1) is not right.",
  34.                  "2.5 - How to clear a specific line.\nBy using the term.setCursorPos we can create some pretty nifty tricks, like reprinting over lines already on the screen, or even clearing a certain line and printing something new.",
  35.                  "2.5 - How to clear a specific line.\nterm.setCursorPos(1,1)\nprint (\"You won't see this\")\nterm.setCursorPos(1,1)\nterm.clearLine()\nprint (\"Hello User\")\n\nWe used the term.clearLine() to remove the line at 1,1 and then we printed a new line where the old line used to be.",
  36.                  "2.6 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  37.                  "SIM"
  38.                  },
  39.                  [3] = {
  40.                  "Key Points in this Chapter:\n1. What is a variable\n2. How to use a variable\n3. How to display a variable\n4. How to convert a variable",
  41.                  "3.1 - What is a variable.\n\nThink of a variable as a container in which you can place text or a number. Using variables allows you to store information and modify it on the fly. Using variables correctly is the key to making a sucessful program.",
  42.                  "3.2 - How to use a variable.\n\nThe first thing we should almost always do when we want to use a variable in a program is to define the variable. We do this by giving the variable a default piece of data. If we don't define a variable then the variable is considered NIL until it has been set.",
  43.                  "3.2 - How to use a variable.\n\nA NIL variable can cause lots of problems in your program, if you try to add together 2 variables and one is NIL, you'll get an error, if you try printing a NIL variable you'll get an error.",
  44.                  "3.2 - How to use a variable.\n\nWe'll be using the variable x from this point onward. Remember that x is different then X in lua.\n\nx = 0\n\nThis defined x to have a default value of 0. You could also do x = \"hello\" if you wanted it to contain text.",
  45.                  "3.2 - How to use a variable.\n\nA variable will hold it's data until you change it. Therefor x = 0 means that x will be 0 until you tell your program that x is something different.",
  46.                  "3.2 - How to use a variable.\n\nYou can also set variables as booleans, booleans are true and false.\nx = true\nThis would set x to true, which is different then \"true\". Using booleans gives you 1 more way to define a variable to be used later in your program.",
  47.                  "3.3 - How to display a variable.\n\nBeing able to show a variable to the user is very important. Who wants to just save the user's name in a variable, but not show it to them during the program? When displaying variables we can use the print or write commands.",
  48.                  "3.3 - How to display a variable.\nx = 0\nprint (x)\nx = \"Bob\"\nprint (\"Hello\"..x)\n\nYou'll notice in the last line that we used our x variable along with another piece of text. To do so we used .. which tells the program to add the x variable directly after the word Hello. In this syntax that would show the user   HelloBob  since we didn't add a space ;)",
  49.                  "3.3 - How to display a variable.\n\nRemember variables are case sensitive, and that variable1 is different then Variable1.\nIf you wanted to place a variable inbetween text you could do.\n\nprint (\"Hello \"..x..\" how are you?\")\n\nThis would print   Hello Bob how are you?",
  50.                  "3.4 - How to convert a variable.\n\nSometimes a variable might need to be converted, this is mainly the case when you want to use the variable as a number, but it's currently a string. For example:\nx = \"0\"\nx = x + 1\n\nThis will not work, as x equals \"0\"",
  51.                  "3.4 - How to convert a variable.\n\nThe difference between 0 and \"0\" is that one is a number and the other is a string. You can't use the basic math functions on strings. Lua can convert a number to a string automatically but it cannot convert a string to a number automatically.",
  52.                  "3.4 - How to convert a variable.\n\nx = 0\ny = \"1\"\nWe can't add those together atm, so we need to convert our string to a number so that we can add it to x.\ny = tonumber(y)\nThis converts y to a number (if it can't be converted to a number then y will be NIL)",
  53.                  "3.4 - How to convert a variable.\n\nNow that we've converted y to a number we can do.\nx = x + y\nThis would make x equal to x(0) + y(1). This means that x is now equal to 1 and y hasn't changed so it's still 1 as well.",
  54.                  "3.4 - How to convert a variable.\n\nIf we want to add a string to another string, we don't use the math symbols, we simply use ..\nx = \"Hello\"\ny = \"Bob\"\nx = x..y\nThis would make x = \"HelloBob\"",
  55.                  "3.4 - How to convert a variable.\n\nRemember that Lua can convert a number to a string, but not the other way around. If you always want to be 100% positive what your variables are, use the functions tonumber(x) and tostring(x).",
  56.                  "3.5 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  57.                  "SIM"               
  58.                  },
  59.                  [4] = {
  60.                  "Key Points in this Chapter:\n1. How to get user input",
  61.                  "4.1 - How to get user input.\n\nWhat's the point of having all these cool variables if your user can't make a variable be something they want? We fix this by allowing the user to input a variable into the program.",
  62.                  "4.1 - How to get user input.\n\nx = io.read()\nThe io.read() tells the program to stop and wait for user input, when the user presses enter that information is then stored in the variable x and the program continues.\nUser input is always stored as a string, therefor if the user types 1 and presses enter, x will be come \"1\", not 1",
  63.                  "4.1 - How to get user input.\n\nOnce the user's input is entered into the x variable you can use that variable like you would any other variable. This means if you then wanted to show the user their input you could follow your io.read() line with print(x)",
  64.                  "4.1 - How to get user input.\n\nBy using the write command before an io.read() we can show text then have the user type after that text.\nwrite(\"Enter Your Name -\"\nname = io.read()\nThis would have the user type their name directly after the - in the write statement.",
  65.                  "4.2 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  66.                  "SIM"               
  67.                  },
  68.                  [5] = {
  69.                  "Key Points in this Chapter:\n1. What is an IF statement\n2. The ELSE statement\n3. The ELSEIF statement\n4. Complex IF's",
  70.                  "5.1 - What is an IF statement.\n\nWe use IF statements to control the programs direction based on certain criteria that you define. Doing this allows us to do only certain things based on certain conditions.",
  71.                  "5.1 - What is an IF statement.\n\nif name == \"Bob\" then\nprint (\"Hello Again Bob\")\nend\n\nThe 1st line says, if the variable name is equal to \"Bob\" then enter this IF statement. The next line is the code that will run if name does equal Bob. The 3rd line says to end the IF statement, if the name was not Bob the program would skip to the line directly after end.",
  72.                  "5.1 - What is an IF statement.\n\nWe have many options in the IF statement, we could do:\nif x >= 1\nRemember we can't do that IF statement if x is a string x = \"1\".\nif name ~= \"Bob\"\nThe ~= option means is not equal too. This if statement would pass if the name was NOT Bob.",
  73.                  "5.2 - The ELSE statement.\n\nSometimes we want to do 1 thing if our IF statement is true and something else if it's false.\nif name == \"Bob\" then\nprint (\"Hello Bob\")\nelse\nprint(\"Your not Bob\")\nend\n\n",
  74.                  "5.2 - The ELSE statement.\n\nNotice how their is only 1 end statement as the last line of the entire IF statement. The ELSE line is a part of the current IF statement.",
  75.                  "5.3 - The ELSEIF statement.\n\nSometimes we want to check for multiple outcomes inside an IF statement, we can achieve this using the ELSEIF statement. The key things to remember is that you still only need 1 end as the last line, because this is 1 full statement, and that you will need to include a then for an ELSEIF.",
  76.                  "5.3 - The ELSEIF statement.\n\nif name == \"Bob\" then\nprint (\"Hello Bob\")\nelseif name == \"John\" then\nprint (\"Hello John\")\nelse\nprint (\"Hello Other\")\nend",
  77.                  "5.3 - The ELSEIF statement.\n\nI still included the final else, which tells the program if the name wasn't Bob or John, then do something else. Notice again that there was only a single end as the last line, because this was 1 full statement.",
  78.                  "5.4 - Complex IF's.\n\nIf's can become very complex depending on your situation. I will show some examples of more complex IF's:\nif name == \"Bob\" and name ~= \"John\" then\nWe are checking the variable twice in 1 if statement by using the AND statement. We could also use the OR statement",
  79.                  "5.4 - Complex IF's.\n\nYou can also place IF statements inside other IF statements, just make sure you place an END statement at the correct place for each IF statement. Next page is an example of a pretty complex IF statement.",
  80.                  "5.4 - Complex IF's.\nif name == \"Bob\" then\n     if x == 1 then\n     print(x)\n     else\n     print(\"Not 1\")\n     end\nprint (\"Hi Bob\")\nelse\nprint (\"Your not Bob\")\nend",
  81.                  "5.4 - Complex IF's.\nWith precise placement of IF statements you can control the flow of your program to a great degree, this allows you to make sure the user is only seeing what you want them to see at all times.",
  82.                  "5.5 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  83.                  "SIM"                               
  84.                  },
  85.                  [6] = {
  86.                  "Key Points in this Chapter:\n1.What is a loop\n2.How to exit a loop\n3.Different kinds of loops",
  87.                  "6.1 - What is a loop\n\nA loop is a section of code that will continually run until told otherwise. We use these to repeat the same code until we say otherwise.\n\nwhile true do\nend\n\nThis is a while loop that has no exit, it will continually run over and over again until the program crashes.",
  88.                  "6.2 - How to exit a loop\nSince we don't want our loops to run until they crash we have to give them a way to stop. We do this by using the BREAK command.The break command is mostly placed inside an IF statement as just placing it in the loop itself would break the loop right away.",
  89.                  "6.2 - How to exit a loop\n\nx = 0\nwhile true do\n     x = x + 1\n     if x == 10 then\n     break\n     end\nend\n\nNotice how are while statements have their own end? This would run the loop and continually add 1 to x until x == 10 then it will break out of the loop.",
  90.                  "6.3 - Different kinds of loops\n\nYou don't always need to use the BREAK command. You can also include a condition in the WHILE statement for how long to run the loop.\n\nx = 0\nwhile x < 10 do\nx = x + 1\nend\n\nWe could also end this early with a BREAK as well.",
  91.                  "6.3 - Different kinds of loops\n\nHeck we don't even have to use the WHILE statement to create a loop.\n\nfor x = 0, 10, 1 do\nprint (x)\nend\n\nThe first line says - x starts at 0, and continues until 10, increasing by 1 every time we come back to this line.",
  92.                  "6.3 - Different kinds of loops\n\nSo using the for statement we could do.\n\nx = 5\nfor x, 50, 5 do\nprint (x)\nend\n\nThis would printout 5 then 10 then 15 ect ect until reaching 50.",
  93.                  "6.4 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  94.                  "SIM"                               
  95.                  },
  96.                  [7] = {
  97.                  "Key Points in this Chapter:\n1. Turning on and off redstone\n2. Checking and Setting Redstone",
  98.                  "7.1 - Turning on and off redstone\n\nOne of the greatest features of CC is that your computer can not only receive redstone signals, but it can also send them as well. We have 6 directions to choose from and they are:\ntop, bottom, front, back, left, right",
  99.                  "7.1 - Turning on and off redstone\n\nWe can control redstone with our computer using 2 basic commands, redstone.getInput(side) and redstone.setOutput(side, boolean).\nWe have to remember to place our sides in quotes though IE \"left\"",
  100.                  "7.2 - Checking and Setting Redstone\n\nredstone.setOutput(\"back\", true)\nThis tells the computer to turn on the redstone output on the back of the computer. We can replace true with false to turn off the redstone output to the back.",
  101.                  "7.2 - Checking and Setting Redstone\n\nif redstone.getInput(\"back\") == true then\nprint \"Redstone is on\"\nend\n\nThis would enter the IF statement if their was power running to the back of the computer.",
  102.                  "7.2 - Checking and Setting Redstone\n\nBy checking and setting different redstone sources while using IF statements we can control multiple things connected to our computer based on the redstone connections.",
  103.                  "7.3 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  104.                  "SIM"                                               
  105.                  },
  106.                  [8] = {
  107.                  "Key Points in this Chapter:\n1. How to turn on a single color\n2. How to turn off a single color\n3. Using multiple colors\n4. Testing Inputs\n5. Turning off all colors.",
  108.                  "8.1 - How to turn on a single color\n\nrs.setBundledOutput(\"back\", colors.white)\n\nThis would turn on the white output in the back.",
  109.                  "8.2 - How to turn off a single color\n\nrs.setBundledOutput(\"back\", rs.getBundledOutput(\"back\") - colors.white)\n\n This would turn off only the color white in the back.",
  110.                  "8.3 - Using Multiple colors\nUsing multiple colors is much easier when you use the colors.combine colors.subtract functions.",
  111.                  "8.3 - Using Multiple colors\n\nout = colors.combine(colors.blue, colors.white)\nrs.setBundledOutput(\"back\", out)\n\nThis would turn on blue and white at the back\nout = colors.subtract(out, colors.blue)\nrs.setBundledOutput(\"back\", out)\nThis would turn off blue, but leave white on.",
  112.                  "8.4 - Testing Inputs\n\nin = rs.getBundledInput(\"back\")\nif colors.test(in, colors.white) == true then\n\nThis would get the current input and store it in the in variable. We then use colors.test on the in variable to see if white is on.",
  113.                  "8.5 - Turning off all colors\n\nrs.setBundledOutput(\"back\", 0)\n\nSetting the output to 0 is the quickest and most efficient way to turn off all colors at the same time.",
  114.                  "8.6 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  115.                  "SIM"                                                               
  116.                  },
  117.                  [9] = {
  118.                  "Key Points in this Chapter:\n1. What is an event?\n2. How do we check for events\n3. What types of events are there?\n4. Using event loops\n5. WHATS GOING ON!",
  119.                  "9.1 - What is an event?\n\nAn event can be many things, from redstone to timers, as well as smashing your face on the keyboard. These can all trigger events within a program, and by correctly using the os.pullEvent() command we can make sure that no matter what happens, we'll know about it!",
  120.                  "9.2 - How do we check for events\n\nevent, param1, param2 = os.pullEvent()\n\nPlacing this line in your code will stop the program until ANY event triggers. So just by pressing a key, you will pass this statement because a keypress is an event",
  121.                  "9.2 - How do we check for events\n\nThe easiest way to check for an event happening is the IF statement.\n\nif event == \"char\" and param1 == \"q\" then\n This line would trigger if you pressed q on your keyboard.",
  122.                  "9.3 - What types of events are there\n\nchar - triggers on keypress\nkey - triggers on keypress\ntimer - triggers on a timer running out\nalarm - triggers on an alarm going off\nredstone - triggers on any change to redstone",
  123.                  "9.3 - What types of events are there\n\ndisk - triggers on disk insertion\ndisk_eject - triggers on disk ejection",
  124.                  "9.4 - Using event loops\n\nUsing the pullEvent() function inside a loop is a great way to determine when to break the loop. For instance:\n\nwhile true do\nevent, param1, param2 = os.pullEvent()\n     if event == \"char\" and param1 == \"q\" then\n     break\n     end\nend",
  125.                  "9.5 - WHATS GOING ON!\nThis is a cool test program to make, so that you can see exactly whats happening in events.\n\nwhile true do\nevent, param1, param2, param3 = os.pullEvent()\nprint (\"Event = \"..event)\nprint (\"P1 = \"..param1)\nprint (\"P2 = \"..param2)\nprint (\"P3 = \"..param3)\nend\n\nDon't Forget to Hold control+t to exit the loop.",
  126.                  "9.6 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  127.                  "SIM"                                                                               
  128.                  },
  129.                  [10] = {
  130.                  "Key Points in this Chapter:\n1. What is a function?\n2. How to use a basic function\n3. How to get a return value from a function",
  131.                  "10.1 - What is a function\n\nThink of a function as a part of your code that can be ran from anywhere inside your code. Once your function is done running, it will take you back to where your code left off.",
  132.                  "10.2 - How to use a basic function\n\nfunction hello()\nprint (\"Hello\")\nend\n\n Here we created a function named hello, and inside that function we placed a print statement that says hello.",
  133.                  "10.2 - How to use a basic function\n\nNow that we have our function created, anytime and anywhere in our program, we can place\nhello()\nThis will jump to the function, run the functions code, then come back to where you called the function.",
  134.                  "10.3 - How to get a return value from a function\n\nMost of the time we want our functions to do repetitious tasks for us though, to do this we can create a more advanced function that will return a value based on what happens in the function.",
  135.                  "10.3 - How to get a return value from a function\n\nfunction add(num1, num2)\nresult = tonumber(num1) + tonumber(num2)\nreturn result\nend\n\nThis is our adding function, it takes 2 numbers that we supply and returns the result.",
  136.                  "10.3 - How to get a return value from a function\n\nTo call our adding function we use.\nx = add(5,5)\nThis makes the add function use the numbers 5 and 5, which it will then add together and return to us. We save that return data in x",
  137.                  "10.3 - How to get a return value from a function\n\nBy combining what we already know, we must assume that we can use variables instead of numbers in our function. Therefor making:\nx = add(x, y)\nA perfectly good way to add 2 variables together.",
  138.                  "10.4 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  139.                  "SIM"                                                                                               
  140.                  },
  141.                  [11] = {
  142.                  "This is not a Chapter, this is just random blurbs of extra information about other features and functions.",
  143.                  "Blurb 0 (The Most Important) - Use NOTEPAD++ to edit your code, the in-game edit kinda sucks\n\nFind your code in saves/world/computer/#",
  144.                  "Blurb 1 - sleep(1) will pause your code for 1 second",
  145.                  "Blurb 2 - timername = os.startTimer(1) will cause a timer to go off in 1 second, use events to check for the name",
  146.                  "Blurb 3 - Making your code readable helps everyone\nwhile true do\nif x == 5 then\nend\nend\nCOULD BE\nwhile true do\n     if x == 5 then\n     end\nend",
  147.                  "Blurb 4 - Atleast 75% of the time, an error tells you exactly whats wrong with your program.\nSomething with NIL means your trying to use a NIL variable.",
  148.                  "Blurb 5 - Google Is your friend, just try \"lua strings tutorial\"",
  149.                  "Blurb 6 - Theres DOZENS of functions I didn't go over, you should read about them on the interwebs\nstring.len()\nstring.sub()\nstring.find()\nio.open()\nio.close()",
  150.                  "Blurb 7 - No one will help you if you don't work on the code yourself and provide them with it.",
  151.                  "Blurb 8 - When your ready for more advanced coding, start looking into the default programs as well as other user created content.",
  152.                  "Blurb 9 - Using matrice's is a good test to see if your grasping lua",
  153.                  "Blurb 10 - You don't have to use functions if you trully don't understand them, they can make your life easier, but they can also make your life much harder.",
  154.                  "Blurb 11 - Find help on IRC, but prepare to have code ready to be shown. http://webchat.esper.net/?channels=#computercraft",
  155.                  "Blurb 12 - You can do almost anything your imagination can think up.....except magic",
  156.                  "Blurb 13 - By holding Control+t you can terminate any program. By holding Control+r you can reboot any computer.",
  157.                  "END"
  158.                  }
  159. }
  160. --"Event Checker""String Functions","File Functions","Math Functions","Calling Another Program","Disk Functions",
  161. Examples = {
  162. [1] = {
  163. "Event Checker",
  164. "This example is a great way to check what event is being passed through the pullEvent() statement. It uses a while loop that continually checks the pull event until the q key is pressed.",
  165. [[
  166. term.clear()
  167. term.setCursorPos(1.1)
  168. while true do
  169. event, param1, param2 = os.pullEvent()
  170. print ("Event "..event)
  171. print ("P1 "..param1)
  172. print ("P2 "..param2)
  173. if event == "char" and param1 == "q" then break end
  174. end
  175. ]],
  176. "eventchecker"
  177. },
  178. [2] = {
  179. "String Functions",
  180. "This example uses some basic string functions to modify a string, we will take a long string and shorten it to 10 characters which will all be lowercased.",
  181. [[
  182. text = "Hello user and Welcome to the World of ComputerCraft"
  183.  
  184. blah = string.sub(text, 1, 10)
  185.  
  186. -- This line says that blah is now equal to 1-10 of text.
  187.  
  188. blah = string.lower(blah)
  189.  
  190. -- This line says that blah is now equal to an all lowercase blah.
  191.  
  192. print (blah)
  193. -- This outputs as  hello user
  194. ]],
  195. "stringfunc"
  196. },
  197. [3] = {
  198. "File Functions",
  199. "This example will check to see if the file exists we will open it in \"r\" mode to read, it will then read line by line from the file and store that data into an array, we will then print each line of the file with a 1 second sleep between them. We can use the file:write(variable) statement if the file is opened in write mode \"w\"",
  200. [[
  201. filename = "tutorial"
  202. if fs.exists(filename) == true then
  203. file = io.open(filename, "r")
  204. local i = 1
  205. local line = {}
  206. while true do
  207. line[i] = file:read()
  208. if line[i] == nil then break end
  209. print (line[i])
  210. sleep (1)
  211. i = i + 1
  212. end
  213. else
  214. print ("File doesn't exist.")
  215. end
  216. ]],
  217. "filefunc"
  218. },
  219. [4] = {
  220. "Math Functions",
  221. "This tutorial will go over some of the math functions, we'll start with a random number from 1-10, we will then divide that by another random number from 1-20. We will take the result and round upwards to a whole number. Meaning that if our answer is 3.1 it will still round up to 4. You'll notice the math.ciel function which does our rounding, we could also use math.floor which would round down instead.",
  222. [[
  223. num1 = math.random(1,10)
  224. num2 = math.random(1,20)
  225. print ("Number 1 is "..num1)
  226. print ("Number 2 is "..num2)
  227. result = num1 / num2
  228. print ("UnRounded Result is "..result)
  229. result = math.ceil(result)
  230. print ("Rounded Result is "..result)
  231. ]],
  232. "mathfunc"
  233. },
  234. [5] = {
  235. "Calling Another Program",
  236. "This tutorial is very basic, but is a very powerful function as well. The shell.run command allows us to run a command from within our program as if we were at the terminal.",
  237. [[
  238. shell.run("programname")
  239. shell.run("mkdir", "testing")
  240. This would create a testing directory
  241. shell.run("copy", "disk/hello", "world")
  242. This would copy the program hello from the disk directory
  243. and place it as a program called world in your root
  244. directory.
  245. ]],
  246. "callprogram"
  247. },
  248. [6] = {
  249. "Disk Functions",
  250. "This tutorial will go over some of the basic floppy functions. We will check to see if a floppy is inserted using the pullEvent() loop, we will then make sure the floppy doesn't have a label, and if it's label is empty we will label it Blank and exit the program. We can learn more about the disk functions by typing help disk at the terminal.",
  251. [[
  252. while true do
  253. event, param1 = os.pullEvent()
  254. if event == "disk" then
  255.     if disk.getLabel(param1) == nil then
  256.     disk.setLabel(param1, "Blank")
  257.     print ("Disk labeled to Blank")
  258.     print ("Disk is in the "..param1.." side")
  259.     break
  260.     else
  261.     print ("Disk already has a label.")
  262.     print ("Disk is in the "..param1.." side")
  263.     break
  264.     end
  265. end
  266. end
  267. ]],
  268. "diskfunc"
  269. }
  270.  
  271. function SaveExamples()
  272. term.clear()
  273. term.setCursorPos(1,1)
  274. print "This will save all of the example programs into the examples folder."
  275. print ""
  276. print "You can access this folder by typing"
  277. print "cd examples"
  278. print "You can list the examples by typing"
  279. print "ls"
  280. print "You can simply edit the file to look"
  281. print "at it, or you can open the file inside"
  282. print "your computer by finding it in your"
  283. print "saves directory under your worldname"
  284. print "and computer #."
  285. print ""
  286. print ("Your Computer # is "..os.getComputerID())
  287. pressany()
  288. sleep(.5)
  289. shell.run("mkdir", "examples")
  290. local i = 1
  291.     while true do
  292.         if Examples[i] ~= nil then
  293.         file = io.open("examples/"..Examples[i][4], "w")
  294.         file:write("--[[\n")
  295.         file:write(Examples[i][2])
  296.         file:write("--]]\n")
  297.         file:write(Examples[i][3])
  298.         file:close()
  299.         i = i + 1
  300.         else
  301.         break
  302.         end
  303.     end
  304. term.clear()
  305. term.setCursorPos(1,1)
  306. print "Examples correctly saved to /examples"
  307. pressany()
  308. end
  309.  
  310. function mainmenu()
  311. while true do
  312. term.clear()
  313. term.setCursorPos(1,1)
  314. print "--------------------------------------"
  315. print "| ComputerCraft Interactive Tutorial |"
  316. print "|            By: Casper7526          |"
  317. print "--------------------------------------"
  318. print "|                                    |"
  319. print "| 1. Start                           |"
  320. print "| 2. Choose Chapter                  |"
  321. print "| 3. Examples                        |"
  322. print "| 4. Save Examples To File           |"
  323. print "| 5. Exit                            |"
  324. print "|                                    |"
  325. print "--------------------------------------"
  326. event, param1, param2, param3 = os.pullEvent()
  327. if event == "char" and param1 == "5" then break end
  328. if event == "char" and param1 == "1" then chapter = 1 LoadChapter(chapter) end
  329. if event == "char" and param1 == "2" then ChooseChapter() end
  330. if event == "char" and param1 == "3" then ChooseExample() end
  331. if event == "char" and param1 == "4" then SaveExamples() end
  332. end
  333. end
  334.  
  335. function LoadExample(num)
  336. term.clear()
  337. term.setCursorPos(1,1)
  338. print (Examples[num][2])
  339. pressany()
  340. term.clear()
  341. sleep(.5)
  342. term.setCursorPos(1,1)
  343. print (Examples[num][3])
  344. pressany()
  345. end
  346.  
  347. function ChooseExample()
  348. while true do
  349. term.clear()
  350. term.setCursorPos(1,1)
  351. print "--------------- Example Index ---------------"
  352. print "---------------------------------------------"
  353. print ""
  354. local i = 1
  355.     while true do
  356.     if Examples[i] == nil then break end
  357.     print (i..". "..Examples[i][1])
  358.     i = i + 1
  359.     end
  360. print ""
  361. print "q. Quit"
  362. print "---------------------------------------------"
  363. write "Choice - "
  364. choice = io.read()
  365. if string.lower(choice) == "q" then break end
  366. if Examples[tonumber(choice)] == nil then print "Thats not a valid chapter." sleep(1) else
  367. LoadExample(tonumber(choice)) break end
  368. end
  369. end
  370.  
  371.  
  372.  
  373. function ChooseChapter()
  374. while true do
  375. term.clear()
  376. term.setCursorPos(1,1)
  377. print "--------------- Chapter Index ---------------"
  378. print "---------------------------------------------"
  379. print ""
  380. local i = 1
  381.     while true do
  382.     if ChapterTitles[i] == nil then break end
  383.     print (i..". "..ChapterTitles[i])
  384.     i = i + 1
  385.     end
  386. print ""
  387. print "q. Quit"
  388. print "---------------------------------------------"
  389. write "Choice - "
  390. choice = io.read()
  391. if string.lower(choice) == "q" then break end
  392. if ChapterTitles[tonumber(choice)] == nil then print "Thats not a valid chapter." sleep(1) else
  393. LoadChapter(tonumber(choice)) break end
  394. end
  395. end
  396.  
  397. function LoadChapter(chapter)
  398. while true do
  399. term.clear()
  400. term.setCursorPos(1,1)
  401. print ("Chapter "..chapter.." - "..ChapterTitles[chapter])
  402. print ("---------------------------------------------")
  403. print (Chapter[chapter][CurrentSection])
  404. print ""
  405. if Chapter[chapter][CurrentSection + 1] == "END" then print "THATS ALL FOLKS!" else
  406. print "Press [Space] To Continue"
  407. end
  408. print "[q] - Main Menu [b] - Previous Page."
  409. if Chapter[chapter][CurrentSection + 1] == "SIM" then print "Press [Enter] To Run Simulation" end
  410. event, param1, param2, param3 = os.pullEvent()
  411.     if event == "key" and param1 == 28 and Chapter[chapter][CurrentSection + 1] == "SIM" then Sim(chapter) EndSim(chapter) chapter = chapter + 1 CurrentSection = 1 end
  412.     if event == "char" and param1 == "q" then CurrentSection = 1 break end
  413.     if event == "char" and param1 == "b" then
  414.     CurrentSection = CurrentSection - 1
  415.     if CurrentSection == 0 then CurrentSection = 1 end
  416.     end
  417.     if event == "char" and param1 == " " and Chapter[chapter][CurrentSection + 1] ~= "END" then
  418.     if Chapter[chapter][CurrentSection + 1] == "SIM" then chapter = chapter + 1 CurrentSection = 1 else CurrentSection = CurrentSection + 1 end
  419.     end
  420. end
  421. end
  422.  
  423. function EndSim(chapter)
  424. while true do
  425. term.clear()
  426. term.setCursorPos(1,1)
  427. print "Great work back there!"
  428. print ""
  429. print "Press [ENTER] to move on to the next chapter"
  430. event, param1, param2 = os.pullEvent()
  431. if event == "key" and param1 == 28 then shell.run("rm", "tmptut") break end
  432. end
  433. end
  434.  
  435. function pressany()
  436. term.setCursorPos(1,17)
  437. print "Press Any Key To Continue"
  438. event = os.pullEvent()
  439. end
  440.  
  441. function Sim(chapter)
  442. stage = 1
  443. while true do
  444. term.clear()
  445. term.setCursorPos(1,1)
  446.     if chapter == 1 then
  447.     print "Your Goals:"
  448.     print ""
  449.     print "* Create a program named hello."
  450.     print "* Type anything you wish inside that program."
  451.     print "* Save and Exit the program."
  452.     print "* Run the program."
  453.     print ""
  454.     print "quit   will exit the sim early."
  455.     write (">") input = io.read()
  456.     if input == "quit" then break end
  457.         --------------------------------
  458.         if stage == 1 then
  459.             if input == "edit hello" then
  460.             shell.run("edit", "tmptut")
  461.             print "Great Job, now let's run our program!"
  462.             sleep(2)
  463.             stage = 2
  464.             else
  465.             print "Remember, lua is case sensitive."
  466.             print "Try"
  467.             print "edit hello"
  468.             sleep(2)           
  469.             end
  470.         elseif stage == 2 then
  471.             if input == "hello" then break
  472.             else
  473.             print "Remember, lua is case sensitive."
  474.             print "Try"
  475.             print "hello"
  476.             sleep(2)           
  477.             end
  478.         end
  479.     end
  480.  
  481.     if chapter == 2 then
  482.     print "Your Goals:"
  483.     print ""
  484.     print "* Create a program named hello."
  485.     print "* Clear the Screen"
  486.     print "* Set the Cursor Pos to 1,1"
  487.     print "* Print \"Hello Loser\" on line 1 of the screen."
  488.     print "* Print \"Welcome\" on line 2 of the screen."
  489.     print "* Clear the 1st line."
  490.     print "* Print \"Hello User\" on line 1 of the screen."
  491.     print "* Run your program!"
  492.     print ""
  493.     print "You can type \"example\" at anytime to see the correct syntax."
  494.     print "quit   will exit the sim early."
  495.     print ""
  496.     write (">") input = io.read()
  497.         if input == "quit" then break end
  498.         if input == "edit hello" then shell.run("edit", "tmptut") end
  499.         if input == "hello" then shell.run("tmptut") pressany()
  500.         term.clear()
  501.         term.setCursorPos(1,1)
  502.         print "Did you program work as you expected?"
  503.         print ""
  504.         print "Press [ENTER] to end the simulation."
  505.         print "Press Any Other Key to go back and work on your program."
  506.         event, param1, param2 = os.pullEvent()
  507.         if event == "key" and param1 == 28 then break end
  508.         end
  509.         if string.lower(input) == "example" then
  510.         term.clear()
  511.         term.setCursorPos(1,1)
  512.         print ("term.clear()")
  513.         print ("term.setCursorPos(1,1)")
  514.         print ("print (\"Hello Loser\"")
  515.         print ("print (\"Welcome\"")
  516.         print ("term.setCursorPos(1,1)")
  517.         print ("term.clearLine()")
  518.         print ("print (\"Hello User\")")
  519.         pressany()
  520.         end
  521.     end
  522.  
  523.     if chapter == 3 then
  524.     print "Your Goals:"
  525.     print ""
  526.     print "--Use the program hello--"
  527.     print "* Create the following variables."
  528.     print "  x = 1"
  529.     print "  y = \"2\""
  530.     print "  z = 0"
  531.     print "  text = \"Output \""
  532.     print "* Add x and y together and store that value in z, then print text and z to the user on the same line."
  533.     print "* Run your program!"
  534.     print ""
  535.     print "You can type \"example\" at anytime to see the correct syntax."
  536.     print "quit   will exit the sim early."
  537.     print ""
  538.     write (">") input = io.read()
  539.         if input == "quit" then break end
  540.         if input == "edit hello" then shell.run("edit", "tmptut") end
  541.         if input == "hello" then shell.run("tmptut") pressany()
  542.         term.clear()
  543.         term.setCursorPos(1,1)
  544.         print "Did you program work as you expected?"
  545.         print ""
  546.         print "Press [ENTER] to end the simulation."
  547.         print "Press Any Other Key to go back and work on your program."
  548.         event, param1, param2 = os.pullEvent()
  549.         if event == "key" and param1 == 28 then break end
  550.         end
  551.         if string.lower(input) == "example" then
  552.         term.clear()
  553.         term.setCursorPos(1,1)
  554.         print ("term.clear()")
  555.         print ("term.setCursorPos(1,1)")
  556.         print ("x = 1")
  557.         print ("y = \"2\"")
  558.         print ("z = 0")
  559.         print ("text = \"Output \"")
  560.         print ("y = tonumber(y)")
  561.         print ("z = x + y")
  562.         print ("print (text..z)")
  563.         pressany()
  564.         end
  565.     end
  566.    
  567.     if chapter == 4 then
  568.     print "Your Goals:"
  569.     print ""
  570.     print "--Use the program hello--"
  571.     print "* Ask the user for their name"
  572.     print "* Show them the line:"
  573.     print "  Hello name how are you today?"
  574.     print "  With name replaced by their input."
  575.     print "* Run your program!"
  576.     print ""
  577.     print "You can type \"example\" at anytime to see the correct syntax."
  578.     print "quit   will exit the sim early."
  579.     print ""
  580.     write (">") input = io.read()
  581.         if input == "quit" then break end
  582.         if input == "edit hello" then shell.run("edit", "tmptut") end
  583.         if input == "hello" then shell.run("tmptut") pressany()
  584.         term.clear()
  585.         term.setCursorPos(1,1)
  586.         print "Did you program work as you expected?"
  587.         print ""
  588.         print "Press [ENTER] to end the simulation."
  589.         print "Press Any Other Key to go back and work on your program."
  590.         event, param1, param2 = os.pullEvent()
  591.         if event == "key" and param1 == 28 then break end
  592.         end
  593.         if string.lower(input) == "example" then
  594.         term.clear()
  595.         term.setCursorPos(1,1)
  596.         print ("term.clear()")
  597.         print ("term.setCursorPos(1,1)")
  598.         print ("write(\"Whats your name? \")")
  599.         print ("name = io.read()")
  600.         print ("print (\"Hello \"..name..\" how are you today?\")")
  601.         pressany()
  602.         end
  603.     end
  604.    
  605.    
  606.     if chapter == 5 then
  607.     print "Your Goals:"
  608.     print ""
  609.     print "--Use the program hello--"
  610.     print "* Ask the user for their name"
  611.     print "* If their name is Bob or John then welcome them."
  612.     print "* If their name isn't Bob or John, then tell them to get lost!"
  613.     print "* Run your program!"
  614.     print ""
  615.     print "You can type \"example\" at anytime to see the correct syntax."
  616.     print "quit   will exit the sim early."
  617.     print ""
  618.     write (">") input = io.read()
  619.         if input == "quit" then break end
  620.         if input == "edit hello" then shell.run("edit", "tmptut") end
  621.         if input == "hello" then shell.run("tmptut") pressany()
  622.         term.clear()
  623.         term.setCursorPos(1,1)
  624.         print "Did you program work as you expected?"
  625.         print ""
  626.         print "Press [ENTER] to end the simulation."
  627.         print "Press Any Other Key to go back and work on your program."
  628.         event, param1, param2 = os.pullEvent()
  629.         if event == "key" and param1 == 28 then break end
  630.         end
  631.         if string.lower(input) == "example" then
  632.         term.clear()
  633.         term.setCursorPos(1,1)
  634.         print ("term.clear()")
  635.         print ("term.setCursorPos(1,1)")
  636.         print ("write(\"Whats your name? \")")
  637.         print ("name = io.read()")
  638.         print ("if name == \"Bob\" or name == \"John\" then ")
  639.         print ("print (\"Welcome \"..name)")
  640.         print ("else")
  641.         print ("print (\"Get lost!\")")
  642.         print ("end")
  643.         pressany()
  644.         end
  645.     end
  646.    
  647.    
  648.     if chapter == 6 then
  649.     print "Your Goals:"
  650.     print ""
  651.     print "--Use the program hello--"
  652.     print "* Create a loop that continually asks the user for their name."
  653.     print "* Only exit that loop if they enter Bob as their name."
  654.     print "* Try using the BREAK statement as well as without."
  655.     print "* Run your program!"
  656.     print ""
  657.     print "You can type \"example\" at anytime to see the correct syntax."
  658.     print "quit   will exit the sim early."
  659.     print ""
  660.     write (">") input = io.read()
  661.         if input == "quit" then break end
  662.         if input == "edit hello" then shell.run("edit", "tmptut") end
  663.         if input == "hello" then shell.run("tmptut") pressany()
  664.         term.clear()
  665.         term.setCursorPos(1,1)
  666.         print "Did you program work as you expected?"
  667.         print ""
  668.         print "Press [ENTER] to end the simulation."
  669.         print "Press Any Other Key to go back and work on your program."
  670.         event, param1, param2 = os.pullEvent()
  671.         if event == "key" and param1 == 28 then break end
  672.         end
  673.         if string.lower(input) == "example" then
  674.         term.clear()
  675.         term.setCursorPos(1,1)
  676.         print ("term.clear()")
  677.         print ("term.setCursorPos(1,1)")
  678.         print ""
  679.         print ("while name ~= \"Bob\" do")
  680.         print ("write(\"Whats your name? \")")
  681.         print ("name = io.read()")
  682.         print ("end")
  683.         print ""
  684.         print ("while true do")
  685.         print ("write(\"Whats your name? \")")
  686.         print ("name = io.read()")
  687.         print ("    if name == \"Bob\" then")
  688.         print ("    break")
  689.         print ("    end")
  690.         print ("end")
  691.         pressany()
  692.         end
  693.     end
  694.    
  695.    
  696.     if chapter == 7 then
  697.     print "Your Goals:"
  698.     print ""
  699.     print "--Use the program hello--"
  700.     print "* Check to see if there is redstone current coming into the back of your computer"
  701.     print "* If there is current coming in the back then turn on the current to the front"
  702.     print "* If there isn't current coming in the back, then turn off the current to the front"
  703.     print "* Tell the user if you turned the current on or off."
  704.     print "* Run your program!"
  705.     print ""
  706.     print "You can type \"example\" at anytime to see the correct syntax."
  707.     print "quit   will exit the sim early."
  708.     print ""
  709.     write (">") input = io.read()
  710.         if input == "quit" then break end
  711.         if input == "edit hello" then shell.run("edit", "tmptut") end
  712.         if input == "hello" then shell.run("tmptut") pressany()
  713.         term.clear()
  714.         term.setCursorPos(1,1)
  715.         print "Did you program work as you expected?"
  716.         print ""
  717.         print "Press [ENTER] to end the simulation."
  718.         print "Press Any Other Key to go back and work on your program."
  719.         event, param1, param2 = os.pullEvent()
  720.         if event == "key" and param1 == 28 then break end
  721.         end
  722.         if string.lower(input) == "example" then
  723.         term.clear()
  724.         term.setCursorPos(1,1)
  725.         print ("term.clear()")
  726.         print ("term.setCursorPos(1,1)")
  727.         print ("if redstone.getInput(\"back\") == true then")
  728.         print ("redstone.setOutput(\"front\", true)")
  729.         print ("print (\"Front is now on.\")")
  730.         print ("else")
  731.         print ("redstone.setOutput(\"front\", false)")
  732.         print ("print (\"Front is now off.\")")
  733.         print ("end")
  734.         pressany()
  735.         end
  736.     end
  737.    
  738.     if chapter == 8 then
  739.     print "Your Goals:"
  740.     print ""
  741.     print "--Use the program hello--"
  742.     print "--Use the back output of the computer--"
  743.     print "* Turn on white"
  744.     print "* Turn on blue"
  745.     print "* Turn on purple"
  746.     print "* Turn off blue"
  747.     print "* Turn off all colors"
  748.     print "* Check to see if white is coming in the front"
  749.     print "* Run your program!"
  750.     print ""
  751.     print "You can type \"example\" at anytime to see the correct syntax."
  752.     print "quit   will exit the sim early."
  753.     print ""
  754.     write (">") input = io.read()
  755.         if input == "quit" then break end
  756.         if input == "edit hello" then shell.run("edit", "tmptut") end
  757.         if input == "hello" then shell.run("tmptut") pressany()
  758.         term.clear()
  759.         term.setCursorPos(1,1)
  760.         print "Did you program work as you expected?"
  761.         print ""
  762.         print "Press [ENTER] to end the simulation."
  763.         print "Press Any Other Key to go back and work on your program."
  764.         event, param1, param2 = os.pullEvent()
  765.         if event == "key" and param1 == 28 then break end
  766.         end
  767.         if string.lower(input) == "example" then
  768.         term.clear()
  769.         term.setCursorPos(1,1)
  770.         print ("term.clear()")
  771.         print ("term.setCursorPos(1,1)")
  772.         print ("out = colors.combine(colors.white, colors.blue, colors.purple)")
  773.         print ("rs.setBundledOutput(\"back\", out)")
  774.         print ("out = colors.subtract(out, colors.blue)")
  775.         print ("rs.setBundledOutput(\"back\", out)")
  776.         print ("rs.setBundledOutput(\"back\", 0)")
  777.         print ("in = rs.getBundledInput(\"front\")")
  778.         print ("if colors.test(in, colors.white) == true then")
  779.         print ("print (\"White is on in front\")")
  780.         print ("else")
  781.         print ("print (\"White is off in front\")")
  782.         print ("end")
  783.         pressany()
  784.         end
  785.     end
  786.    
  787.     if chapter == 9 then
  788.     print "Your Goals:"
  789.     print ""
  790.     print "--Use the program hello--"
  791.     print "* Create an event loop"
  792.     print "* Print the char that was pressed"
  793.     print "* Stop the loop when the q key is pressed"
  794.     print "* Stop the loop if the redstone event happens"
  795.     print "* Run your program!"
  796.     print ""
  797.     print "You can type \"example\" at anytime to see the correct syntax."
  798.     print "quit   will exit the sim early."
  799.     print ""
  800.     write (">") input = io.read()
  801.         if input == "quit" then break end
  802.         if input == "edit hello" then shell.run("edit", "tmptut") end
  803.         if input == "hello" then shell.run("tmptut") pressany()
  804.         term.clear()
  805.         term.setCursorPos(1,1)
  806.         print "Did you program work as you expected?"
  807.         print ""
  808.         print "Press [ENTER] to end the simulation."
  809.         print "Press Any Other Key to go back and work on your program."
  810.         event, param1, param2 = os.pullEvent()
  811.         if event == "key" and param1 == 28 then break end
  812.         end
  813.         if string.lower(input) == "example" then
  814.         term.clear()
  815.         term.setCursorPos(1,1)
  816.         print ("term.clear()")
  817.         print ("term.setCursorPos(1,1)")
  818.         print ("while true do")
  819.         print ("event, param1, param2 = os.pullEvent()")
  820.         print ("     if event == \"redstone\" then")
  821.         print ("     break")
  822.         print ("     end")
  823.         print ("     if event == \"char\" and param1 == \"q\" then")
  824.         print ("     break")
  825.         print ("     else")
  826.         print ("     print (\"You pressed - \"..param1)")
  827.         print ("     end")
  828.         print ("end")
  829.         pressany()
  830.         end
  831.     end
  832.    
  833.     if chapter == 10 then
  834.     print "Your Goals:"
  835.     print ""
  836.     print "--Use the program hello--"
  837.     print "* Ask the user for their first name."
  838.     print "* Ask the user for their last name."
  839.     print "* Combine the 2 strings using a function"
  840.     print "  return the result into the fullname variable"
  841.     print "* Show the user their full name"
  842.     print "* Run your program!"
  843.     print ""
  844.     print "You can type \"example\" at anytime to see the correct syntax."
  845.     print "quit   will exit the sim early."
  846.     print ""
  847.     write (">") input = io.read()
  848.         if input == "quit" then break end
  849.         if input == "edit hello" then shell.run("edit", "tmptut") end
  850.         if input == "hello" then shell.run("tmptut") pressany()
  851.         term.clear()
  852.         term.setCursorPos(1,1)
  853.         print "Did you program work as you expected?"
  854.         print ""
  855.         print "Press [ENTER] to end the simulation."
  856.         print "Press Any Other Key to go back and work on your program."
  857.         event, param1, param2 = os.pullEvent()
  858.         if event == "key" and param1 == 28 then break end
  859.         end
  860.         if string.lower(input) == "example" then
  861.         term.clear()
  862.         term.setCursorPos(1,1)
  863.         print ("term.clear()")
  864.         print ("term.setCursorPos(1,1)")
  865.         print ("function combine(s1, s2)")
  866.         print ("result = s1..s2")
  867.         print ("return result")
  868.         print ("end")
  869.         print ("write(\"What's your first name? \")")
  870.         print ("firstname = io.read()")
  871.         print ("write(\"What's your last name? \")")
  872.         print ("lastname = io.read()")
  873.         print ("fullname = combine(firstname, lastname)")
  874.         print ("print (\"Hello \"..fullname)")
  875.         pressany()
  876.         end
  877.     end
  878.    
  879.    
  880. end
  881.    
  882. end
  883.  
  884.  
  885. mainmenu()
  886.  
  887. print "You don't need to thank me."
  888. print "Thank yourself for learning!"
  889. print "To learn more search online!"
  890. print "You can also type help index!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement