#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    10

    would i use an exception here?


    Hi guys,
    I'd like some input for a problem I'm having:
    I'm writing a program for a bot that has to move around and collect coins (efficiently). It can only move N,S,E,W, and I've done the program so it moves to the nearest coin according to what its sensors tell it.
    Is there a nice looking way to say "you can't move in reverse of the direction you just came from" (ie if the bot moved N two spots, he can now only move N,E,W, to avoid covering the same area again).
    I could do this using an incredibly long if-than, but there has to be a nicer way, however I haven't read anything even remotely suggesting a better way exists to do this. Any suggestions? I'm all ears.
    B.
  2. #2
  3. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    uhhh im really not sure if this would work but here is my idea. it is definately not the fastest way for the program to run either. after it moves N, S, E, or W have it save it as a variable. ie if it moved N make it lastmove = 'N' and so on.

    then have it go through the function (im guessing its a function) that decides where it should move next. then make it nextmove = 'N' if its north etc... at then end have it do" if lastmove == nextmove:" and have it repeat the function if they are the same.

    the only problem there is if it keeps on hitting the same directions it will keep on having to go through the function again. so its not the best way. you also could have 4 seperate functions. use the same thing i had for lastmove = 'N' on the first one. then have an if lastmove == 'n' have it go through a function where it can only go S, E, or W. then have an elif statements for S, E, and W.

    im probably not the best person to get help from but im trying and thats my logic around the problem.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    You could create a dictionary of allowed directions and look up the intended direction there:
    Code:
    allowed_directions = {'N': ['N', 'E', 'W'],
                          'S': ['S', 'E', 'W'],
                          'E': ['N', 'S', 'E'],
                          'W': ['N', 'S', 'W']}
    
    ...
    # check if next direction is valid:
    if nextmove not in allowed_directions[lastmove]:
        # invalid direction,  select nextmove from allowed directions, e.g. 
        nextmove = choose_random(allowed_directions[lastmove])
    This way you don't need a large if-then-else tree.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    and I've done the program so it moves to the nearest coin according to what its sensors tell it.
    Change this bit so it picks the nearest coin that isn't where it just came from according to what its sensors tell it.

    How depends on how you wrote that bit.

    Code:
    coin_directions = Find_nearest_coins_in_order()
    for item in coin_directions:
        if not item == last_direction:
            new_direction = item
    Or similar.

IMN logo majestic logo threadwatch logo seochat tools logo