Solution Exercises Project Day 1
Solution Exercises Project Day 1
Get the owl to the target! The allowed commands are move, turn left and turn right.
Solution
turn right
move
turn right
move
move
turn right
move
turn right
move
turn left
move
turn left
move
move
turn left
move
move
move
This maze is very simple, you only have to go straight ahead. Solve the maze with only one move command! The
jump command can be helpful for this.
Solution
turn right
start:
move
jump start
In this exercise the maze is slightly different. Once the owl has hit a wall, only one step to the right is missing.
Extend the solution of the previous task! The commands explore and branch can be helpful here.
1
Solution
var int isWalkable
turn right
jump condition
loop:
move
condition:
explore isWalkable front
branch isWalkable loop
turn right
move
In this maze, the goal can be reached by going straight ahead to the next wall, then turning right and repeating these
two steps until the goal is reached. First, write a program in which the owl walks straight ahead until it reaches the
next wall. Use this program as a subroutine to solve the maze. You can simply create such a subprogram by adding
a LabelDefinition (i.e. straightToWall:) beforehand, in order to jump to it.
Solution
start:
turn right
jump straightToWall
straightToWall:
var int isWalkable
jump condition
loop:
move
condition:
explore isWalkable front
branch isWalkable loop
jump start
Find your way out of the maze with the given subprogram guess! Whenever you can no longer walk straight,
jump to the label guess. After execution, guess writes 0 or 1 in the variable result and jumps back to the label
return. If you get the number 0 you have to turn left. If the result is 1, you have to turn right to get to the goal. The
functionality of guess is not relevant here.
Solution
var int t
var int result
loop:
explore t front
branch t go
jump guess
return:
branch result r
turn left
jump loop
2
r:
turn right
jump loop
go:
move
jump loop
guess:
var int zero
set zero 0
var direction d
get_direction d
var int b
test equal b d east
branch b e
set result 0
jump return
e:
set result 1
jump return
Create and update a counter that counts the number of left turns against the number of right turns. Whenever you
can turn left and right, choose the direction that (according to the counter) has been chosen less often. Otherwise,
go straight or turn in the only free direction. If there is a tie, go right first. You can ssume that the map for this task
does not contain any dead ends.
Solution
var int counter
set counter 0
var int t
var int t2
var int comp
con:
explore t front
branch t walk
explore t left
explore t2 right
test equal comp t t2
branch comp ask_the_counter
branch t go_left
turn right
sub counter counter 1
jump con
go_left:
turn left
add counter counter 1
jump con
ask_the_counter:
test less t counter 0
branch t L
turn right
sub counter counter 1
jump con
L:
turn left
3
add counter counter 1
jump con
walk:
move
jump con
Now you are going to implement your first maze solving algorithm, which is called right-hand rule. The main idea
is that our owl, Fridolin, is following the wall to his right (so, in fact, we could also call it right-wing rule) until he
reaches the end of the maze. To make the implementation easier, Fridolin provided some pseudocode for you:
While you did not arrive at the goal location:
If you don’t have a wall to your right:
Move right.
Else:
If you don’t have a wall in front of you:
Move straight.
Else:
Turn left.
1. Implement the algorithm and lead the owl to the goal.
2. This algorithm does not always lead the owl to its goal. Imagine a scenario where this is the case and think
about a solution for this problem. Afterwards you can extend the implementation by your solution.
Solution
1. var int isWalkableRight
var int isWalkableFront
next_move:
explore isWalkableRight right
branch isWalkableRight right_free
jump right_blocked
right_free:
turn right
move
jump next_move
right_blocked:
#Since we want to follow the wall we need to check if we can move forward
explore isWalkableFront front
branch isWalkableFront front_free
jump front_blocked
front_free:
move
jump next_move
front_blocked:
turn left
jump next_move
#For performance jump right_blocked would be better!
2. The right-hand rule will not work when you start within the maze and if you encounter an obstacle which is
not connected to the border of the maze. In this case, you will be trapped in a circle. So, one solution could
be to check whether you are trapped and if so, to break free by going straight to the next wall which you can
follow. Implementation: See Pledge Algorithm on Sheet 2.
4
Task 1.7: Bonus: Brute Force
Problems can be solved theoretically by trying all possibilities. This procedure is called brute force.
This task aims to solve the maze with a special strategy, which is successful in many mazes: Before each step,
check the markings on the current square. If all directions are marked, stop. Otherwise, turn the owl in a direction
that is not yet marked, mark it and take one step forward. Take the direction you first used to get to the field only
after you tried all other directions.
Hint: For solving this exercise, marks are highly recommended. Formally they will be introduced on day 3, but
you can have a look at the DSL Specification (CMS: Materials) for further information about marks.
Solution
start:
var int marking
get_mark marking
var int noPathUsed
test equal noPathUsed marking 0
branch noPathUsed walkLeft
jump checkMark
walkLeft:
mark left
var int leftWalkable
explore leftWalkable left
var int notLeftWalkable
test equal notLeftWalkable leftWalkable 0
branch notLeftWalkable start
turn left
move
jump start
checkMark:
var int westUsed
var int northUsed
var int eastUsed
var int southUsed
var direction dir
var int turningFinished
5
branch eastUsed walkSouth
test equal eastUsed marking 13
branch eastUsed walkSouth
test equal northUsed marking 14
branch northUsed walkEast
jump stop
walkNorth:
get_direction dir
test equal turningFinished dir east
branch turningFinished walkLeft
turn left
jump walkNorth
walkEast:
get_direction dir
test equal turningFinished dir south
branch turningFinished walkLeft
turn left
jump walkEast
walkSouth:
get_direction dir
test equal turningFinished dir west
branch turningFinished walkLeft
turn left
jump walkSouth
walkWest:
get_direction dir
test equal turningFinished dir north
branch turningFinished walkLeft
turn left
jump walkWest
stop: