CSE 111 University at Albany Robot Simulator Worksheet

CSE 111Fall 2019
Project #4
Due: Sunday, October 20, at 11:59 PM
Task #1
Your task is to program the robot to calculate the area of the rectangular room inside which the
robot starts and then exit the room. The exit will always be at the southeast corner of the east
wall. The robot may start anywhere inside the room, facing any direction. When the robot exits
the room, it should print the area of the room. The size of the room may vary, but it will never
be smaller than 2×2. Three sample initial worlds are shown below. The green dot shows where
the robot should end up when the program ends. It is important to note, that when your program
is graded, the initial world used by the grader may be different than those shown below, and
different than the one(s) that you used to test your program. Hence, it is important that your
program works on all initial worlds that meet the specifications above. Your program (p file)
should be named project_4_task_1_username.p, where username is your UBIT username.
Submit your program on UB Learns on or before the due date.
6
5
4
3
2
1
0
6
5
4
3
2
1
0
0 1 2 3 4 5 6
6
5
4
3
2
1
0
0 1 2 3 4 5 6
0 1 2 3 4 5 6
Task #2
Your task is to program the robot to determine which of the rectangular rooms in its world has
the largest area. The westmost room is room #1. Rooms continue to increase in number as you
move to the east. The eastmost room has no exit. The pathway between adjacent rooms is
always at the bottom of the wall separating the two rooms. In the example shown below, room
#2 has the largest area. There can be any number of rooms. Each room may be any size, with a
minimum size of 2×2. The robot should print to the console which room has the greatest area.
You may assume that there will not be a tie between rooms that have the maximum area. The
robot can start anywhere, in any room, facing any direction. It does not matter where the robot is
when the program ends It is important to note, that when your program is graded, the initial
world used by the grader may be different than what is shown below, and different than the
one(s) that you used to test your program. Hence, it is important that your program works on all
initial worlds that meet the specifications above. Your program (p file) should be named
project_4_task_2_username.p, where username is your UBIT username. Submit your program
on UB Learns on or before the due date.
12
11
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12
CSE 111
Robot Simulator
Creating New Instructions
The Problem
Wouldn’t it be nice if the robot could turn right?
Defining new instructions allow for the robot to implement complex tasks with a
single instruction
Defining New Instructions
Why would we want to define new instructions?
Smaller programs
Programs are easier to read
Resusability
More efficient program design
Write and debug new instruction once
Use multiple times
Example
turnright
Not a primitive instruction
Can be defined as three (3) turnleft instructions
Format
def new_instruction:
BEGIN
Instructions
END
Block Structuring
Instructions are placed between the delimiters
BEGIN
END
Rules for Naming New Instructions
Letters, numbers, underscores(_) may be used
Reserved words may NOT be used
Reserved Words
def
BEGIN
END
Increasing Readability
Use lowercase when naming single word new instructions
Use underscores or uppercase letters to separate words if multiple words are
used
Example
AdvanceForward
Navigate_Maze
How does the simulator know where your program starts?
Program must start with main
Define an instruction called main
Without defining main and calling it, your program will not work properly!
Note if there are NO user-defined instructions defined, then main is not needed
You’ve seen this in previous projects!
Sample Program
General Structure
def new_instruction:
BEGIN
Instructions
END
Note, more new instructions may be defined here…
def main:
BEGIN
Instructions
END
main
Example
def turnright:
BEGIN
turnleft
turnleft
turnleft
END
def main:
BEGIN
move
turnright
move
move
turnleft
END
main
Debugging New Instructions
Debug each new instruction carefully
Verification is important
Once a new instruction is verified, it can be used repeatedly with confidence
Remember
The robot understands what your program says, NOT what you mean!!!
CSE 111
Robot Simulator
Mac Usage
Editing & Running
Open Finder
Select Utilities
Open Terminal
Change directories (folders) to where you’d like to work
You are starting in your home directory
Command
cd directory
where directory is the name of the directory
Example
cd cse-111
If the directory had not yet been created, you can create it using Finder
or, in Terminal, use the command mkdir cse-111
Editing Your Program
Use the Editor pico
Type pico
Type your program
Commands are at the bottom of the screen
Save
Control O
Open
Control R
Exit
Control X
Running Your Program
Type python RobotSim.py
CSE 111
Robot Simulator
Decision Making
Decision Making
The ability to make decisions significantly increases the capability of the robot to
perform tasks
How is this accomplished?
Conditional Statements
IF THEN
If Then Instruction
Format
if condition:
BEGIN
Instructions
END
How does it work?
If condition is met, then perform instruction(s) in THEN clause
THEN clause are the instructions after the colon (:)
Block Structuring
Instructions are placed between the delimiters BEGIN & END
Reserved Words
if
BEGIN
END
Test Conditions
Is the robot next to a wall?
front_is_clear
front_is_blocked
left_is_clear
left_is_blocked
right_is_clear
right_is_blocked
What direction is the robot facing?
facing_north
facing_south
facing_east
facing_west
Conditions are also reserved words
front_is_clear
front_is_blocked
right_is_clear
right_is_blocked
left_is_clear
left_is_blocked
facing_north
facing_south
facing_west
facing_east
Example
if front_is_clear:
BEGIN
move
END
Nested If Then Instructions
The instructions in the execution block an If Then instruction can be more if then
instructions
Example
Format
if left_is_clear:
BEGIN
if right_is_clear:
BEGIN
move
END
END
Project #3
Task #1
Learn How to Use the IF/THEN Instruction
When the robot is in a square room, move to the northwest corner of the room
and face north
The initial position and direction the robot is facing is unknown
The size of one side of the room will be the same as the number of characters
in your username.
CSE 111
Robot Simulator
Robot Simulator Demonstration
The Robot’s World
What is the world?
The environment in which the robot operates in
What does the world consist of?
Cartesian Coordinate System
x,y
The robot can occupy any space in the first quadrant
x and y must be greater than or equal to zero
x,y ≥ 0
The Robot can navigate at any coordinate where x and y are both integers
Walls
Walls are halfway between the locations that the robot can be on
The robot can NOT go through a wall.
If the robot tries to go through a wall, the program will end in an
error
The locations of the walls are determined by the world specification file
World is bounded
Robot cannot move beyond the boundaries of the world
World is square
The size of the square is determined by the world specification file
The Robot’s Capabilities
Navigating the World
The robot can move & turnleft
Navigation Details
move
Moves forward one space on the grid
turnleft
Rotate 90º
Specifying the Initial World
World Specification File
Specifies the World
Text File
File must end in a .w extension
Use any editor you’d like
Be careful!
Some editors like to add their own extension, such as .txt
Format
First Line
Starting Position
Direction Robot is Facing
north
south
east
west
Size of World
Must be an integer
Note the Order of these three parameters matters
Example
3 4 west 8
Robot starts at position 3,4 (x=3, y=4), facing west
Maximum x and y coordinates in the world are 8
Subsequent Lines
Determine Wall Locations
Format
X Coordinate Y Coordinate Vertical or Horizontal
Each parameter separated by spaces
x,y Determines South West Starting Point of Wall
Wall Extends One Unit North or East
Vertical or Horizontal Orientation Specified by v or h
Example
3.5 2.5 v
3.5 3.5 v
3.5 4.5 v
4 5.5.5 h
The x and y coordinate should always contain .5
Example
3 5 east 8
3.5 2.5 v
3.5 3.5 v
3.5 4.5 v
4.5 3.5 h
The Program
A program is a collection of instructions
The instructions you know thus far are:
move
turnleft
Instructions are case sensitive!
Specifying the Program
Text File
File must end in a .p extension
Use any editor you’d like
Be careful!
Some editors like to add their own extension, such as .txt
Project #2
Task #1
Test Your First Program
Test the program you did in Project #1
Maze can be downloaded from the course website
Project #2
Task #2
Learn How to Specify an Initial World
World of will spell out the first letter of your username with walls
Robot should start at 3,7 facing west
Size
Maximum x,y value in world should be 10
CSE 111
Robot Simulator
Loops
Loops
Repeating a sequence of instructions
Unconditional
Repeat a set number of times
REPEAT
Conditional
Repeat while a condition is true
WHILE
Unconditional Loops
Format
repeat x times:
BEGIN
Body of loop (sequence of instructions)
END
How does it work?
Repeat the body of the loop x times
New Reserved Words
repeat
times
Unconditional Loop Example
Let’s rewrite the turnright instruction
Original Definition
def turnright:
BEGIN
turnleft
turnleft
turnleft
END
New Definition
def turnright:
BEGIN
repeat 3 times:
BEGIN
turnleft
END
END
Conditional Loops
Format
while condition:
BEGIN
Body of loop (sequence of instructions)
END
How does it work?
Repeat the body of the loop while the given condition is met
Conditions are the same as the conditions for If Then statements
New Reserved Words
while
Condition is checked BEFORE loop body is executed
It is possible that the loop body may never be executed
Constructing WHILE loops
Identify condition for which loop should terminate
Negate termination condition for loop condition
Determine what must be done before and after the loop
Perform minimal number of instructions in the loop
Conditional Loop Example
Task
Move until the robot gets to a wall or the world’s boundary
Initial World
5
4
3
2
1
0
0 1 2 3 4 5
Code
while front_is_clear:
BEGIN
move
END
Final World
5
4
3
2
1
0
0 1 2 3 4 5
What happens if the initial world is as shown below?
CSE 111
Robot Simulator
Memory
The Problem
Wouldn’t it be nice if the robot could remember things?
Where it has been?
How many positions it has moved?
What it has seen during its journey?
The Solution
The Concept of Memory
The robot can remember things
A variable is the key to accessing memory
What can a variable store?
Text
aka, string
Integers
How is data stored?
Assignment
Format
Storing Integers
VariableName = Value
Storing Text
VariableName = “Value“
Naming Variables
Variable Names May Consist Of:
Alphanumeric Characters
Letters, Numbers
Underscores
Variable Names May NOT Be:
Reserved Words
Examples:
Count
x
i
Steps
advance_3_spaces
positions_moved
How can data be viewed?
Displaying the Contents of Memory
print
Format
print VariableName
Additional print Command Functionality
A message can be printed
Format
print “Message”
where Message is what you’d like to print to the screen
Example
print “Hello”
Performing Calculations on Stored Data
The robot can add, subtract, and multiply
Addition
Operator: +
Format
VariableName = VariableName + Integer
VariableName = VariableName + VariableName
Example
distance = 0
if front_is_clear:
BEGIN
move
distance = distance + 1
END
print distance
Subtraction
Operator: Format
VariableName = VariableName – Integer
VariableName = VariableName – VariableName
Multiplication
Operator: *
Format
VariableName = VariableName * Integer
VariableName = VariableName * VariableName
Comparisons Using Variables
Used in
If Then Statements
Conditions
Equal (==)
Not Equal (!=)
Less Than ()
Example #1
if passed_a_wall == “yes”:
BEGIN
turnleft
move
END
Example #2
if number_of_turns

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER