[go: up one dir, main page]

0% found this document useful (0 votes)
62 views21 pages

Python Answer For DT Worlds-2

The document contains a series of Python scripts using the cs1robots library, showcasing various robot tasks across different worlds. Each section describes the robot's movements, decision-making processes, and interactions with beepers in environments like 'amazing', 'hurdles', and 'harvest'. The scripts illustrate programming concepts such as loops, conditionals, and functions in a robotic context.

Uploaded by

Chala Jemal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views21 pages

Python Answer For DT Worlds-2

The document contains a series of Python scripts using the cs1robots library, showcasing various robot tasks across different worlds. Each section describes the robot's movements, decision-making processes, and interactions with beepers in environments like 'amazing', 'hurdles', and 'harvest'. The scripts illustrate programming concepts such as loops, conditionals, and functions in a robotic context.

Uploaded by

Chala Jemal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Amazing1

from cs1robots import * while not hubo.on_beeper():


load_world("worlds/amazing1.wld") if hubo.right_is_clear():
hubo=Robot(beepers=1) for i in range(3):
hubo.set_pause(0.05) hubo.turn_left()
hubo.set_trace("green") hubo.move()
hubo.drop_beeper() elif hubo.front_is_clear():
hubo.move() hubo.move()
while not hubo.on_beeper(): else:
if hubo.front_is_clear(): hubo.turn_left()
hubo.move() hubo.turn_left()
else:
hubo.turn_left()
hubo.turn_left()

Amazing2 Amazing3
from cs1robots import * from cs1robots import *
load_world("worlds/amazing2.wld") load_world("worlds/amazing3.wld")
hubo=Robot(beepers=1) hubo=Robot(beepers=1)
hubo.set_pause(0.05) hubo.set_pause(0.05)
hubo.set_trace("green") hubo.set_trace("green")
hubo.drop_beeper() hubo.drop_beeper()
hubo.move() hubo.turn_left()
hubo.move()

1
while not hubo.on_beeper(): for i in range(3):
if hubo.right_is_clear(): hubo.turn_left()
for i in range(3): hubo.move()
hubo.turn_left() hubo.pick_beeper()
hubo.move() for i in range(3):
elif hubo.front_is_clear(): hubo.turn_left()
hubo.move() for i in range(5):
else: hubo.move()
hubo.turn_left() hubo.pick_beeper()
hubo.turn_left() hubo.turn_left()
hubo.move()
hubo.pick_beeper()
Amazing3a hubo.turn_left()
from cs1robots import * for i in range(5):
load_world("worlds/amazing3a.wld") hubo.move()
hubo=Robot() hubo.pick_beeper()
hubo.set_pause(0.05) hubo.turn_left()
for i in range(2):
hubo.set_trace("green") hubo.move()
hubo.turn_left() hubo.turn_left()
hubo.pick_beeper() for i in range(6):
for i in range(6): hubo.move()
hubo.move() while hubo.carries_beepers():
hubo.pick_beeper() hubo.drop_beeper()

2
Amazing5 fairy_tale
from cs1robots import * from cs1robots import *
load_world("worlds/amazing5.wld") load_world("worlds/fairy_tale.wld")
hubo=Robot(beepers=1) hubo=Robot(beepers=1)
hubo.set_pause(0.05) hubo.set_pause(0.05)
hubo.set_trace("green") hubo.set_trace("green")
hubo.move() hubo.drop_beeper()
for i in range(2): hubo.move()
hubo.turn_left() a= False
hubo.move() while not hubo.on_beeper():
for i in range(3): if ( not a ) and hubo.right_is_clear():
hubo.turn_left() hubo.turn_left()
hubo.drop_beeper() hubo.move()
hubo.move() a = True
while not hubo.on_beeper(): elif ( a) and hubo.right_is_clear():
if hubo.right_is_clear(): for i in range(3):
for i in range(3): hubo.turn_left()
hubo.turn_left() hubo.move()
hubo.move() elif hubo.front_is_clear():
elif hubo.front_is_clear(): hubo.move()
hubo.move() else:
else: hubo.turn_left()
hubo.turn_left() hubo.turn_left()
hubo.turn_left()

3
frank18
from cs1robots import * hubo.move()
load_world("worlds/frank18.wld") turn_right()
hubo=Robot() for i in range(5):
hubo.set_pause(0.05) hubo.move()
hubo.set_trace("green") while hubo.on_beeper():
def turn_right(): hubo.pick_beeper()
for i in range(3): turn_right()
hubo.turn_left() for i in range(6):
for i in range(2): hubo.move()
hubo.move() for i in range(2):
hubo.turn_left() hubo.turn_left()
for i in range(6): while hubo.carries_beepers():
hubo.move() hubo.drop_beeper()
while hubo.on_beeper():
hubo.pick_beeper()
turn_right()
for i in range(3):
hubo.move()
turn_right()
hubo.move()
while hubo.on_beeper():
hubo.pick_beeper()
hubo.turn_left()

4
hanoi3 hanoi4
from cs1robots import * from cs1robots import *
load_world("worlds/hanoi3.wld") load_world("worlds/hanoi4.wld")
hubo=Robot() hubo=Robot()
hubo.set_trace("green") hubo.set_pause(0.05)
def turn_right(): def turn_right():
for i in range(3): for i in range(3):
hubo.turn_left() hubo.turn_left()
hubo.move() hubo.move()
hubo.turn_left() hubo.turn_left()
for i in range(3): for i in range(4):
while hubo.on_beeper(): while hubo.on_beeper():
hubo.pick_beeper() hubo.pick_beeper()
hubo.move() hubo.move()
for i in range(2): for i in range(2):
hubo.turn_left() hubo.turn_left()
for i in range(3): for i in range(4):
hubo.move() hubo.move()
turn_right() turn_right()
hubo.move() hubo.move()
hubo.turn_left() hubo.turn_left()
hubo.turn_left() hubo.turn_left()
while hubo.carries_beepers(): while hubo.carries_beepers():
hubo.drop_beeper() hubo.drop_beeper()

5
harvset1 def forward():
from cs1robots import* while hubo.front_is_clear():
import time if hubo.on_beeper():
load_world("worlds/harvest1.wld") hubo.pick_beeper()
hubo=Robot() else:
hubo.set_trace("green") hubo.move()
hubo.set_pause(0.02) while hubo.left_is_clear():
time.sleep(1) forward()
def right(): left_turn()
for i in range(3): forward()
hubo.turn_left() right_turn()
def left_turn():
hubo.turn_left() harvest2
if hubo.on_beeper(): from cs1robots import*
hubo.pick_beeper() import time
hubo.move() load_world("worlds/harvest1.wld")
hubo.turn_left() hubo=Robot()
hubo.set_trace("green")
def right_turn(): hubo.set_pause(0.02)
right() time.sleep(1)
if hubo.front_is_clear(): def right():
hubo.move() for i in range(3):
right() hubo.turn_left()

6
def left_turn(): harvest3
hubo.turn_left() from cs1robots import*
if hubo.on_beeper(): load_world("worlds/harvest2.wld")
hubo.pick_beeper() m=Robot(avenue=6,street=2)
hubo.move() def right():
hubo.turn_left() for i in range(3):
m.turn_left()
def right_turn(): def rotation():
right() if (i+1)%4==0:
if hubo.front_is_clear(): right()
hubo.move() else:
right() m.turn_left()
for i in range(1,10,2):
def forward(): for j in range(i):
while hubo.front_is_clear(): if m.on_beeper():
if hubo.on_beeper(): m.pick_beeper()
hubo.pick_beeper() m.move()
else: rotation()
hubo.move() m.move()
for i in range (3): rotation()
forward() for j in range(10):
left_turn() if m.on_beeper():
forward() m.pick_beeper()
right_turn() m.move()

7
m.pick_beeper() harvest4
right() from cs1robots import*
m.move() import time
right() load_world("worlds/harvest3.wld")
for i in range(9,1,-2): m=Robot()
for j in range(i): m.set_trace("green")
m.move() m.set_pause(0.02)
if m.on_beeper(): time.sleep(1)
m.pick_beeper() def right():
rotation() for i in range(3):
m.move() m.turn_left()
rotation()
m.move() def lp():
m.pick_beeper() m.turn_left()
right() if m.on_beeper():
for i in range(11): m.pick_beeper()
m.move() m.move()
right() m.turn_left()
for i in range(5):
m.move() def rm():
m.turn_left() right()
m.turn_left() if m.front_is_clear():
while m.carries_beepers(): m.move()
m.drop_beeper() right()

8
def pm(): def left_turn():
while m.front_is_clear(): hubo.turn_left()
if m.on_beeper(): if hubo.on_beeper():
m.pick_beeper() hubo.pick_beeper()
else: hubo.move()
m.move() hubo.turn_left()
while m.left_is_clear():
pm() def right_turn():
lp() right()
pm() if hubo.front_is_clear():
rm() hubo.move()
right()

harvest5 def forward():


from cs1robots import* while hubo.front_is_clear():
import time if hubo.on_beeper():
load_world("worlds/harvest4.wld") hubo.pick_beeper()
hubo=Robot() else:
hubo.set_trace("green") hubo.move()
hubo.set_pause(0.02) while hubo.left_is_clear():
time.sleep(1) forward()
def right(): left_turn()
for i in range(3): forward()
hubo.turn_left() right_turn()

9
hurdles1 hurdles2
from cs1robots import * from cs1robots import *
import time import time
load_world("worlds/hurdles1.wld") load_world("worlds/hurdles2.wld")
hubo=Robot() hubo=Robot()
hubo.set_pause(0.05) hubo.set_pause(0.05)
time.sleep(0.5) time.sleep(0.5)
hubo.set_trace("green") hubo.set_trace("green")

while not hubo.on_beeper(): while not hubo.on_beeper():


if hubo.right_is_clear(): if hubo.right_is_clear():
for i in range(3): for i in range(3):
hubo.turn_left() hubo.turn_left()
hubo.move() hubo.move()
elif hubo.front_is_clear(): elif hubo.front_is_clear():
hubo.move() hubo.move()
else: else:
hubo.turn_left() hubo.turn_left()

hubo.pick_beeper() hubo.pick_beeper()

10
hurdles3 hurdles4
from cs1robots import * from cs1robots import *
import time import time
load_world("worlds/hurdles3.wld") load_world("worlds/hurdles4.wld")
hubo=Robot() hubo=Robot()
hubo.set_pause(0.05) hubo.set_pause(0.05)
time.sleep(0.5) time.sleep(0.5)
hubo.set_trace("green") hubo.set_trace("green")

while not hubo.on_beeper(): while not hubo.on_beeper():


if hubo.right_is_clear(): if hubo.right_is_clear():
for i in range(3): for i in range(3):
hubo.turn_left() hubo.turn_left()
hubo.move() hubo.move()
elif hubo.front_is_clear(): elif hubo.front_is_clear():
hubo.move() hubo.move()
else: else:
hubo.turn_left() hubo.turn_left()

hubo.pick_beeper() hubo.pick_beeper()

11
maze1 hubo.turn_left()
from cs1robots import * for i in range(2):
import time hubo.move()
load_world("worlds/maze1.wld") for i in range(3):
hubo=Robot() hubo.turn_left()
hubo.set_pause(0.05) for i in range(9):
time.sleep(0.5) hubo.move()
hubo.set_trace("green") if hubo.on_beeper():
while not hubo.on_beeper(): hubo.pick_beeper()
if hubo.right_is_clear(): for i in range(3):
for i in range(3): hubo.turn_left()
hubo.turn_left() hubo.move()
hubo.move() for i in range(3):
elif hubo.front_is_clear(): hubo.turn_left()
hubo.move() for i in range(9):
else: hubo.move()
hubo.turn_left() if hubo.on_beeper():
hubo.pick_beeper() hubo.pick_beeper()
hubo.turn_left()
myworld hubo.move()
from cs1robots import * hubo.turn_left()
load_world("worlds/myworld.wld") while hubo.carries_beepers():
hubo=Robot() hubo.drop_beeper()
hubo.set_pause(0.05)

12
newspaper hubo.move()
from cs1robots import * for i in range(4):
load_world("worlds/newspaper.wld") up()
hubo=Robot(beepers=1) hubo.drop_beeper()
hubo.set_trace("red") round()
hubo.set_pause(0.02) hubo.move()
def right(): for i in range(4):
for i in range(3): down()
hubo.turn_left() round()
def up():
hubo.turn_left() plant1
hubo.move() from cs1robots import*
right() create_world(avenues=7,streets=7)
hubo.move() hubo=Robot(beepers=36)
hubo.move() hubo.set_trace("green")
def round(): hubo.set_pause(0.02)
for i in range(2): def right():
hubo.turn_left() for i in range(3):
def down(): hubo.turn_left()
hubo.move() def left_turn():
hubo.turn_left() hubo.drop_beeper()
hubo.move() hubo.turn_left()
right() hubo.move()
hubo.move() hubo.turn_left()

13
def right_turn(): def right():
for i in range(2): for i in range(3):
right() hubo.turn_left()
hubo.move() def left_turn():
def forward(): if not hubo.on_beeper():
while hubo.front_is_clear(): hubo.drop_beeper()
hubo.drop_beeper() hubo.turn_left()
hubo.move() hubo.move()
hubo.move() hubo.turn_left()
while hubo.left_is_clear(): def right_turn():
forward() for i in range(2):
left_turn() right()
forward() hubo.move()
right_turn() def forward():
while hubo.front_is_clear():
if not hubo.on_beeper():
plant2 hubo.drop_beeper()
from cs1robots import* hubo.move()
import time hubo.move()
load_world("worlds/harvest3.wld") while hubo.left_is_clear():
hubo=Robot(beepers=6) forward()
hubo.set_trace("green") left_turn()
hubo.set_pause(0.02) forward()
time.sleep(1) right_turn()

14
rain1 def right():
from cs1robots import * for i in range(3):
load_world("worlds/rain1.wld") hubo.turn_left()
hubo=Robot(beepers=8 , avenue=3 , hubo.drop_beeper()
street=6 , orientation="S") hubo.move()
hubo.set_trace("red")
for i in range(7):
hubo.set_pause(0.02)
if hubo.right_is_clear():
hubo.drop_beeper() hubo.drop_beeper()
hubo.move() hubo.move()
while not hubo.on_beeper():
elif hubo.front_is_clear():
if hubo.right_is_clear():
hubo.move()
hubo.drop_beeper() else:
hubo.move()
hubo.turn_left()
elif hubo.front_is_clear():
hubo.move()
hubo.move() right()
else:
hubo.move()
hubo.turn_left()
while not hubo.on_beeper():
if hubo.right_is_clear():
rain2
hubo.drop_beeper()
from cs1robots import *
hubo.move()
load_world("worlds/rain2.wld") elif hubo.front_is_clear():
hubo=Robot(beepers=8 , avenue=3 ,
hubo.move()
street=6 , orientation="S")
else:
hubo.set_trace("red")
hubo.turn_left()
hubo.set_pause(0.02)

15
sort1 trash1
from cs1robots import * from cs1robots import *
load_world("worlds/sort1.wld") load_world("worlds/trash1.wld")
hubo=Robot(orientation="N") hubo=Robot()
hubo.set_trace("red") def right():
hubo.set_pause(0.02) for i in range(3):
def right(): hubo.turn_left()
for i in range(3): def round():
hubo.turn_left() for i in range(2):
hubo.pick_beeper() hubo.turn_left()
for i in range(6): while hubo.front_is_clear():
hubo.move() hubo.move()
hubo.pick_beeper() while hubo.on_beeper():
right() hubo.pick_beeper()
hubo.move() round()
right() while hubo.front_is_clear():
hubo.move() hubo.move()
for i in range(5): right()
hubo.move() hubo.move()
hubo.pick_beeper() while hubo.carries_beepers():
right() hubo.drop_beeper()
hubo.move() round()
hubo.turn_left() hubo.move()
hubo.turn_left() hubo.turn_left()

16
trash2 trash3
from cs1robots import * from cs1robots import *
load_world("worlds/trash2.wld") load_world("worlds/trash3.wld")
hubo=Robot() hubo=Robot()
def right(): hubo.set_pause(0.02)
for i in range(3): def right():
hubo.turn_left() for i in range(3):
def round(): hubo.turn_left()
for i in range(2): def rotation():
hubo.turn_left() if i%2==1:
while hubo.front_is_clear(): right()
hubo.move() else:
while hubo.on_beeper(): hubo.turn_left()
hubo.pick_beeper() def round():
round() for i in range(2):
while hubo.front_is_clear(): hubo.turn_left()
hubo.move()
right() for i in range(10):
hubo.move() for j in range(9):
while hubo.carries_beepers(): while hubo.on_beeper():
hubo.drop_beeper() hubo.pick_beeper()
round() hubo.move()
hubo.move() while hubo.on_beeper():
hubo.turn_left() hubo.pick_beeper()

17
if i<9: def round():

rotation() for i in range(2):

hubo.move() hubo.turn_left()

rotation() for i in range(11):

else: for j in range(9):


while hubo.on_beeper():
hubo.turn_left()
hubo.pick_beeper()
for i in range (9):
hubo.move()
hubo.move()
while hubo.on_beeper():
hubo.turn_left()
hubo.pick_beeper()
while hubo.carries_beepers():
if i<10:
hubo.drop_beeper()
rotation()
hubo.move()
trash4
rotation()
from cs1robots import *
else:
load_world("worlds/trash4.wld")
right()
hubo=Robot() for i in range (10):
def right(): hubo.move()
for i in range(3): right()
hubo.turn_left() for i in range (9):
def rotation(): hubo.move()
if i%2==1: hubo.turn_left()
right() hubo.turn_left()

else: while hubo.carries_beepers():

hubo.turn_left() hubo.drop_beeper()

18
wrong def rotation():

from cs1robots import * if i%2==1:


right()
load_world("worlds/wrong.wld")
else:
hubo=Robot()
hubo.turn_left()
hubo.set_trace("red")
def round():
hubo.set_pause(0.05)
for i in range(2):
while not hubo.on_beeper():
hubo.turn_left()
if hubo.right_is_clear(): for i in range(10):
for i in range(3): for j in range(9):
hubo.turn_left() while hubo.on_beeper():

hubo.move() hubo.pick_beeper()

elif hubo.front_is_clear(): hubo.move()

hubo.move() while hubo.on_beeper():


hubo.pick_beeper()
else:
if i<9:
hubo.turn_left()
rotation()
hubo.pick_beeper()
hubo.move()
rotation()
yardwork else:
from cs1robots import * hubo.turn_left()
load_world("worlds/yardwork.wld") for i in range (9):
hubo=Robot() hubo.move()

def right(): hubo.turn_left()

for i in range(3): while hubo.carries_beepers():


hubo.drop_beeper()
hubo.turn_left()

19
zigzag1 a=random.randint(2,20)
from cs1robots import * b=random.randint(2,20)
create_world() create_world(avenues=a,streets=b)
hubo=Robot() hubo=Robot()
hubo.set_trace("green") def right():
def right(): for i in range(3):
for i in range(3): hubo.turn_left()
hubo.turn_left() def move():
def move(): for j in range(b-1):
for j in range(9): hubo.move()
hubo.move() for i in range(a/2):
for i in range(5): hubo.turn_left()
hubo.turn_left() move()
move() right()
right() hubo.move()
hubo.move() right()
right() move()
move() if i<a/2-1:
if i<4: hubo.turn_left()
hubo.turn_left() hubo.move()
hubo.move() if a%2==1:
hubo.turn_left()
zigzag2 hubo.move()
from cs1robots import * hubo.turn_left()
import random move()

20
21

You might also like