<titledata-rh="true">Recursion and backtracking with Robot Karel | mf</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://blog.mfocko.xyz/algorithms/recursion/karel/"><metadata-rh="true"property="og:locale"content="en"><metadata-rh="true"name="docusaurus_locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docusaurus_version"content="current"><metadata-rh="true"name="docusaurus_tag"content="docs-algorithms-current"><metadata-rh="true"name="docsearch:version"content="current"><metadata-rh="true"name="docsearch:docusaurus_tag"content="docs-algorithms-current"><metadata-rh="true"property="og:title"content="Recursion and backtracking with Robot Karel | mf"><metadata-rh="true"name="description"content="Aproblemwithtoomanyrestrictions.
<h2class="anchor anchorWithStickyNavbar_LWe7"id="introduction">Introduction<ahref="#introduction"class="hash-link"aria-label="Direct link to Introduction"title="Direct link to Introduction"></a></h2>
<p>In this exercise we will be working with a Robot Karel and with a »very« limited
resources. The point of this exercise is to show how powerful recursion and
backtracking can be even without anything else at your hand.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="your-environment-and-problem-description">Your environment and problem description<ahref="#your-environment-and-problem-description"class="hash-link"aria-label="Direct link to Your environment and problem description"title="Direct link to Your environment and problem description"></a></h2>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="environment">Environment<ahref="#environment"class="hash-link"aria-label="Direct link to Environment"title="Direct link to Environment"></a></h3>
<p>You are given a robot that is present in a maze and is looking for an exit. Maze
consists of different walls and exit is marked with a single so-called “beeper”.</p>
<p>Walking into a wall results in a permanent damage of the robot.</p>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="interface">Interface<ahref="#interface"class="hash-link"aria-label="Direct link to Interface"title="Direct link to Interface"></a></h3>
<p>You can control the robot using the following interface:</p>
<ul>
<li>actions — you can use them to change the current state of the robot and its
surroundings<!---->
<ul>
<li><code>robot.step()</code> — moves robot one step further</li>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="problem">Problem<ahref="#problem"class="hash-link"aria-label="Direct link to Problem"title="Direct link to Problem"></a></h3>
<p>Your task is to decide whether there is an exit from the maze or not. You can see
<p><imgdecoding="async"loading="lazy"alt="Image of the maze"src="/assets/images/maze-a374d908bc9445061e15faeddc71641e.png"width="770"height="839"class="img_ev3q"></p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="simple-problem-to-get-familiar-with-the-robot">Simple problem to get familiar with the robot<ahref="#simple-problem-to-get-familiar-with-the-robot"class="hash-link"aria-label="Direct link to Simple problem to get familiar with the robot"title="Direct link to Simple problem to get familiar with the robot"></a></h2>
<p>If you feel completely lost after the previous description, let me start you off
with a simpler problem.</p>
<p>You are standing in front of the stairs, your task is to walk up the stairs.</p>
<p><imgdecoding="async"loading="lazy"alt="Image of the stairs"src="/assets/images/stairs-5ee5d03905645aeb13eeaa7774451a64.png"width="1058"height="1161"class="img_ev3q"></p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="brainstorm-the-idea">Brainstorm the idea<ahref="#brainstorm-the-idea"class="hash-link"aria-label="Direct link to Brainstorm the idea"title="Direct link to Brainstorm the idea"></a></h2>
<p>As a first step write down any ideas and things that you have noticed or came to
<h2class="anchor anchorWithStickyNavbar_LWe7"id="rough-pseudocode">»Rough« pseudocode<ahref="#rough-pseudocode"class="hash-link"aria-label="Direct link to »Rough« pseudocode"title="Direct link to »Rough« pseudocode"></a></h2>
<p>As a next step write a <strong>mock up</strong> of a pseudocode solving the problem, you are
allowed to use comments as placeholders for bigger chunks of code.</p>
<p>Those comments are also a very good hints for decomposition and short, but
descriptive, commnets (if they are short enough and you decide not to factor them
out to separate functions).</p>
<divclass="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><divclass="admonitionHeading_Gvgb"><spanclass="admonitionIcon_Rf37"><svgviewBox="0 0 12 16"><pathfill-rule="evenodd"d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><divclass="admonitionContent_BuS1"><p>The smaller the function is, the easier it is to test it and argue about its
correctness.</p></div></div>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="proper-pseudocode">»Proper« pseudocode<ahref="#proper-pseudocode"class="hash-link"aria-label="Direct link to »Proper« pseudocode"title="Direct link to »Proper« pseudocode"></a></h2>
<p>If you are satisfied with the <em>»rough« pseudocode</em>, it's time to convert it into
a proper one. Get rid of the uncertain pieces of functionality and replace them
with proper pseudocode, i.e. list of the things that should happen in its place.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="library">Library<ahref="#library"class="hash-link"aria-label="Direct link to Library"title="Direct link to Library"></a></h2>
<p>If you got here, and you <strong>actually</strong> wrote down the pseudocode, you can try your
solution after downloading the sources linked at the beginning. If you download
the ZIP-file, you can there:</p>
<ul>
<li>
<p><code>generate_mazes.py</code> - that was used to generate the same maze with beepers in
different locations</p>
</li>
<li>
<p><code>karel_tk.py</code> - library which can run Karel given the his world</p>
<ul>
<li>documentation can be found <ahref="https://www.fi.muni.cz/~xfocko/ib111/10/docs/"target="_blank"rel="noopener noreferrer">here</a></li>
<li>also requires Tk Python library to be installed (it should be included in
majority of Python installations)</li>
</ul>
</li>
<li>
<p><code>*.kw</code> - which represent multiple worlds for Karel I have prepared</p>
</li>
<li>
<p><code>skeleton.py</code> - skeleton for your solution, needs to be put in the same directory
as <code>karel_tk.py</code> and takes path to the world as a first argument, example usage:</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="solution">Solution<ahref="#solution"class="hash-link"aria-label="Direct link to Solution"title="Direct link to Solution"></a></h2>
<p>Solution to this problem will be released as a second part, so that you can try
it out by yourself without any influence of “example solution”.</p>
<p>If you want to get any feedback, feel free to mail me your solution (including
all the steps that lead to your final solution, if you wish to get feedback on