473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
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
1 2587
bartonc
6,596 Expert 4TB
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

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

Similar topics

0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
13
by: Yannick | last post by:
Hi, I would like to program a small game in Python, kind of like robocode (http://robocode.sourceforge.net/). Problem is that I would have to share the CPU between all the robots, and thus...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 431 open ( +3) / 3425 closed ( +8) / 3856 total (+11) Bugs : 916 open (-23) / 6273 closed (+44) / 7189 total (+21) RFE : 244 open...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 430 open ( -4) / 3447 closed (+17) / 3877 total (+13) Bugs : 922 open ( -7) / 6316 closed (+31) / 7238 total (+24) RFE : 245 open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.