-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZebraCrossing.py
61 lines (48 loc) · 1.24 KB
/
ZebraCrossing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Zebra Crossing Karel
"""
from karel.stanfordkarel import *
def turn_right():
for i in range(3):
turn_left()
def move_to_next_stripe():
"""
precondition: Karel is at the bottom right of the stripe and is facing East
postcondition: Karel is at the bottom left of the next stripe
"""
for i in range(4): # 3 stripes
move()
def beeper_column():
"""
Precondition: Karel is at the start of the column, facing the right direction
Postcondition: Karel is at the end of the column
"""
put_beeper() # fence post problem
while front_is_clear():
move()
put_beeper()
def draw_stripe():
"""
Precondition:
Karel is at the bottom left corner of a stripe
Karel starts by facing East
Postcondition:
Karel is at the bottom right corner of a stripe
Karel is facing East
"""
turn_left()
beeper_column()
turn_right()
move()
turn_right()
beeper_column()
turn_left()
def main():
draw_stripe()
while front_is_clear():
# move across the 3-square gap
# make a column of two beepers
move_to_next_stripe()
draw_stripe()
if __name__ == "__main__":
run_karel_program('ZebraCrossingKarel1.w')