SIT22001 PROGRAMMING I
Lecture 2
Fall 2024
School of Global Entrepreneurship & ICT
Handong Global University
SIT22001 – Lecture 2 Handong Global University 1
2D ROBOT CONTROL
Read Sections 5~9 to do the following tasks:
Zigzag1
Hurdles1
Newspaper delivery
Harvest1
Hurdles2
Harvest2
Harvest3
SIT22001 – Lecture 2 Handong Global University 2
INTERACTIVE MODE (1/2)
>>>from cs1robots import *
>>>create_world()
SIT22001 – Lecture 2 Handong Global University 3
INTERACTIVE MODE (2/2)
>>>hubo = Robot() >>>[Link]() >>>hubo.turn_left()
SIT22001 – Lecture 2 Handong Global University 4
SCRIPT MODE
from cs1robots import *
create_world()
hubo = Robot()
[Link]()
hubo.turn_left()
[Link]()
hubo.turn_left()
[Link]() Why ?
hubo.turn_left()
[Link]()
hubo.turn_left()
SIT22001 – Lecture 2 Handong Global University 5
TRACE & PAUSE
from cs1robots import *
create_world()
hubo = Robot()
hubo.set_trace(“blue”)
hubo.set_pause(1)
[Link]()
hubo.turn_left()
[Link]()
hubo.turn_left()
[Link]()
hubo.turn_left()
[Link]()
hubo.turn_left()
SIT22001 – Lecture 2 Handong Global University 6
BUGS
[Link]() Syntax error !
from cs1robots import *
create_world()
hubo = Robot()
[Link]()
hubo.turn_left() Runtime error !
hubo.turn_left() What’s wrong ?
hubo.turn_left()
[Link]()
SIT22001 – Lecture 2 Handong Global University 7
COMMENTS
Used for humans (to understand a program)
-To embed programmer-readable annotations.
-To make the source code easier to understand.
Starting with #
Ignored by the Python interpreter:
# My first program
from cs1robots import *
create_world()
# This line should be ignored!
SIT22001 – Lecture 2 Handong Global University 8
TURNING RIGHT (1/3)
1
3
4
0
SIT22001 – Lecture 2 Handong Global University 9
TURNING RIGHT (2/3)
# initialize the world # move and turn right
from cs1robots import * [Link]()
create_world() hubo.turn_left()
hubo = Robot() hubo.turn_left() turn right
# turn left hubo.turn_left()
hubo.turn_left() Why? # move and turn right
# move and turn right [Link]()
[Link]() hubo.turn_left()
hubo.turn_left() hubo.turn_left() turn right
hubo.turn_left() turn right hubo.turn_left()
hubo.turn_left() [Link]()
SIT22001 – Lecture 2 Handong Global University 10
TURNING RIGHT (3/3)
from cs1robots import * # move and turn right
create_world() [Link]()
hubo = Robot() turn_right()
def turn_right():
hubo.turn_left() # move and turn right
hubo.turn_left() [Link]()
hubo.turn_left() turn_right()
hubo.turn_left() [Link]()
# move and turn right
[Link]()
turn_right()
SIT22001 – Lecture 2 Handong Global University 11
PRACTICE USING FUNCTIONS 1
PROBLEM 1: ZIGZAG1
Write a program [Link] that makes your robot visit
the entire world in a zigzag fashion.
Analyze the zigzag path and
use the functions to make
your program as compact as
possible.
SIT22001 – Lecture 2 Handong Global University 12
PRACTICE USING FUNCTIONS 2
PROBLEM2: HURDLES1
Hubo has entered a hurdles race with obstacles as in the
world file hurdles1. Write a program that makes Hubo
follow the path indicated below, pick up a beeper at the
grid point (10,1) and get back to the starting grid point
(1,1) and recover the original orientation.
: the starting grid point
Again, make your program as compact as possible using
functions.
SIT22001 – Lecture 2 Handong Global University 13
HOW TO LOAD HURDLES1
from cs1robots import *
load_world("worlds/[Link]") #but not create_world()
hubo = Robot()
directory hurdle1 file
Create a directory worlds and download all world files.
SIT22001 – Lecture 2 Handong Global University 14
DROPING A BEEPER
from cs1robots import *
load_world("worlds/[Link]")
hubo = Robot(beepers = 1)
hubo.set_trace("blue")
[Link]()
[Link]()
hubo.drop_beeper()
[Link]()
[Link]()
SIT22001 – Lecture 2 Handong Global University 15
PICKING A BEEPER
from cs1robots import *
load_world("worlds/[Link]")
hubo = Robot()
hubo.set_trace("blue")
[Link]()
[Link]()
hubo.pick_beeper()
[Link]()
[Link]()
SIT22001 – Lecture 2 Handong Global University 16
PRACTICE USING FUNCTIONS 2
SIT22001 – Lecture 2 Handong Global University 17
PRACTICE USING FUNCTIONS 3
PROBLEM 3: NEWSPAPER DELIVERY
Hubo delivers newspapers in his local neighborhood. Make
him climb up the stairs to the front door of a house, drop the
newspaper (represented by a beeper) on the top step, and
return to his starting point as illustrated below. The name of
the world file is [Link].
Use top-down design: As already explained in the previous
lecture, decompose the problem into sub-problems and then
focus on each sub-problem one by one.
SIT22001 – Lecture 2 Handong Global University 18
PRACTICE USING FUNCTIONS 3
SIT22001 – Lecture 2 Handong Global University 19
PRACTICE USING FUNCTIONS 4
PROBLEM 4: HARVEST1
It's harvest time! Make the robot pick up all the carrots
(represented by beepers) in this garden. The world file is
[Link]. Employ top-down design. Use for-loops.
SIT22001 – Lecture 2 Handong Global University 20
PRACTICE USING FUNCTIONS 4
SIT22001 – Lecture 2 Handong Global University 21