Jump to content

[Journal] Learning to Program from Scratch

9 replies to this topic

#1 alaphwolf

  • New Members
  • 7 posts

Posted 10 January 2013 - 02:34 PM

To show the methodology of a non-programmer learning to write LUA programs. The hope is that this journal will serve to help others, help myself, and perhaps entertain.
Updating may be slow as I have both a full time job and a full time family but the idea is that with the journal being so public it will push me to update more regularly.

Project 1 - My First Real Program

Goal -
When done I hope to have a program that will allow me to give a turtle variable height, length, and width measurements and it will clear the area defined by those inputs and return to its origin.

Phase 1 -
The goal for phase one will be to get the turtle to clear an area above itself as defined by the input of the user and then return to ground.

I will post any code that I write along with what I think is going on with the code before I test, what I expect to happen when I run it, what actually happens, and anything I learn or any question that I have.

So first code and test:

So it should print on the screen
How far up?
Then let me put in a number and the turtle should go up that amount.
I wonder what happens if turtle.detectUp() returns true? A simple halt? or will it do that thing where it waits for that to be true of for the terminal to be halted??
I'm not sure I fully understand Arg, does it have to be Arg? Could I substitute Aup? I'll try that later. I get the u = u + 1 loop thing but its still weird ( if I did while u = Arg would it only move up once? or would it fail out at the first loop? I'll have to try that.

time to run this code...be right back =D

Ok so I wrote the code in notepad++ and saved it as tup.lua so when I go into my turtle it shows the file as tup.lua, how can I just have it show the name with no extension? the other files in there are just listed as file. Ok saved as file type all files with no extension and it worked.... that's so odd =/

ran program tup2
bios:338: [string"tup2"] :14: 'end'
expected (to close 'while' at line 8)

So something is wrong on line 8?

added an end statement, ah ok I didn't end all three conditions this should fix it
Well ok then, ran the program and it printed
How far up?
Well that didn't work


Sometime later after some sleep.

So I'm using the example from the turtle.forward page off the wiki as a base... is Arg a lua thing or was it chosen for clarity? Changed all instances of to tUP and ran the code from the wiki, it worked so Arg isn't a lua thing its just an argument. The (...) must just say "look at what the user wrote and set that to Arg or tUP or whatever"

So here is the code so far
when you run it the turtle goes up...and up, and up, good thing there was a block above it....it might have just kept going =(

Ahha, I didn't encapsulate the iterater(?is that a real thing?) within the loop that had the a <= Arg statement, because of that a never incremented up and was always less than Arg creating an infinite loop, lets fix it and see what happens---- be right back.

It worked!!!!!

Well that's good but it's not exactly what I want, so tonight if I get any free time I'll look into doing it in a manner such that the user is prompted to enter the 'Up' amount and also try and get the turtle to go back down....and maybe if I'm real ambitious have it report back that it went up x and returned.
When I get home from work we'll try out any code changes and let you know what happens.

Todays lessons:
be more careful wrapping code blocks, you could have lost a turtle were it not for a ceiling =/
Arg (...) isn't lua for argument is the following, well it is but you don't have to type Arg, you can type what ever you want.
the tonumber , tostring function (argument?, command?) converts some input to number or string or whatever.

I will work on make this more visual (adding screenshots and maybe some video)


edit - also need to figure out how to do the spoiler thing with code

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 January 2013 - 02:37 PM

 alaphwolf, on 10 January 2013 - 02:34 PM, said:

edit - also need to figure out how to do the spoiler thing with code

#3 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 10 January 2013 - 02:40 PM

Looking forward to reading more updates! It's interesting to see your whole process with the original code, the errors, what you thought when you saw the errors, when you figured out the problem.
Great post :)

#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 10 January 2013 - 03:24 PM

Hmm... This is interesting, but at the same time I can see it being difficult to keep up, especially when you start getting into more complex programs. Maybe a video journal would be easier to maintain?

#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 10 January 2013 - 04:26 PM

Fun read!
Fun tip, when declaring a variable like Args, it's generally a good idea to make it local to that program. By that I mean declaring it like local Args = {...}.
That way, the variable does not go to the other programs you use, and have you accidentally mess up your code with information you only want for one specific program.

#6 alaphwolf

  • New Members
  • 7 posts

Posted 10 January 2013 - 09:21 PM

First off, thank you for the kind words. @Bubba, I see what you mean, for this project though I think I'll be alright, plus I barely have time to sit and fuss with code let alone record, edit, annotate, a video =D but it is something I will consider.

Secondly, I'm at work right now (I work nights as an x-ray tech) so I can't test any of this until I get home. So this will mainly be a theory night, that said I can do a few things using http://www.lua.org/demo.html

So our earlier code was ok but I really want our argument to be entered after the user is prompted as opposed to just after typing the path to the program.


So that is code from the wiki tut on loops, it looks like it will do exactly what I need it to do but I'm not sure I understand how it works in detail.

The first line is a write(), as I understand it this should print the text to screen and leave the cursor at the end of the statement, a print() call would return the cursor to the next line in the same manner as a write("some text /n"), does this effect data entry? does returning the line clear the entry?
in the next line we have a read(), this is the key I think. In fact if I'm right the previous line doesn't matter at all as the read() just looks at what was last typed (this is why I'm wondering about how print() and write("/n") work, if they return the line then read() should activate and receive either a nil value or the text from print() or write("/n"). Also if /n activates from within the "" then how could you get /n to print to screen? print("/n")? write("/n /n")? need to test that.

------- train of thought derailed due to patients will come back to this, in the mean time I can't figure out what putting [[ code ]] does??? any help, what do double brackets tell the compiler?

#7 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 11 January 2013 - 05:55 AM

Double brackets indicate a multiline input. So for example,
Would be equal to

Using print("text") is EXACTLY the same as using write("text\n") The print() function just appends the string with "\n" automatically.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 January 2013 - 10:51 AM

also using

print( [[
This is a massive
multiline text that
retains all formatting
including indentation
whitespace]] )

#9 alaphwolf

  • New Members
  • 7 posts

Posted 18 January 2013 - 07:55 PM

So the past week has been very busy for me and no updates as of yet, haven't even really had time to think about this.

That said I do have a thought on how I want this to execute and if Im thinking right how the loops will look. Basically I want the program to go up then back down, then move left 1 and go up again, repeat the up-left loop until the left threshold is met, return to the beginning of the line and then move forward and repeat. While this doesn't feel like the most efficient route I have a feeling it may be the easiest to code. I'm still a very long way off from this point though so who knows.
I hope to get some work done on the variable up code this weekend. I need to set some deadlines for each of the phases to help push me along. As long as the deadline is reasonable RL shouldn't interfere too much.

Anyhow, this is still ongoing and not abandoned =D but now you can see what I mean about infrequent updates.


#10 Kenny_13

  • New Members
  • 1 posts

Posted 03 February 2013 - 02:20 AM

Its kinda hard u should color the things u do not wanna put in computer

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users