Stone Mason Karel in Python
Tags: python3, pythonbeginner, pythonprojects, 100daysofcode
Published: 2021-04-22 16:04:41

---
title: Stone Mason Karel in Python
published: true
date: 2021-04-22 16:02:43 UTC
tags: Python3,pythonbeginner,pythonprojects,100DaysOfCode
canonical_url: https://bharadwaj.hashnode.dev/stone-mason-karel-in-python
---

 **Introduction**

It is to repair the damage done to the Stanford Main Quad in the 1989 Loma Prieta earthquake. In particular, Karel should repair a set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches. When Karel is done, the missing stones in the columns should be replaced by beepers.

**Instructions to be followed**

Karel’s final location and the final direction Karel is facing at the end of the run do not matter. Karel may count on the following facts about the world:

Karel starts at the corner where 1st Avenue and 1st Street meet, facing east, with an infinite number of beepers in Karel’s beeper bag. The first column should be built on 1st Avenue.

The columns are always exactly four Avenues apart, so they would be built on 1st Avenue, 5th Avenue, 9th Avenue, and so on.

The final column will always have a wall immediately after it. Although this wall appears after 13th Avenue in the example figure, your program should work for any number of beeper columns.

The top of a beeper column will always be marked by a wall. However, Karel cannot assume that columns are always five units high, or even that all columns within a given world are the same height. 

In an initial world, some columns may already contain beepers representing stones that are still in place. Your program should not put a second beeper on corners that already have beepers. Avenues that will not have columns will never contain existing beepers.

There are 3 images where our code should be valid to all of them. They are

![Screenshot (90).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619106678428/G86lWkjBY.png) ![Screenshot (89).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619106686112/BRtpBZtjM.png) ![Screenshot (88).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619106716301/1LFHavMXS.png)The top is SampleQuad1, next one is SampleQuad2, and the last one is SampleQuad3.

The code for them is

```
def repair_column():
    turn_left()
    while front_is_clear():
        put_stone()
        move()
    if front_is_blocked():
        put_stone()
    original_column()

def original_column():
    turn_left()
    turn_left()
    while front_is_clear():
        move()
    turn_left()

def next_column():
    for i in range(4):
        move()
def put_stone():
    if beepers_present():
        pass
    else:
        put_beeper()

def main():
    repair_column()
    while front_is_clear():
        next_column()
        repair_column()

if __name__ == ' __main__':
    run_karel_program('SampleQuad1.w')

```

Just replace the last line of the code to check whether the code is running successfully in all three images or not.

The outputs for all three are shown below

![Screenshot (91).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619107310251/CBvV5XCEH.png)

![Screenshot (92).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619107317953/M43FyUcdB.png)

![Screenshot (93).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1619107327089/i0JLklV7O.png)