목차

이전 항목

5. Argument de méthode

다음 항목

7. Javascript

현재 문서

주의사항

해당 문서는 중요 변경 작업 중에 있습니다. 일부 문서에 텍스트가 빠져있거나, 불어로 플레이스홀더(placeholder) 자리차지만 되어 있을 수 있습니다.

6. Turn right ??

In the last lesson, I mislead you. Here’s the code we ended with:

class RepairedRobot(UsedRobot):
    def turn_right(self):
        for i in range(3):
            self.turn_left()

The robot is not repaired: it still accomplishes right turns by turning left three times. We really need to fix this. However, in order to do so, we will need to learn a bit more about the actual program that controls the robot. This is going to be a bit long, but very worthwhile in the end. Along the way, you will learn new Python concepts ... and you will even see some and understand some Javascript code!

6.1. Digging into Reeborg’s code

Open Reeborg’s Diary. If you want, you can also hide the world by clicking on the “World” button.

Enter and execute the following code and look at the result in the Diary:

r = UsedRobot()
dir_js(r)

dir_js is a Javascript function, understood by Python/Brython, that I wrote to enable you to see an object’s methods and attributes. Right now, it does not tell us much. Here is what I get when I do this:

__class__
body

주석

I use a single letter r for the robot name as this is a very short program and I don’t need a descriptive name.

We do not know if they are methods or attributes. __call__ starts and ends with two underscore characters; this is a convention in the Python world to denote some internal Python code that is mostly reserved for advanced programmers. The other is body. So, we know that r.body is something. Run the following code:

r = UsedRobot()
dir_js(r.body)

주석

Python programmers use a convention where variable names that start with an underscore, like _prev_x are meant to indicate that they are “private” and should normally not be changed by another programmer.

You should see something like:

x
y
objects
orientation
_is_leaky
_prev_x
_prev_y
_prev_orientation

You might have guessed that objects refers to the various objects (tokens, etc.) carried by Reeborg. Also, various challenges told you that Reeborg is at the correct x position, and similarly for y. So it would seem likely that x and y refer to Reeborg’s position. As a programmer, your first reflex should be write a program and see if this is the case.

Try this!

Select the world Empty, turn off the highlighting, and execute the following code:

jumper = UsedRobot()  # mutant robot that teleports itself
jumper.body.x = 8
jumper.body.y = 10

All you should see is a robot created at x=1, y=1 ... which might not be what you have expected. If you turn on the highlighting and run the program again, you will get the expected result.

Try this!

Turn off the highlighting and add the following instruction:

jumper.turn_left()

at the end of your program and run it again.

6.2. What happened?

You may recall from previous tutorials that Reeborg’s actions are recorded (like a movie) and played back one “frame” at a time. The recording of a given state happens when some special instructions are given. By changing the value of the attribute x or y of the jumper.body object, you do not trigger a frame recording. However, by adding a turn_left() instruction at the end, we do make a recording of the situation, which shows us that the previous instructions did indeed change the robot’s position. Alternatively, the code highlighing utility triggers a frame recording so that the line of code about to be executed can be shown with the more recent state of the world.

So, assuming we want to have the program work perfectly, whether or not the code highlighting is on or not, how can we trigger a frame recording without using an existing method which could cause the robot to not end up in our desired position or orientation? The answer will be provided by looking at the Javascript code powering most of Reeborg’s World.

6.3. Javascript !?

If we are going to look at some Javascript code and you are reading this tutorial with Python as your first (and only) programming language, you might be wondering if you made a mistake in choosing Python over Javascript.

Don’t worry, you did not.

You already know about libraries; chances are there are some functions defined in yours on Reeborg’s World. Libraries are sometimes written in a different language than the main programming one. For numerical work, Fortran has long been the language of choice and most numerical libraries have been written in Fortran. Many other libraries have been written in the C language.

Python is sometimes described as a glue language. You can write Python programs that make use of functions found in Fortran and C libraries. Usually, to make use of such libraries, one refers to documentation written that indicates what functions can be called and how.

You can think of the Javascript code powering most of Reeborg’s world as a special library. However, no documentation on that library exists. The way to find out about the functions existing in this library are to look at the code itself, which is what we are about to do. However, before we do this, you should take a quick crash course on Javascript.

Do this!

Read the one page tutorial on Converting Python code into Javascript. By doing the reverse steps, you could convert Javascript into Python. The quick tutorial will teach you enough to be able to get all the information you need from the Javascript code inside Reeborg’s World. Make sure to come back to this page when you are done, so that you can continue with this tutorial.

Now that you have read the quick tutorial on converting Python code into Javascript, it is time to read some Javascript code.