DEV Community

Vishnubhotla V D V Bharadwaj
Vishnubhotla V D V Bharadwaj

Posted on • Originally published at bharadwaj.hashnode.dev on

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 Screenshot (89).png Screenshot (88).pngThe 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')

Enter fullscreen mode Exit fullscreen mode

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

Screenshot (92).png

Screenshot (93).png

Top comments (0)