473,722 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Robot Implementation

1 New Member
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 2605
bartonc
6,596 Recognized Expert Expert
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
334
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 ( -1) / 229 closed ( +2) / 452 total ( +1) New / Reopened Patches ______________________
13
1668
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 allocate a time period to each robot. However I couldn't find any way to start a thread (robot), and interrupt it after a given time period. Any suggestions on how to proceed? Is Python just not adapted to this kind of things?
0
263
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 ( +4) / 240 closed ( +1) / 484 total ( +5) New / Reopened Patches ______________________
0
278
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) / 241 closed ( +1) / 486 total ( +1) New / Reopened Patches ______________________
0
8860
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9234
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9151
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5994
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.