hi guys, I have an assignment that is due on december first and i cant seem to get a wrap around it and was wondering if someone could help. I have been given a Robot module with 17 testboards and some predefined functions such as sensor, move, currentPosition, boardSize and so on and i have to come up with an algorithm and code to make it go around the board picking up the "coins" while avoidingthe "blocks" and without falling off the "edge". the link to the assignment is: http://www.cs.sfu.ca/CC/120/ggbaker/assign-1067/assign4 and the link to the robot module is http://www.cs.sfu.ca/CC/120/ggbaker/assign-1067/assign4-robot . if anyone has anyideas or could help that would be great. thanks

Recommended Answers

All 8 Replies

Interesting project, I will play with it!
Just curious, is this a computer science course? Hard to digest, since most CS departments are bastions of conservative stuff like C++ and Java. Engineering and Science departments luckily have been more pragmatic and open to using Python.

Ya this is a computer science course but it is a first year. It is to get the students used to programming as a whole so they use python since it is a simple language.

If with 'simple' you mean 'not unnecessarily complex', I agree. Python is in many ways more powerfull than C++ or Java. Learning Python first might lead to a lot of pissing and moaning by the students who have to confront the often silly complexities of these other languages later. Well, good karma with your studies.

Have you started pseudo-coding your project? You might find out that it looks almost like Python syntax.

Ya i have some type of a psedocode for it but it cannot handle all of the boards it does most of it but the last 5 and 2 of the others it wont do. if there is a coince in teh middle of 3 blocks it will not be able to do it and so on and the maze im not even sure how to go about that.

I have some of the algorithm down and it works for most of my board but i have problems with certain ones. If there is a board like the one i am attaching. this is the code i have so far but i dont know where to go from here.

from robotlib import *

r=Robot(testboard=8)

def start(W,N):
    """
    Moves Robot to (0,0)
    """
    a,b=r.sensor(W)
    c,d=r.sensor(N)
    while str(a)=="BLOCK":
        if int(d)!=1:
            r.move(N)
        a,b=r.sensor(W)
        c,d=r.sensor(N)
    for i in range(int(b)-1):
        r.move(W)
        
    c,d=r.sensor(N)
    if str(c)!="BLOCK":
        for i in range(int(d)-1):
            r.move(N)
    while str(c)=="BLOCK":
        r.move(E)
        for i in range(int(d)-1):
            r.move(N)
        c,d=r.sensor(N)
    
    
    
def check1(direction):
    """
    Checks to see if there is a wall in the row. If yes move down and skip
    row there is nothing to gather or go around.
    """
    
    a,b=r.sensor(direction)
    e,h=r.sensor(S)
    if str(a)=="WALL" and int(h)!=1 :
        r.move(S)
        g,h=r.sensor(S)
        
def check2(direction):
    """
    Checks to see if block is in the way. If yes find way to go around.
    """

    a,b=r.sensor(direction)
    if str(a)=="BLOCK":
        for i in range(int(b)-1):
            r.move(direction)
        block1(direction)
                    

def check3(direction):
    """
    Checks to see if coin is in row. If yes go forward and gather all the
    coins then move back to first colum.
    """
    
    a,b=r.sensor(direction)
    while str(a)=="COIN":
        r.move(direction)
        a,b=r.sensor(direction)
        c,d=r.sensor(W)
    x,y=r.currentPosition()
    g,h=r.sensor(W)
    if str(g)!="BLOCK":
        for i in range(int(x)):
            r.move(W)
    else:
        block2(direction)

def block1(direction):
    a,b=r.sensor(N)
    c,d=r.sensor(S)
    if int(b)>1:
        r.move(N)
        r.move(direction)
        e,f=r.sensor(E)
        if str(e)=="WALL":
            g,h=r.sensor(S)
            while int(h)==1 and str(g)=="BLOCK":
                r.move(E)
                g,h=r.sensor(E)
        r.move(S)
def block2(direction):
    c,d=r.sensor(W)
    e,f=r.sensor(S)
    if str(c)=="BLOCK" and int(d)<2:
        r.move(S)
        
            
"""        
x,y=r.currentPosition()
a,b=r.sensor(W)
c,d=r.sensor(N)
if str(a)=="WALL":
    for i in range(int(x)):
        r.move(W)
if str(c)=="WALL":
    for i in range(int(y)):
        r.move(N)
"""
start(W,N)
num=r.coinsLeft()
while int(num)!=0:
    check1(E)
    check2(E)
    check3(E)
    num=r.coinsLeft()

This is just a test frame where the robot moves at more or less at random, avoiding walls and blocks. When the robot sees an open coin it moves there and fetches it. Eventually it will get all the coins unless they are totally blocked in. Now one has to add look ahead strategy to minimize the moves. I am having fun with that!

# module robotlib.pyc from: 
# http://www.cs.sfu.ca/CC/120/ggbaker/assign-1067/assign4-robot

#from Tkinter import *
import random
from robotlib import *

# use one of the testboards (0 - 16)
r = Robot(testboard=12)

def look_around():
    """
    directions are N, E, S, W
    obj can be WALL, COIN, BLOCK (avoid hitting Block and WALL)
    dist is distance including the space of obj    
    """
    sensor_list = []
    for dir in [N, E, S, W]:
        obj, dist = r.sensor(dir)
        sensor_list.append((dist, obj, dir))
    return sensor_list

def check_coin():
    """
    check if there is a COIN in item[1] of the tuple
    """
    sensor_list = look_around()
    coin_list = []
    for item in sensor_list:
        if item[1] == COIN:
            coin_list.append(item)
    if coin_list:
        # sort the tuples so closest distance is first
        return sorted(coin_list)
    else:
        return False

def fetch_coin(coin_list):
    # move robot to closest=item[0] of tuple in coin_list[0]
    spaces = coin_list[0][0]
    # move direction=item[2] of tuple in coin_list[0]
    dir = coin_list[0][2]
    for x in range(spaces):
        r.move(dir)

def check_blockwall():
    """
    check if there is a BLOCK or WALL in item[1] of the tuple
    and is it next to the robot
    don't move in any of the directions in the dir_list
    """
    sensor_list = look_around()
    dir_list = []
    for item in sensor_list:
        if item[0] == 1 and (item[1] == BLOCK or item[1] == WALL):
            dir_list.append(item[2])
    return dir_list

def check_wall(dir):
    """want to use this for look ahead strategy"""
    sensor_list = look_around()
    for item in sensor_list:
        if item[1] == WALL and dir == item[2]:
            return item[0]

def next_move(directions):
    # shuffle directions so you don't get stuck in the corners
    random.shuffle(directions)    
    for dir in directions:
        dir_list = check_blockwall()
        if dir not in dir_list:
            r.move(dir)
            break

directions = [N, E, S, W]
moves = 0

while True:
    # is there a coin in any of the four directions
    coin_list = check_coin()
    if coin_list:
        #print coin_list # test
        fetch_coin(coin_list)
    if r.coinsLeft() == 0:
        break
    next_move(directions)
    moves += 1
    print directions, moves  # test

Note: Using random moves the robot has collected all the coins on this particular board in 24 moves up to 136 moves. Whoever teaches this course should be congratulated.

Member Avatar for Mouche

Seeeing if I could just get it to pick up all the coins without errors, I wrote some working code EXCEPT that it had no random thing, so if in its path going back and forth it saw no coins, it got stuck bouncing in a line. I decided to check out Ene Uran's code, implemented the random, and it works! Fun stuff.. (it almost always works; it has a weird flaw of hitting a block every once in awhile)

# Program that directs a robot
from robotlib import *
import random
r = Robot(testboard = 11)

# 1 square Movement functions for the robot; robot 'r' is default
def n(robot = r): robot.move(N)
def e(robot = r): robot.move(E)    
def s(robot = r): robot.move(S)
def w(robot = r): robot.move(W)
def opp(direction):
    if direction == N:
        s()
    if direction == E:
        w()
    if direction == S:
        n()
    if direction == W:
        e()

def sense_all(robot = r):
    get_coin = 0
    sense_N,d_N = robot.sensor(N)
    sense_E,d_E = robot.sensor(E)
    sense_S,d_S = robot.sensor(S)
    sense_W,d_W = robot.sensor(W)
    if sense_N == COIN:
        for num in range(d_N):
            n()
            get_coin += 1
    if sense_E == COIN:
        for num in range(d_E):
            e()
            get_coin += 1
    if sense_S == COIN:
        for num in range(d_S):
            s()
            get_coin += 1
    if sense_W == COIN:
        for num in range(d_W):
            w()
            get_coin += 1
    if get_coin > 0:
        sense_all()
    else:
        return 0

def check_for_wall(direction):
    # if block, turn other way
    wall_that_direction = r.sensor(direction)[0]
    wall_immediately_there = r.sensor(direction)[1]
    if wall_that_direction != COIN and wall_immediately_there == 1:
        opp(direction)
        return 1
def check_hit():
    hit = check_for_wall(E)
    if hit == 1:
        opp(E)
        hit = 0
    hit = check_for_wall(W)
    if hit == 1:
        opp(W)
        hit = 0
    hit = check_for_wall(N)
    if hit == 1:
        opp(N)
        hit = 0
    hit = check_for_wall(S)
    if hit == 1:
        opp(S)
        hit = 0
    
direction = E
not_direction = ""
directions = [N,E,S,W]
while True:
    sense_all()
    if r.coinsLeft() == 0:
        break
    check_hit()
    r.move(direction)
    direction = directions[random.randrange(4)]
print "You win."

If you find errors, please note them, so I can figure this thing out. I'm interested in how to make it more intelligent.

Thanks alot guys for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.