Here is a look at Karel++ World with an open world file and an open robot
program. At the top of the screen we have
menus and under them a palette of buttons that repeat menu selections for
convenience. Along the right side we have two additional palettes: first the
Control Palette and, at the far right, the Place Palette. The main part of the
window contains the open files.
Figure 1. A View of Karel++ World for Windows.
You need two files in order to run a program. One file contains the robot program itself. The other describes the robot world with any walls and beepers. To run a program in a world when both of these have been prepared previously, you need to open both files in the Karel++ World. The program and world on page 11 of the text are with the distribution files. The program is called "First.kpp", and the corresponding world is named First.wld.
Figure 2. A World Window with one Beeper.
Find the file First.wld in your Karel++ folder. You can either double click this file (if the appropriate association has been made) or drag it onto the application Karel++ icon. This will start the simulator. You can also start the Karel++ World simulator by double clicking on its icon. You can then select Open from the File menu, or the second palette button from the left at the top of the screen, and navigate to the First.wld file in the file dialog and select and open it.
However you run the World, you should now see a page with the Street and Avenue grid, with a single beeper at 1st Street and 4th Avenue. This window is shown in Figure 2. Note the Street numbers to the left and the Avenue numbers across the top. This window can be scrolled and resized. There are 1000 streets and 1000 avenues in this world. If a robot moves outside this world it can't be seen.
Figure 3. The Program Window
Next, you need to open a Karel++ program to be used within this world. You can go back to the desktop, without quitting the simulator, and double click on the file First.kpp, or you can select Open from the File menu and navigate to this file and open it. This will open another window after a few seconds while the World analyzes the program. This new window will contain the text of the program. You may need to resize this window, but it should look like Figure 3. The window can be moved so that it doesn't cover the world.
To run this program you use the controls in the Control palette. These
controls are graphical rather than textual, however. The Control palette is a
near the right side of the program window.
|
Figure 4. The Control Palette
The Control Palette is shown in Figure 4. It has six controls that are suggestive of the controls found on a VCR.
The first button will Pause a running program. It can be continued after a Pause. When this tool is selected, a left mouse button click will execute one Step of the program, just like the Step tool. A right button click will execute Step Over.
The second buttonwill Stop a running or paused program. A program can be restarted from the beginning after a Stop, but it cannot be continued. The escape key on the keyboard will also halt a running program.
The third button will Run a program automatically, executing one instruction after the other until the end. You can pause the program at any time by hitting the Pause button. You can also Stop it with the Stop button. While a program runs the instruction that is about to be executed next will be highlighted in the program window. When this tool is selected a left mouse click will Pause the program and a right click will Stop it.
The fourth button, Fast Run, will run a program as fast as you computer can execute it. You can also Pause or Stop the execution. The program window will not indicate the instruction being executed during a fast run, however. A short program like First.kpp will probably execute so fast in this mode that you won't have a chance to Pause it. When this tool is selected a left mouse click will Pause the program and a right click will Stop it.
The last two buttons are used to Step through the program. The fifth button will Step through a program, one instruction at a time, pausing at the end of a single instruction. Each instruction will be highlighted in the program window each time it is executed. This is often the most useful way to see what a program is doing in detail. It is also useful to step through a program that has execution errors. When this tool is selected, mouse clicks
The last button is the Step Over button. It will completely execute whatever is highlighted in the program window without stepping through the individual steps of whatever is highlighted. It will then Pause. Since the entire main task block can be thought of as an instruction, and it is initially highlighted at the beginning of an execution, hitting this button at that time will FastRun the entire program.
After you execute a program using the buttons on the Control Palette, you can execute it again and again until you close it or open another program. You can have several world windows open at the same time, but only one program window. With one program and several worlds, you can execute the same program in different worlds.
Figure 5. The World After a Robot has been Delivered
When a robot program is in execution, the robots will appear in it after their delivery specifications have been executed. The robot shows the direction it is facing in the world by the triangle shown on its chest, and, more subtly, by the direction that its eyes point. The robot in Figure 5 is at 1st Street and 2nd Avenue and it is facing East. After a robot executes a turnOff instruction, it will be shaded to indicate that it cannot execute more instructions.
Run the First.kpp program in the First.wol world using each of the four run modes: Run, FastRun, Step and StepOver. In Step mode, you will need to hit the Step button several times. You can also go back and forth between the various modes. Try to Run the program with the Run button and then quickly Pause it. From there you can Step through it for example. These files came with the Karel++ software and should be on your computer; if not, you can download them here: first.kpp and first.wld.
Run the program stair.kpp in the world Stair.wol. These files also should have come with the Karel++ software. If necessary you can download them here: stair.kpp and stair.wld. It is a program that uses new instructions written by a robot programmer.
To write your own robot programs, you need a text editor. Any text editor will do, such as the Windows Notepad. You should be careful about indenting lines in your programs so that they are easy for you and others to read. The part of the simulator called the Parser that reads the program will ignore your indentation, however.
When you prepare a program with a text editor, you won't be able to open it in the Karel++ World by double clicking it. This is because it will have the editor's document icon. You can open it from the File menu of the Karel++ World, or you can drag the file icon over the application icon and release it.
If you want to make a program file open automatically in the Karel++ World instead of in the editor, you can establish an association between the extension .kpp and the Karel World program. Windows Explorer will let you set these associations in its View/Options menu. The file will then get the Karel++ World document icon and will open in the World by double clicking. You should then use the kpp extension only after you are sure that you will be making no more changes to a file, since after doing so it won't open automatically in the editor. You will still be able to open it from the editor's File menu however, or by using drag and drop.
If you write a program that has lexical or syntax errors then Karel++ World will find these and report them to you when you try to open the program file. These must be corrected before you can continue. After you make corrections you can open the file containing the robot program again.
Karel++ World can be used to build robot worlds. To do so requires the use of the tools in the Place Palette. The Place Palette is shown in Figure 6.
|
Figure 6. The Place Palette
The tools in the Place palette are used to place wall and beeper objects in the world. You select a tool from the Place palette with the mouse. When a tool is selected, the cursor shape changes to indicate which tool is selected.
The first tool is None. It is not used to place anything in the world. Select this if you don't want to modify the current world by accident when you press the mouse down in a world window.
The second tool is used to place horizontal wall segments between Streets. When you select this tool, the cursor changes to a similar shape. You can then click the mouse (left button) between streets and across an avenue to place a single wall segment. Click several times in several places to create more wall segments. The right mouse button can be used to remove streets.
The third tool is used to place vertical wall segments between Avenues. It's use is similar to the horizontal wall tool.
The fourth tool is used to place single beepers at a corner in the world. Figure 6 shows a view in which this tool has been selected. The large + sign indicates that beeper numbers on a corner will be increased when the mouse is clicked there. To place a beeper click the mouse on a corner after selecting this tool. To place several beepers on one corner, click several times. The right mouse button can be used to remove beepers.
The last tool is used to place an infinite number of beepers on a corner. Its use is similar to the tools above it.
Sometimes you place a wall or beeper in the wrong place. Each of the tools can be used to remove objects as well as place them. To accomplish this you can click a second time on a tool in the Place Palette to change it to a tool that removes objects of that kind. The Place Palette changes to show the new use. For example, when you have selected the Remove Vertical Wall tool by double clicking, the Place palette looks like Figure 7. When the "removal" tools are used, the right mouse button acts like an "add" tool.
Figure 7. The Place Palette when the Remove Vertical Wall Tool is Selected.
By the way. The Remove Infinite Beepers tool will remove all beepers on a corner if there are several or if there are an infinite number.
Once you have built a world you can save it. You can either select Save or Save As from the File menu, or you can press Alt-FS from the keyboard. You will be prompted for a name with the standard file dialog.
Use a text editor to create the program on page 20 of the book. Then use the world you built above in which to execute the program. The program has errors, of course. Now use the text editor to correct the program.
Next you want to execute the corrected program. But you still have the old program open in the Karel++ World. To execute the changed version, you just need to open the program again. When you open a program file and you already have one open, you will be presented with a confirmation dialog. To avoid this, you can close the original program first, either by selecting Close from the file menu (assuming the program window is on top), or by clicking in the window's close box. Figure 1 shows a typical screen while the user is executing a Karel++ robot program.
You can print a world if it is the top window. Currently only a single page is printed: the page that contains the lower-leftmost corner shown on the screen. You can print the program file that you opened if the top window is the Program Window.
If you write programs that use several robots, the Robot Window will be very helpful. It shows the class, name, and current position of all robots in the world, including the direction they face and the number of beepers in their beeper bags. This window is continuously updated as the Robot Program runs. Figure 8 shows three robots from two different classes. It is from a solution to Problem 5.27.
Figure 8. The Robot Window, showing three Robots.