|
In this tutorial you will learn how to program MowerJo. MowerJo is
programmed using standard JavaScript. The commands that you will use
to command MowerJo are functions written in JavaScript. We
will learn to program MowerJo by example. Be aware: if
you enter the code incorrectly, MowerJo will not do anything.
Step 1: In the Code box on the main page
type the following: move()
This command causes MowerJo to move forward one step.
Step 2: Click the Load Code Button.
When you click this button a new window opens displaying the field.
Step 3: Click the Initialize Button.
Clicking this button loads MowerJo into the window. Notice that the
button now has the caption Execute Code.
Step 4: Now click the Execute Code Button. When
you click this button this time,the commands in the Code Box are
executed. A messagebox should appear indicating that you hit a
wall. You hit the wall because you were facing North (up) and tried
to move forward one step. If you hit a wall, the game is over and
you need to close the window.
Step 5: In the main window select the Initial Direction Drop Down List
and select S for South. Ensure that the command
move() is still in the Code Box. Now click
the Load Code Button and the Initialize Button.
Notice the MowerJo is now facing South (down).
Step 6: Now click the Execute Code Button and notice that
MowerJo moves South. You may continue clicking the button until MowerJo
hits the wall. Close this window.
Step 7: Now modify the code in the Code Box so that it
has the following code:
move()
turnLeft()
Step 8: Load the code, initialize, and execute the code. Click the
Execute Code Button several times. Notice that MowerJo drives in a
circle.
Step 9: Now we want to make MowerJo smart so that he can clear the
perimeter of the yard without hitting the wall. We will use an if
statement and the conditional pathIsClear() to determine
if the path in front of him is clear. The conditional pathIsClear()
is a function that determines if there is a wall or lawn gnome in front of
MowerJo. If the path is clear then the function returns "true"; otherwise
it returns "false". Type the following into the Code Box:
if(pathIsClear()){
move()
}
else{
turnLeft()
}
Step 10: Now we will use the while statement to clear the
perimeter. Type the following into the Code Box:
while(pathIsClear()){
move()
}
while(!pathIsClear()){
turnLeft()
}
Step 11: Load the code and execute it. Notice how MowerJo does
several things each time that you click the Execute Code Button.
Step 12: Now try the following tasks. If you get stuck, you can
check the solutions here.
-
Program MowerJo so that he zig-zags down the page from the top left corner to
the bottom right corner. Each time you click the Execute Code Button,
MowerJo should move forward one step, turn left, move, and turn right.
-
Repeat #1 but modify the code so that MowerJo zig-zags down the page from the
top left to the bottom right corner with only one click of the Execute Code
Button.
-
Program MowerJo so that he cuts the entire yard. Each time you click the
Execute Code Button, MowerJo should move forward one step or
turn.
-
Repeat #3 but modify the code so that MowerJo cuts the entire yard with one
click of the Execute Code Button.
-
Program MowerJo so that he clears the entire yard when starting in the middle
of the yard at location 5,5.
|