Not logged in. Login

A4 Hints

The Module

To get you started working with the module, you have have a look at this example robot program. The robot in this program doesn't do anything particularly useful (and might sometimes fall off the board or hit a block), but at least it gets the game started.

from robotlib import Robot, N, S, E, W, COIN, WALL, BLOCK

r = Robot(testboard=1)

for i in range(5):
    r.move(N)
    r.move(E)
    r.move(S)
    r.move(W)

    obj, dist = r.sensor(N)
    if obj == WALL:
        print("I see a wall " + str(dist) + " units north.")

r.exit()

Look at the robotlib documentation for what the methods promise to do/return.

Suggested Plan

The real assignment here is to come up with the best algorithm you can for this problem. The program itself is just an implementation of your algorithm, so there's no real “plan” that can be given for your program. There are a few things you can do to start working on an algorithm.

  • Start by working on an algorithm for boards with no blocks. That way there's nothing to hit: just come up with an algorithm that takes you around the board enough that you can see and grab each coin. Make sure your algorithm never allows the robot to fall off the board.
  • Try the problem with one block on the board. Try it assuming that the block is not on the outside of the board, so you can freely cruise around the outside board, looking for coins. Make sure you test with coins on the same row and column as the block. Make sure your algorithm never lets the robot run into the block.
  • Now, try to modify your algorithm so it also works for any two or three blocks on the board. Again, you might want to assume that none of the blocks are on the edges of the board.
  • There are many ways to attack the problem after this (and even before). Try some stuff: try to come up with a method that will let you avoid obstacles and still find your way around the board.
  • Don't be discouraged if you can't “finish” this problem. It's hard and if more than a couple of students produce an algorithm that works in general, I'll be surprised. [I'd be surprised if the TAs got it without some pain.]

Notice that there's relatively little programming in the above list. Most of your time on this assignment can (and should) be away from the computer.

Directions

The four compass directions you get from the module aren't necessarily easy to work with, depending what you're doing.

You can do something like this:

directions = [N, E, S, W]
…
for d in directions:     # check the sensor in direction d (or whatever you need done)
    …

You can also consider creating functions that take a direction and give you back the value you really need. For example,

  • reverse(d) that returns the opposite direction, so reverse(S)==N.
  • left(d) that returns the direction to the left, so left(N)==W.
  • right(d) that is similar, so right(N)==E.
  • whatever else captures what you want the logic of your code to express.

You could then have the robot spin in a circle like this:

d = N
while True:
    r.move(d)
    d = right(d)
Updated Thu July 18 2024, 10:13 by ggbaker.