-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKarelRhoomba.py
84 lines (66 loc) · 2.31 KB
/
KarelRhoomba.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This tells PyCharm who Karel is
# Every Karel file has a line just like it
from karel.stanfordkarel import *
"""
This is a worked example. This code is starter code; you should edit and run it to
solve the problem. You can click the blue show solution button on the left to see
the answer if you get too stuck or want to check your work!
Note: The starter code for this example is the solution.
"""
def main():
"""
Clear the world, row by row. Each time a row is
cleared, reset to the start of the row to create
a consistent pre/post of the while loop
Left is clear until you are on the top row
"""
while left_is_clear(): # While there is nothing to the North of Karel, clear a row
# Note: the while-loop always assumes that Karel is facing right (east)
clear_row()
reset_to_next_row()
# Fencepost problem! This last clear_row() fixes it.
clear_row()
def clear_row():
"""
Clear an entire row
Pre: Karel is either facing East in column 1, or facing West in the last column
Post: If Karel started in column 1 they are now in the last column.
If Karel started in the last column they are now in column 1.
The row Karel is on has no beepers in it.
"""
while front_is_clear(): # We don't know for sure how large the world may be, so use a while loop!
clear_corner()
move()
# Fencepost problem! This last clear_corner() fixes it.
clear_corner()
def clear_corner():
"""
Cleans a corner so that there are no beepers on it.
Pre: The corner Karel is on has zero or one beepers present.
Post: The corner Karel is on has zero beepers present.
"""
if beepers_present():
pick_beeper()
def reset_to_next_row():
"""
Pre: Karel is at the end of a row, facing right (East)
Post: Karel is at the start of the next row, facing right (East)
"""
turn_around()
move_to_wall()
turn_right()
move()
turn_right()
def move_to_wall():
while front_is_clear():
move()
def turn_right():
for i in range(3):
turn_left()
def turn_around():
turn_left()
turn_left()
# This is "boilerplate" code which launches your code
# when you hit the run button
if __name__ == '__main__':
main()