Saturday, January 16, 2010

Guido van Robot: Getting Started

Our first software "toy" is Guido van Robot (aka GvR). As with many of the things we'll look out on this blog let's start with a little history.

GvR is a Python implementation of Karel the Robot. Karel the Robot was originally created in 1981 by Richard Pattis to help teach students fundamental programming concepts and start them on the way to learning the Pascal programming language. The Karel concept has been adapted to many languages including C/C++, Java and our language of choice, python. To learn more about GvR's history visit the History section of the GvR website.

To learn more about the Python programming visit its home on the web at python.org. One of Python's strengths is that it has a simple syntax that makes it easy to learn. GvR implements the Karel concept using a Python like syntax. So after some time with GvR you can start to look at other tutorials on Python and have a leg up. In a later entry I'll provide some additional python resources for further exploration.

Getting GvR
You can get GvR via the download page on the GvR site. The GvR project is hosted on the open source project site sourceforge.net. Follow the link to Download GvRng. If you're using Microsoft Windows there is an installer exe file that has everything needed, including Python. If you are using Linux, the GvR site tells you that you'll need Python and PyGTK. You'll also need the python-gtksourceview package from your distribution. Once you get it installed start it up.

Building your first world
I encourage you to work with the lessons on the GvR site, which are adaptations of the original Karel exercises. We're going to combine some of the pieces in those lessons for our first set of programs.  So here comes your first bit of type in code. 

pwpTurns.wld (type into World Editor)

robot 7 3 N 0
wall 3 7 W
wall 7 7 N
wall 7 3 E
wall 3 2 N



The above code  should be typed into the World Editor tab of the GvR editor. The text is colorized to match what you'll see in GvR. The world editor sets up the map that your robot will move around on. This map consists of 4 "wall" panels and the GvR robot. The map is made up of rows and columns with the robot being placed at an intersection. (In this case the intersection of column 7 and row 3.) The direction of the robot is indicates which way it is facing. (N=up, S=Down, E=Right, W=Left) Walls start are placed on the side of the intersection indicated by the direction. We're going to get the robot to move around a little. When we're done we'll have a program that get's our robot around the map in 5 lines of code.

Step 1: Moving GvR
The first thing we're going to do is move the robot to the first red wall.

Enter the following in to the Code Editor:
move
move
move
move
turnoff

Press the Reload button
Press the Execute button

Well that was easy. The robot moves to the wall and stops. The turnoff instructions gives you a dialog box informing you that the robot has turned off.

Step 2: Getting Loopy 1
That was easy but what if we could move 4 spaces, or any number of spaces in only 2 lines of code? That would make it a lot easier. So let's change our program to what follows:

do 4:
    move
turnoff

Once you've got this in the code editor click Reload and Execute.

The result should be the same but in only 3 lines instead of the 5 lines of the first program. That's a 40% reduction in lines of code. Loops are really useful. We'll look at a different variety of loop in a couple of steps.

Speaking of steps now would be a good time to try the Step option of GvR. Reload your program and instead of pressing Execute press Step. Continue to press Step until you reach the end of the program. Stepping through a program can be useful when the program doesn't do what you want. You can watch what happens as you step through each command. As you experiment with GvR the step option will be a good way to check your programs.

Step 3: Turning GvR
The GvR robot wouldn't be much use if it could only go straight. So it has an instruction turnleft, that allows you to turn the robot. In keeping with the Karel concept of simplicity, the robot only moves forward and only turns left. While this may seem limiting it is all that is needed to get the robot anywhere you need it to go. Add the line turnleft before the turnoff command and then Reload and Execute. The program should look like this:

do 4:
    move
turnleft 
turnoff

Step 4: Going in Circles: Nested loops
One of the things you'll notice is that the move command in the loop is indented. This is a python syntax rule. So let's add an outer loop that will execute our current program 4 times. Change your program to look like what's below.

do 4:
    do 4:
        move
    turnleft 
turnoff

Click Reload and Execute. Now we have a program that does what we set out to do. Our robot goes around in a square.  One last change to the program will give you a springboard to further exploration.

Step 5: Making decisions
Our last step is to replace the inner do 4: loop with a new instruction while. The while instruction will continue to loop while the condition provided is true. In this case we will use one of the conditions that GvR understands front_is_clear. Change your program to look like this:

do 4:
    while front_is_clear:
        move
    turnleft
turnoff

What our program is going to do is move forward until it finds a wall. It will then turn left and do it again, a total of 4 times. This program could be used with a different world with the walls set at other locations as long as the path is 4 legs long.

Experiment on your own. Next time will explore the GvR concept of "beepers" and conditional statements.  

Playing with Programming?

Welcome to this new blog. The goal of Playing with Programming is to explore computers and computing with an eye to the past. The time period from 1977 to 1985 is often referred to as "The Golden Age of 8-bit Computers". Many manufacturers had success with a variety of computer designs. The following is a limited list. The links will take you to OLD-COMPUTERS.COM an on-line museum of computing history. Some of these systems I've played with, at least one I own. The first computer system I purchased was the brown plastic classic, the Commodore 64. Its still in a closet because someday I'll make it an exhibit in a small computer museum.
While I put the arbitrary date of 1985 on the end of the 8-bit era, Commodore was making and selling versions of the C-64 until 1993! The 1985 date reflects the year that both Commodore (Amiga 1000) and Atari (520 ST) introduced systems based on the 16/32-bit Motorola 68000 processor. The same chip that Apple used in the original Macintosh launched a year earlier.

But as any computer user knows, hardware is only half of the system. Software is the other half. All of these systems contained some variety of the BASIC programming language. Each one tweaked just a bit to allow access to the those features that made the computers distinctive. Some of these systems had cassette tape drives for data storage, some 5 1/4 inch floppies, some cartridge slots like the game machines of the time. (Most notably the Atari 2600) One way that you could put programs into your computer was to type them in. If you had no cartridge in the computer it would boot to BASIC and you could start typing in your program. If you did this you wanted a tape or disk drive to store your hard work. Where did the programs come from? The magazines of the time often published articles that included type in code. Computer hobbyists would by the magazines and type in the programs. In doing so they had something else to use there computer for, but also learned a little more about how the system worked. The classic magazines of the period are Byte, Compute! and Creative Computing. (Thanks to the folks at the Classic Computer Magazine Archive some of these early magazines are available on line.)

I know what you're think, "enough already with the trip down memory lane." You're right. So what will we be looking at in this blog? There are many tools that have been designed to help students learn what programming is all about. My intention in this blog is to introduce you to some of those tools and provide you with some "type in" programs. Many of the tools have a number of available type in programs created as parts of lesson plans for using the tools to teach. Links will provided for those. I'll also provide some of my own programs for you to use with full notes on what the program is doing and why. One other thing you should know as you read this. Many of these posts will be collected and edited into articles for the Twin Cities PC User Group monthly newsletter the Digital Viking.

Starting Out
The first program we will use is called Guido van Robot or GvR for short. GvR is an adaptation of the program Karel the Robot that was written in the early 1980s as a way to teach students programming with a simplified version of the Pascal language. GvR was developed in the 2000s to do the same using the Python language. Visit the history page of the GvR site for more information on its development.

Some other programs we'll play with in the future include Alice, a java based 3D environment created at Carnegie-Mellon University, and Etoys based on the Squeak language a variant of Smalltalk. We'll add to this list. As we move forward. Please feel free to suggest other tools in the comments section.

Thanks for stopping by we'll get started with GvR in the next post.