Vous êtes sur la page 1sur 10

3. First program As mentioned previously, there are two main windows used to interact with Reeborg.

To the left is a program window; this is where you write the instructions for Reeborg to follow. To the right is the actual "graphical world" window in which Reeborg moves according to the instructions received. Click on the Open Program button and select the file "move1.rur". (Note: dependings on the settings on your computer, you may or may not see the ".rur" extension.) You should see the following computer code, or code for short, appear in the program window. move() turn_off() The word code is synonymous with "program text". A program is a series of instructions. In this case, the code consists of two instructions: move(): instructs Reeborg to move forward one "step". turn_off(): tells Reeborg that we have no more instructions to give him and that he can turn itself off, to save energy. Reeborg recognizes instructions as commands that he must obey by the presence of two parentheses () which follow the words making up the instructions. Now, click on the button and watch Reeborg move in his world. Your turn Change the program by adding a second move() instruction as follows: move() move() turn_off() Click on the Save button and give it the name "move2" (the program will automatically add the ".rur" extension). Congratulations! You have just created your first program. Now, ask Reeborg to execute or to run the program by clicking on the button. Experiment! You can ask Reeborg to take more than two steps. What happens if you ask Reeborg to take so many steps that he has to move beyond the world boundary? In particular, look at the program window; you should have noticed by now that instructions are highlighted as they are being executed by Reeborg.

4. Dealing with eRRoRs Reeborg can follow instructions to the letter - which is both a good thing and a bad thing. For example, write the following program Move() turn_off() Page 1 of 10

save it under "wrong_move.rur", and try to execute it. Reeborg will complain in his own particular way. This is because, to Reeborg, the letters M and m (uppercase and lowercase respectively) are different. Your turn Make sure you try it to see exactly what happens. In the future, if you see the same kind of complaint from Reeborg, it might help you discover what's wrong with your program. In other words, you will find how to recognize one kind of bug. You will learn more about bugs a bit later. Well, you might learn sooner rather than later, if you write your own programs with bugs in them! Quiz What is the shortest valid program you can write? By valid program, we mean one that Reeborg will execute without giving any error messages. Make sure you try it to see if this simple program works as expected. 5. Any comments? Avez-vous des commentaires? Open the first program that you have written. You did save it and keep it as I asked you to, didn't you? If not, rewrite it while I wait for you. Ok, your program should now be displayed in the program window. Now, remember what I told you was the most important thing to do when writing a computer program? If not, go back to the very beginning and read again. I'm waiting ... Got it? Right, the most important thing to do is to make it easy for other people to read your program. This takes a lot of practice and usually requires a fair bit of thinking. However, we can use a trick of writing notes meant for other humans only (and not for the computer) inside the program. We call these notes comments. There are a few ways to write comments in a program. I'm going to teach you the simplest way used in Python. Add the following text as the first line of your program: # My first program So that it should now look like the following: # My first program move() move() turn_off() If you are not colour blind, you will see that the line which begins with the symbol #, appears in green. This symbol indicates that the rest of the line is a comment which is ignored by Reeborg (or Python). Having the text for comments shown in green helps us distinguish comments from instructions. There is no special reason for the colour choice; I just chose to write RUR-PLE so that it would appear like that. Your turn What happens if you put the symbol # at the beginning of a command line? Try it, save the resulting program and run it by clicking on the button to see what Reeborg makes of it. Teaching Reeborg some French While the creators of Reeborg designed him so that he obeys instructions in English, they realised that not everyone understands English. So, they gave him the ability to easily learn a second language. For Page 2 of 10

example, if we want to tell someone to "move forward" in French, we would say "avance". We can tell Reeborg that "avance" is a synonym of "move" simply by writing avance = move. The order here is important; the known command has to be on the right, and the new one has to be on the left. Note that we don't have any parentheses "()" appearing since the parentheses would tell Reeborg that we want him to obey an instruction; here, we are simply teaching him a new word. When we want Reeborg to follow the new instruction, we will use avance(). Your turn Write a program with the following instructions: avance = move avance() turn_off() and save it under "avance.rur". Now, try your new program. Experiment! If you want, you can also teach Reeborg a synonym for turn_off. Or, you may give synonyms in a language other than French if you prefer, even creating your own language. Then, watch Reeborg as he obeys instructions written in your language.

6. Making a left turn. As we have mentioned before, Reeborg can turn in any direction ... provided it is to his left, and by 90 degrees at that. This is because his creators were too cheap to implement a better steering mechanism. To ask Reeborg to turn left you write ... turn_left()! For example, if Reeborg is starting from his usual initial position, this simple program move() turn_left() move() turn_off() will give rise to the following display. Your turn Have Reeborg trace a simple square, using the following program: move() turn_left() move() turn_left() move() turn_left() move() turn_left() turn_off() Remember that you must save your program before you can use it. Try it! Note that, if English is not your favourite language, you can always create a synonym in your language, as long as you define it first, before using it. However, the synonym you introduce must use the English Page 3 of 10

alphabet (letters without any accents). For example, in French, one might define vire_a_gauche = turn_left and use vire_a_gauche() to instruct the robot to turn left. Your turn again! Write a program that will make Reeborg turn counterclockwise around a square, taking 2 steps forward then turning left and repeating until he is back at his starting point facing east, his original direction, as illustrated below. Experiment! Feel free to try to write other programs to make Reeborg move along various paths in his world. Going home When you enter Reeborg's world, Reeborg is usually standing on the corner of the first avenue and the first street, facing East. Click on the "Show/Hide world file" button , which is at the extreme right. You will see the display below change, with some text appearing on the right hand side. In particular, note the line that says robot = (1, 1, 'E', 0). This indicates the position of Reeborg in his world. The first number is the avenue on which Reeborg is standing, the second is the street, and the letter between the quotes indicates the direction that Reeborg is facing (E = East, N = North, W = West, S = South), East being facing right, North facing up, etc.. The fourth number (0) is the number of beepers that Reeborg carries; we will learn about beepers a bit later. As you have seen before, you can make Reeborg move by using the up and left arrow keys on your computer keyboard. When you so move Reeborg, he doesn't leave any trace of his movements. You should notice however that the text between the parentheses in robot = (...) changes as Reeborg moves, to reflect his new position in his world. Your turn Using the keyboard, move Reeborg until he is standing at the corner of the third avenue and the fourth street, facing South, as indicated below. Then, write a program that will make Reeborg start from this new position and go back to the corner of the first avenue and the first street, facing East as indicated below. Try to have Reeborg take fewer steps and turns to do this than we have shown below.

Page 4 of 10

7. Beepers In this lesson, we will learn about the all important beepers! Beep! Beep! Reeborg's world includes beepers. These are small objects (drawn fairly big on the screen) that make a small beeping sound when they are turned on. Reeborg can only hear beepers if he is standing right on top of them, on the same corner. He can pick them up and carry them (turned off) in his pockets, or he can put them down (turning them on automatically). You can instruct Reeborg to pick up beepers, through the pick_beeper() command, or to put some down, through the put_beeper() command. If you ask Reeborg to pick up beepers where there are none, or to put down beepers if he doesn't carry any, he will complain and shut down. Your turn Open up the world file beepers1.wld, which should look like the picture below on the left. You open a world file by clicking on the button. Write a program that will have Reeborg pick up the beeper, and put it down after carrying it as illustrated.

8. Darn bugs! Nobody likes to talk about computer bugs. So, I will try to keep this lesson short. All I ask you to do is to read it I won't ask you to write programs with bugs! What is a bug? The origin of the word bug in computer jargon is often attributed to an actual incident where a moth was found inside Harvard University's Mark II computer; apparently this moth had caused the Page 5 of 10

computer to stop working. It was found by the team headed by renowned computer scientist, mathematician, and young naval officer Grace Murray Hopper, who went on to invent the concept of compiler languages in computer programming. Dr. Grace Hopper eventually rose in the U.S. naval hierarchy to the rank of Rear Admiral. The moth was preserved, taped into Hopper's log book, as shown below. Interestingly, the log book included a note saying, "First actual case of bug being found." as you can see. Picture adapted from the public archive of the U.S. Naval Historical Center Actually, the word bug in a technological context is attributed by the Oxford English Dictionary to Thomas Edison. According to the Oxford Dictionary, the following text can apparently be found in the March 11, 1889 edition of the Pall Mall Gazette: Mr. Edison, I was informed, had been up the two previous nights discovering 'a bug' in his phonograph - an expression for solving a difficulty, and implying that some imaginary insect has secreted itself inside and is causing all the trouble. It thus appears that the original 'bug', though it was indeed an insect, was in fact imaginary. Unfortunately, computer bugs, while they are not insects, are also not imaginary. Dealing with bugs In computer jargon, a bug is an error that causes a program to behave in an unexpected way. If you are writing computer programs, you are going to have bugs in them sooner or later - everybody does. Good programmers seek to "remove" bugs or "fix" them as soon as they find that their program behaves unexpectedly. RUR-PLE has been designed to help you find bugs. 1. The "oil leak" of the robot, leaves a trace behind that allows you to see (trace) the instructions followed by the robot.

Page 6 of 10

Page 7 of 10

Page 8 of 10

Page 9 of 10

Page 10 of 10

Vous aimerez peut-être aussi