By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Python Robot Implementation

P: 1
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.
Expand|Select|Wrap|Line Numbers
  1. from robotlib import *
  2.  
  3. r=Robot(testboard=8)
  4.  
  5. def start(W,N):
  6.     """
  7.     Moves Robot to (0,0)
  8.     """
  9.     a,b=r.sensor(W)
  10.     c,d=r.sensor(N)
  11.     while str(a)=="BLOCK":
  12.         if int(d)!=1:
  13.             r.move(N)
  14.         a,b=r.sensor(W)
  15.         c,d=r.sensor(N)
  16.     for i in range(int(b)-1):
  17.         r.move(W)
  18.  
  19.     c,d=r.sensor(N)
  20.     if str(c)!="BLOCK":
  21.         for i in range(int(d)-1):
  22.             r.move(N)
  23.     while str(c)=="BLOCK":
  24.         r.move(E)
  25.         for i in range(int(d)-1):
  26.             r.move(N)
  27.         c,d=r.sensor(N)
  28.  
  29.  
  30.  
  31. def check1(direction):
  32.     """
  33.     Checks to see if there is a wall in the row. If yes move down and skip
  34.     row there is nothing to gather or go around.
  35.     """
  36.  
  37.     a,b=r.sensor(direction)
  38.     e,h=r.sensor(S)
  39.     if str(a)=="WALL" and int(h)!=1 :
  40.         r.move(S)
  41.         g,h=r.sensor(S)
  42.  
  43. def check2(direction):
  44.     """
  45.     Checks to see if block is in the way. If yes find way to go around.
  46.     """
  47.  
  48.     a,b=r.sensor(direction)
  49.     if str(a)=="BLOCK":
  50.         for i in range(int(b)-1):
  51.             r.move(direction)
  52.         block1(direction)
  53.  
  54.  
  55. def check3(direction):
  56.     """
  57.     Checks to see if coin is in row. If yes go forward and gather all the
  58.     coins then move back to first colum.
  59.     """
  60.  
  61.     a,b=r.sensor(direction)
  62.     while str(a)=="COIN":
  63.         r.move(direction)
  64.         a,b=r.sensor(direction)
  65.         c,d=r.sensor(W)
  66.     x,y=r.currentPosition()
  67.     g,h=r.sensor(W)
  68.     if str(g)!="BLOCK":
  69.         for i in range(int(x)):
  70.             r.move(W)
  71.     else:
  72.         block2(direction)
  73.  
  74. def block1(direction):
  75.     a,b=r.sensor(N)
  76.     c,d=r.sensor(S)
  77.     if int(b)>1:
  78.         r.move(N)
  79.         r.move(direction)
  80.         e,f=r.sensor(E)
  81.         if str(e)=="WALL":
  82.             g,h=r.sensor(S)
  83.             while int(h)==1 and str(g)=="BLOCK":
  84.                 r.move(E)
  85.                 g,h=r.sensor(E)
  86.         r.move(S)
  87. def block2(direction):
  88.     c,d=r.sensor(W)
  89.     e,f=r.sensor(S)
  90.     if str(c)=="BLOCK" and int(d)<2:
  91.         r.move(S)
  92.  
  93.  
  94. """        
  95. x,y=r.currentPosition()
  96. a,b=r.sensor(W)
  97. c,d=r.sensor(N)
  98. if str(a)=="WALL":
  99.     for i in range(int(x)):
  100.         r.move(W)
  101. if str(c)=="WALL":
  102.     for i in range(int(y)):
  103.         r.move(N)
  104. """
  105. start(W,N)
  106. num=r.coinsLeft()
  107. while int(num)!=0:
  108.     check1(E)
  109.     check2(E)
  110.     check3(E)
  111.     num=r.coinsLeft()
  112.  
  113.  
  114.  
Nov 30 '06 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
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.
Nov 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.