CSEg 1101 Introduction to Computing
Lecture 6
OUTLINE ASTU
• More practice through 2D robot control
• Conditional + while-loop
• Color conversion
ASTU
MORE PRACTICE(through 2D robot control)
PROBLEM : RAIN1*
It was a beautifully sunny day. Hubo is playing outside with
friends. Suddenly, it started to rain. He remembered that the
windows in his house were all open. So he went back to the
house and stopped in front of the door. Help Hubo close the
windows of the house. A closed window has a beeper in
front of it.
ASTU
Starting at position (2,6) and facing east.
Dropping a beeper in order to close a window
Use rain1.wld as the world file.
ASTU
Pseudo code
1. Mark the starting point.
2. Move forward to the east.
2. While not returning to the starting point:
If there is a window, close it. How?
If the front is clear, move forward.
Otherwise turn right left.
3. Unmark the starting point and turn to the east
ASTU
PROBLEM : RAIN2*
Ami, Hubo's friend, lives in a bigger house. Ami was playing
outside with Hubo when it started raining. Help Ami close
the windows in her house.
How to check a window ?
ASTU
How to find a window
if right is clear:
if front is clear,
then move forward.
if right is blocked, then :
Move backward.
close a window.
ASTU
Start at position (2,6) with facing east.
Use rain2.wld as the world file.
Your program must work for both rain1.wld and rain2.wld
ASTU
PROBLEM : TRASH1*
The wind blew really hard last night. There is litter every-
where outside Hubo's house. His parents asked him to go
and clean up the driveway, as well as the path leading to
the curb. They both are in a straight line, with garbage at
random places as illustrated below(left). Hubo should col-
lect all the litter, and put it in the garbage can, situated
north of his starting point. The final situation should look
like the following(right):
Pseudo code
1. While front is clear:
Move forward.
collect all litter. How?
2. Turn around.
3. Move back to the starting point.
4. Go to the north and put all litter in the garbage can.
5 Move back to the starting point.
How?
How to put all litter in the trash can
while hubo.carries_beepers():
hubo.dorp_beeper()
To collect all litter at a position, please refer to your pro-
gram for solving Harvest5.
Use trash1.wld as the world.
PROBLEM: TRASH2
Hubo's parents are so proud of
his work that they ask him to
pick up all the garbage that got
blown away in their backyard
during the windstorm. Have him
pick up all the garbage and
bring it back with him to his
starting position. Create your
own world file, corresponding
to situation
Notice that thelike theofone
location any illus-
beeper is not given in advance.
trated
The sizeinofthe left figure.
the backyard is not given, either.
HOW TO CLEAN THE BACKYARD
To create a backyard with litter,
use edit_world in Section 7.
PROBLEM: Return
Write a program that will allow Hubo to return to his usual
starting position and orientation (avenue 1, street 1, facing
east), starting from any position and orientation in an
empty world. You can create a robot with a given position
and orientation like this:
hubo = Robot(orientation =“E”, avenue =7, street =5)
“E”, “W”, “S”, “N”
Initial Position
(orientation, avenue, street) (orientation, avenue, street)
“E” 7 5 “E” 1 1
COLOR COVERSION
PROBLEM 18: THREE_COLOR POSTER*
In the previous lecture, you learned a program that convert a
color image to a black-and-white image. Modify that pro-
gram to convert a color image to
a three-color poster.
Use yuna.jpg as the color image.
How to convert pixels
bright pixels yellow
dark pixels blue
neither dark nor dark green.
Three-color
color photo
poster
from cs1media import *
threshold = 100 Modify here
white = (255, 255, 255)
black = (0, 0, 0)
img = load_picture(“./images/yuna.jpg")
w, h = img.size()
for y in range(h):
for x in range(w):
r, g, b = img.get(x, y)
v = (r + g + b) / 3.0
if v > threshold:
img.set(x, y, white) Modify here.
else:
img.set(x, y, black)
img.show()
Result with kara.jpg
Result with 2PM.jpg