Wednesday, March 3, 2010

Moving Beepers

As promised in the previous post here is a world and some code for moving beepers.

First the world definition:
robot 2 2 N 0
wall 2 2 W 8
wall 2 9 N 8
wall 9 2 E 8
wall 2 2 S 8
wall 6 2 W 3
wall 6 7 W 3
beepers 5 9 1

If you saved the "two rooms" world file this is the same thing with a couple of changes. First off the last number on the robot line is now 0. This indicates that Guido starts off with no beepers in his beeper bag. The other change is the addition of the beepers line at the end.

Now for some code. Since we don't have an indicator of when to stop, like the bag full of beepers in the previous post, we need to do a counted loop. I made some changes in approach to allow counting the corners that the robot hits. The code looks like this:

define turnright:
   
do 3:
        turnleft

do 
8:
    while front_is_clear:
        if left_is_clear:
            while left_is_clear:
                turnleft
                move
        else:
            move
   
turnright
    if
any_beepers_in_beeper_bag:
        if not_next_to_a_beeper:
            putbeeper
    else:
        if next_to_a_beeper:
            pickbeeper   
turnoff

The main thing here is that we are looking for corners so the while loop is controlled by the front_is_clear condition.

Changes to try: Put the do 8 loop inside of another loop, for example make a do 4 loop. Then indent all of the rest of the code in by 4 spaces at the beginning of the line. Change the number beepers at the point indicated to 4. Run the program and GvR should do 4 laps and place one beeper in each corner of the "second" room.

That's it for now. Next time we'll switch gears to Alice for a little while.

No comments:

Post a Comment