Learn to think like a computer, to code like a guru [Part 2]

17 May

So I’m back from work and ready to have some fun, I guess I will enjoy some play-station 3 games. Since I have already mastered how to give orders to my robot “Jazari, using the “Sequence” rule, I will ask him to get me “Kill zone 3” disk.

  1.  Move 10 meters forward
  2. Turn 90 degrees left
  3. Move 5 meters forward
  4. Grasp killzone 3 disk from the table using right hand
  5. Move 5 meters backward
  6. Turn 90 degrees right
  7. Move 10 meters backward
  8. Un grasp killzone 3 from right hand

Okay, I will load this  “sequence” of orders into Jazari, and wait for him to bring me the game. Until he is back please read the above steps carefully and make sure you don’t have any problems with them.

Clock

Time is ticking

It seems that “Jazari” is taking a bit longer than it should to get me the disk. I will go to check what’s wrong with him. I can see that Jazari is standing right infront of my disks not doing anything at all. what could have gone wrong? I’m pretty sure that the sequence is correct. Anyways I’m standing right infront of my discs, I will grab the disc myself.

Jazari has hanged

I can’t find my “Killzone 3” disc, ohh I have just remembered that I gave it to a friend last week, that’s okay I can play “resident evil 5” for today. Now I understand why Jazari has stopped working. He couldn’t continue the execution of the sequence as he couldn’t execute one of the commands in the sequence.

If a computer or a robot failed to execute a command in a sequence, it will not continue to execute the next commands and it will keep waiting in it’s place forever without doing anything which is known an a hang.

Okay, now we need to think what to do to avoid such a hang. We need something more than just a sequence of commands that are executed from top to bottom. Something that enable us to make Jazari execute a specific set of  commands only under certain conditions. Here comes the second rule we found in Jazari’s manual, “Branching”.

Branching can be defined as the ability to execute a specific set of commands when certain conditions are met, if these certain conditions are not met then this specific  set of commands will never be executed.

Let’s rewrite our order to Jazari  using both rules, sequence and branching. But first let me push the reset button to restart Jazari, and pull it back to my living room.

1- Move 10 meters forward
2- Turn 90 degrees to left
3- Move 5 meters forward
4- If killzone 3 disk is on the table
Begin if
Grasp killzone 3 disc from the table using right hand
End if
else
Begin else
Grasp resident evil 5 disc from the table using right hand
End else
6- Move 5 meters backward
7- Turn 90 degrees to right
8- Move 10 meters backward
9- Un grasp the disk from right hand

Let’s see what Jazari will do this time. Let me push that button and wait. That has worked, in less than 20 seconds Jazari is back with resident evil 5 disk and has dropped it on my living room carpet.

here we have asked Jazari to move to the table where I put my games, then take a look and see if Killzone 3 disk is on the table or not. then execute the commands between “begin If” and “end if” when killzone 3 disk is on the table otherwise it will execute the commands between “begin else” and “end else”. after that it will return back to my living room and leave the disk from his hand (which could be killzone 3 or resident evil 5 according to the availability of killzone 3 on the table). In our case Jazari has returned with resident evil 5 as killzone 3 is not available on the table.

You might have noticed that branching has changed the order of execution as the command “Grasp killzone 3 disc from the table using right hand” was not executed although it is located before “Grasp resident evil 5 disc from the table using right hand”. 

To better understand branching we will draw an illustrative diagram to show why its called branching and to show the execution direction as it is no longer from top to bottom.

Click the image for a high resolution picture

As you can see the execution is moving from top to bottom, until it reaches the “if” command (The area highlighted in green) by then there are two branches for Jazari to choose which one of them to execute, he checks if the killzone 3 disk is on the table or not. Since I have lent the killzone 3 disk to a friend the execution direction will change to the left branch where Jazari will pick up resident evil 5 disk (which I’m 100% sure its on the table). after then Jazari  will execute the next commands (under the green area) normally in order.

Now what If the killzone 3 was on the table? The execution direction was going to change to the right branch where Jazari was going to pick up killzone 3 disk, then Jazari  will execute the next commands (under the green area) normally in order.

As you can see from the diagram, I have highlighted the branching area with green, also I given the “if” command a single number in the sequence. I did that because the “if” command is treated as one block, which means that it will always be executed. The “if” command execution is simply the execution of either the commands between the “begin if” and “end if” when the condition is correct or execution of the commands between the “begin else” and “end else”, when the condition is not correct.

The if command takes this form, and it must be always written that way, anything written in bold must always be written. as I have just said the “if” command is a single command although it is written on multiple lines.

If certain condition is correct
Begin if
Put here the list of commands you want to execute if the condition is correct
End if
Else
Begin Else
put here the list of commands you want to execute if the condition is not correct
End Else


how can an “if” be a single command while it’s written on multiple lines?
To answer this question I will ask you this question, is the below a single command or multiple commands?

Move
10 meters
forward

What if I wrote it that way on a single line

Move 10 meters forward

Is it a single command or multiple commands?

Well, computers and robots don’t see things the way we humans see, for example computers and robots don’t know what lines are. They don’t even care if a command is written on a single line or multiple lines. In fact computers and robots come from the factory with a limited set of commands that the factory engineers have put into them, for example, we have seen the “move” command that make Jazari moves forward or backward. Where do you think I got move from? I got it from Jazari’s catalog.

Now I’m telling you that Jazari’s engineers have indicated that the “if” is a single command, so from Jazari’s point of view it is a single command. Train your mind to accept this fact as you are here to learn how to think like a computer.

To help your mind accept the fact that “if” is a single command always imagine it as drawn below, As you can see it looks like a single block now surrounded with dotted line. so when you want to use an if command you will just put that block, then write down the commands you want between begin if, end if , begin else, and end else. I hope that helps.

If command

Again remember that when you include the “if” command in a sequence you either include it all or nothing at all. There is no such a thing as an “if” without “begin if”, “end if”, “else”, “begin else”, and “end else”. The max you can do, is to write or not write any commands between “begin if”, “end if”,  “begin else” and “end else”.

Why I’m stressing so hard on that “if” is a single command?

First let’s assume that  “resident evil 5” disk was not on the table.

1- what Jazari was going to do? Why?

2- what can we do in this case?

Think about these questions and i will provide the answer in the next post. But If you were able to answer both questions then congratulations, your mind is starting to think like a computer.

4 Responses to “Learn to think like a computer, to code like a guru [Part 2]”

  1. xthelostkingx May 18, 2012 at 2:43 am #

    Thanks again!
    Will the answers of the questions are :
    1- He is going to stop because he failed to execute the “if” command.
    2- We can use multiple conditions by using “else if” and ask the robot to search for more games available on the table (if we 100% sure that the game we are looking for is on the table) or we can change the statements under “else” to grab any available game on the table.
    I hope I was right … keep your lessons going on. 🙂

    • abmera May 18, 2012 at 4:32 am #

      You are perfectly correct, do you have any previous programming knowledge?

      • xthelostkingx May 18, 2012 at 5:14 am #

        Yeah, I played a little with Visual Basic at school and now I am trying to learn c++ because as you know it is the game development standard.

      • abmera May 18, 2012 at 5:29 am #

        C/C++ is my first love in programming. Good luck with that

Leave a comment