Last active
April 23, 2020 00:18
-
-
Save gleitz/cd35632ed7b84330340a6355dd00c58c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from karel.stanfordkarel import * | |
""" | |
File: MidpointKarel.py | |
---------------------- | |
When you finish writing it, MidpointKarel should | |
leave a beeper on the corner closest to the center of 1st Street | |
(or either of the two central corners if 1st Street has an even | |
number of corners). Karel can put down additional beepers as it | |
looks for the midpoint, but must pick them up again before it | |
stops. The world may be of any size, but you are allowed to | |
assume that it is at least as tall as it is wide. | |
""" | |
def main(): | |
# make main diagonal | |
draw_diagonal(MAGENTA) | |
# drop back down | |
move_to_bottom() | |
# face west | |
turn_right() | |
# scan diagonal for magenta | |
while not corner_color_is(MAGENTA): | |
continue_on_diagonal() | |
# we found the magenta! | |
drop_bottom_beeper() | |
# back to the beginning | |
turn_right() | |
move_forward_full() | |
turn_around() | |
# clean up diagonal | |
draw_diagonal(BLANK) | |
# get back to the beeper | |
find_beeper() | |
def continue_on_diagonal(): | |
move() | |
if facing_west(): | |
turn_right() | |
else: | |
turn_left() | |
def turn_right(): | |
turn_left() | |
turn_left() | |
turn_left() | |
def turn_around(): | |
turn_left() | |
turn_left() | |
def move_to_bottom(): | |
if facing_north(): | |
turn_around() | |
elif facing_east(): | |
turn_right() | |
elif facing_west(): | |
turn_left() | |
move_forward_full() | |
def drop_bottom_beeper(): | |
move_to_bottom() | |
put_beeper() | |
def move_forward_full(): | |
while front_is_clear(): | |
move() | |
def draw_diagonal(color): | |
while front_is_clear(): | |
paint_corner(color) | |
move() | |
turn_left() | |
if front_is_clear(): | |
move() | |
turn_right() | |
else: | |
turn_right() | |
paint_corner(color) | |
def find_beeper(): | |
turn_right() | |
move_forward_full() | |
turn_right() | |
while no_beepers_present(): | |
move() | |
# There is no need to edit code beyond this point | |
if __name__ == "__main__": | |
run_karel_program() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from karel.stanfordkarel import * | |
def main(): | |
put_beeper() | |
line() | |
wall() | |
while front_is_clear(): | |
while no_beepers_present(): | |
move() | |
pick_beeper() | |
go() | |
wall() | |
def line(): | |
while front_is_clear(): | |
move() | |
put_beeper() | |
def wall(): | |
if front_is_blocked(): | |
turn_around() | |
def go(): | |
while front_is_clear(): | |
move() | |
def turn_right(): | |
"turns Karel right" | |
for i in range(3): | |
turn_left() | |
def turn_around(): | |
"turns Karel 180 degrees" | |
for i in range(2): | |
turn_left() | |
if __name__ == "__main__": | |
run_karel_program() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment