Python Robot Implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nnobakht
    New Member
    • Nov 2006
    • 1

    #1

    Python Robot Implementation

    Hi, I'm working on an assignment for school which i am a bit stuck on. The assignment is to make robot which i have been given the library for move around different boards and collecting "coins" and avoiding "blocks" and not falling of the edge. The full assignment is outlined at http://www.cs.sfu.ca/CC/120/ggbaker/assign-1067/assign4 and the robot library is at http://www.cs.sfu.ca/CC/120/ggbaker/.../assign4-robot, i have written some of the code from my algorithm and pseudocode that i came up with but it does not work for all the boards that are available. here is a copy of my code.
    Code:
    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()
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I haven't looked too closely at your code, but I do see a tricky thing to figure into your algorithm. On the cmpt 120 ass. page the rules state:
    In this configuration, each of these conditions will be true:

    r.sensor(N) == (COIN, 1)
    r.sensor(E) == (WALL, 4)
    r.sensor(S) == (BLOCK, 4)
    r.sensor(W) == (COIN, 4)

    so make sure that you are checking to see if you are blocked first.

    Comment

    Working...