Sunday, October 10, 2010

Programming Notes 1

I'm currently in Bachelor of Science: Computer Science, so I thought I'd share some notes :)

Flowcharts are the best way to go before programming anything. It's a collection of shapes/symbols that have specific roles for your code. The internet has buttloads of lists that you can find easily! It acts like a Pseudo Code which is equivalent to a draft version of your code. With it you can Simulate any future outcomes with your code and find potential bugs without having any problems later on.

Pseudo Code - draft code. For example:
if 50 is greater than X
then
give player lots of potions.
else 
tell the player to lose it.

Simulate also means Simulation. It means "reading your code" in a way. Kinda hard to explain here, maybe something like this:
====================================
do
Give player 5 potions
X is equal to 40
while
if player potions is equal to X
     the player will gain 500 gold
else
you don't have enough
(go back to do)
====================================
if we simulate this
Player has 5 potions
5 is not equal to 40
go back to beginning
Player has 10 potions now
10 is not equal to 40
start over
...
Player has 40 potions now
40 is equal to 40
end the process.
====================================
That's how simulation goes :) Try changing the player's potion amount and simulate it! (In fact try doing -2  with algebra you have in mind. haha)

In programming, there's what we call "Integer Math" which means whole numbers only, no decimals and "Double/Float" allows decimals in code. But I'll work on explaining the stuff I learned some time later :D

Friday, July 31, 2009

First Day

The Intro
Because I did a lot of heavy eventing before (such as making CMS, TBS and CBS and editing the DBS) I kinda got the gist of some stuff like variables, switches and conditional branches as well as loops. Apparently YF told me that this is going to be easier for me and so he taught me some basic things and I did my very first script XD

Since my mindset was, I want to event in VX but I want to make the annoying methods out such as setting variables for the stats of your every members in the database. So I got this sweet template from Yanfy. My start wasn't very wise but at least it got me excited to work on XD

So I did a simple script the involves just setting 10 variables to "whoever is in your party" stats. I told about it with TDS and he taught me a few things such as when to use aliasing and so forth. But it just stopped there because I was too busy.

The Basics:

Then a month(?) later, TDS somehow made me pick it up again after talking for a while. He taught me about methods. But first he went with the basic:

Variables, scary at first for many, but your best friend in the end.

variable = "Archeia"
That's it.


Wow~!
It's just a container right?


They hold information, whether it's a number, string or object. Different variables would be:


variable
@variable
VARIABLE

What does it do~?



variable is a normal variable used for common stuff
@variable is used for stuff you will use through a class
VARIABLE is a constant, which means it's value is constant and will never change. Like setting pre set values for menus or a game.

A variable can be named anything, unless it's name is a reserved word like the blue names you see in scripts. Variables are also case-sensitive. They have to be in lower cases. Higher cases are constants.

There's also the Global Variables, which means they can be accessed anypoint of the game and is stored in your computer's memory. They usually have $variable in them. It is highly discouraged to use it so often unless needed. A good example of bad use can be found on...Mog's script *cough*, title screen where we'll see like $screen x and $screen y when another way can be done.

With that said!



The Exercise

First were going to deal with variables through examples. Variables can hold any value pretty much Numbers letters etc. Were going to start with letters. Make a new project and a new script above Main and type the following:

name = "nessiah"

p name
exit

That variable I made is called name and it holds my name, then it prints it.
" " is a string or letter. In this case the string is "nessiah"


Now let's try something like this!



name = "nessiah"

p name.capitalize
exit

If you tried it out, what it did was, it printed it as Nessiah, you see . is a property.
Usually you can find them in help files. That's a string property, to capitalize.

Now try

name = "Nessiah"

p name.downcase!
exit

Question

What does it do?
Makes everything lower case?


Yep.

How would you apply it?
variable.downcase?


Yep.
Awesome!


Resume

. is very important, but you're not limited to just one.
Try this on your variable. p name.reverse!.capitalize


Suddenly I feel like I know what it does haha~



Because it's easy to read and you do understand it.
But wait you can also do math with letters:


name = " nessiah " * 3

p name
exit

* is the symbol for multiplication.



Wow...that's amazing~! It can do it with words.




So how did it appear?


Nessiah Nessiah Nessiah
You can also add to it, just replace name with name = " Nessiah " + " is the cutest~! "


Climax

So you get the basics of it?
It handles information and lets you manipulate it, with numbers is pretty much the same.


But for numbers, does it go like "3" * 3 as 3x3x3?
Actually I should test it <_<; style="font-weight: bold;">XD


Ooooh it's 333 lol.



By the way, this = is assigning, just like the mathematical symbol equal:
variable is equal to value
variable = value

If you do not assign a variable it's nil.
Also can do math. But let's do something worthwhile towards your first (technically second) script. Do this:

name = "Nessiah"
age = 18

p name + " is " + age.to_s + " years old."

exit

age.to_s is a property, it makes a number into a string so it can be added to the other string. You cant add numbers to letters due to logic but when turned into a string it turns 18 into this "18."

so instead of being read as numbers. It's read as letters
18.to_s is the same as doing this "18"
Just as property like reversing or capitalize.

Now to make your first method. Methods are like commands you make to do repetive stuff, Let's say you want a method to print your name.


def print_my_name
p "Nessiah"
return
end

print_my_name
exit

def means define, you define the name of your method like you did with variables but this time you add def variable, def print_my_name. It's like a variable, but dont be fooled, It's much more useful and has it's own properties. You see what good is a method, if it doesnt allow you to modify it and that's where those mysterious def method( arg ) come into play.

You dont want it to always print your name. What if you have some other name in mind?
You add an argument to the method



So def variable has a property called argument?




It's more of a function. Let me show you

def print_my_name(name)
p name
return
end

print_my_name("Archeia")
exit

See how i added ( name ) and then added print_my_name("Archeia")?



Yes~



Well that's the argument. Think of as a telling someone to do something and even though is an action they can do normally. Add some instruction, like cook. Cook what? Cook( fish )
If you change the "Archeia" it will print any name

This is a bit of the hard part, somewhat of a hurdle for some.
But once you get it...


Scripts are yours
Now tell me how would you call something
You tell someone to do

how to call something ?
def? *is confused*


I mean in your life, Real life.
How would you call something you tell someone to do?
Like Go brush your teeth.





It's ok if you dont get it
Just try saying something


Let's say I Want to make something clean a dishes
How will I make it clean the dishes?


No, It's a command archie
You command someone or something
To do something you ask.

Aaaahhh



Like I command you to make my game for me.



No.



...
Anyway.


Think of def as commands
def command end

Within def command end
Goes what you want it to do

In our case with
def print_my name
It was printing a name
p "Nessiah"

what end means is that since it's a method, it needs a place to start and a place to end
def is where it starts with the name. End is where it ends the process of the command


I got it.



Try making a method? Method is the command btw.
It's name is method in programming, but I want to familiarize it with you


It's ok. So method is a command. Actually after you said that, a lot of things made sense.



Yep
Well let's see another way to connect it
See how RPG maker has it's commands?

Tell me one you remember off the top of your head from VX
Ok I heard SE play
#--------------------------------------------------------------------------
# * Play SE
#--------------------------------------------------------------------------
def command_250
@params[0].play
return true
end
As you can see it's called command_250, which plays a sound effect based off a parameter value. What does @ do? @params[0].play?


@params draw from the program. It's a variable assgined through the engine itself
Basically the paramenters of the command, Like SE volume, SE pitch, except bunched in an array. But as you can see I just wanted to show you a simple method


It's ok!


Now for a method you can use now. Rememeber the commands right?



Or methods. Well they have this things called arguments, which are for a lack of a better word arguments. Like in an argument. I tell you my name in an argument or conversation and you remember it.

Got it. So wait, uh how to say does argument make you remember a command or something? I kinda got lost there.


Arguments are part of the commands. They're a way to add instructions to a command
print_my_name, Remember that? It prints your name, in a small box.


yes
I also remember the (name)


Yep were getting to that
You understand what that methods does?
def command name return means it returns from that point, like in events end process
and end it's the ending point.

Well I want you to make a method that prints your name and your age
No arguments just a simple method/command that will print your name and age



name = Jas
age = 18
then p it

...
...






def method
p "Nessiah is 18 years old"
end

OH! Hahaha. I'm making things much more complex~!
orz


Exactly
No need to always use what I teach you
Use your ingenuity to make things

Usually is less complex than what I make it seem. Now make the same method your way
give it your own name and make it print your own sentence.


def ribbon
p "It is made of silk"
end

ribbon

You did it perfectly! You would use ribbon to call the method from when you need it. Now let's add an argument. Let's say you want it to tell you the color of the silk.


def ribbon(color)
p "It is made of " + color + " silk"
end

ribbon("Red")


You see def ribbon( color )
color is a variable that can be use within your command in this case when we call the command. We can add to the command to tell you a color, and like variables you can set a value to it without calling it:

def ribbon(color = "Purple" )
p "It is made of " + color + " silk"
end

ribbon

But if add this below at ribbon

ribbon("Red")

ah I got it the one below it will overwrite purple right?
Yes
She got it!!

Yey!



Try and make your own method.
In which the argument prints a name with your name as default.


Is it something like this?



def real_name (name = "Jasmin)
p "My name is" + name + "!"
return
end
name ("Nessiah")

Test it yourself and see what happens



oh wait
I missed a " D:


Yep.



def real_name (name = "Jasmin")
p "My name is" + name + "!"
return

end


name ("Nessiah")


Hmm, I can't seem to make it just say the default name.



Well you see Jasmin is a default value, which means in the case you do this
real_name Instead of this: real_name( "Nessiah" ) It's default value would be Jasmin


AHHH I GET IT
oh wow
*facepalm*

I've been overwriting it
hahahaah ahahaha orz...
I can't believe I missed that!

Now to the deep dark secret for you to practice
Arguments can be plenty, not only one.


def method( argument_1, argument_2 )
And so on as long as a command is after each. Now try and make a method with 2 arguments. That prints a name and age with default values of your name and age.
At that would be the last test of the night

def real_name (name = "Jasmin", age = "18")
p "My name is" + name + "and I'm" + age + "!"

return
end


real_name


I got it!
And awww...


Sweet
Now call it with my name and age. If you can.
I want to see if you can guess it first.

def real_name (name = "Jasmin", age = "18")
p "My name is" + name + "and I'm" + age + "!"
return
end

real_name (name = "Archeia", age = "23")

No need for the ( name =, age = )
Just Archeia, "23"


Although you could have used age.to_s
Just to assure, If people put 23 instead of "23"
Since they would most likely use a number and not a string.

I got it~!
I changed it around


Well that's the biggest hurdle of scripting
And you pretty much passed.


Yey!



Tomorow Structure control
I.E if syntax
if archie > 18
chocolate!
end

But yeah familiarize yourself with commands and arguments
And tomorrow we will make a calculator.


Oh wow!



All you're missing is structure control and classes
But tomorrow flow control.


Practice if you can with commands
Make some simple ones, for printing simple stuff


ok!
p "Food!"


Well hope you practice and become better.



ok I'll practive witht his :3
Wow ultimate typo O_o


Tomorow you will have a lot more to look forward too
Also read the help file of VX and search for string


Well it wont help you much...
Seems VX help file sucks



;_____;


You obtained The Ruby Bible x1!!
Go to the
Built-in Classes and Methods > String

pcase str.upcase -> aString
Returns a copy of str with all lowercase letters replaced with their uppercase counterparts. The operation is locale insensitive---only characters ``a'' to ``z'' are affected.

"hEllO".upcase » "HELLO"

It has very good examples



Question, does downcase really have !
last time we did was we added a !


Only practice with the ones you can read and understand
If something confuses you skip it for now.


That's the same method for compatibility purposes.
Older versions used ! On some methods to return a value.
So both are used But ! returns a nil value if nothing happens.
Unlike the normal versionWhich being more advance just cleans it up


Well good luck Nessiah and have a nice day.


Thank you!