-
Notifications
You must be signed in to change notification settings - Fork 0
/
karel.py
127 lines (102 loc) · 3.57 KB
/
karel.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from gui import Gui
from tkinter import *
class World():
def print_world(self):
for i in self.world:
print(*i)
print('\n')
def get_karel(self):
x = -1
y = -1
for i in range(len(self.world)):
for j in range(len(self.world[i])):
if self.world[i][j] == 'K':
x = i
y = j
break
return (x, y)
def __init__(self, world):
self.world = world
class Robot():
##############
# Directions #
# 0 - down #
# 1 - right #
# 3 - left #
# 2 - up #
##############
def loop(self):
self.gui.window.mainloop()
def __init__(self):
self.block = 0
self.gui = Gui(Tk())
while(self.gui.world == ""): #There we are waiiting for attaching the map
self.gui.window.update()
while(self.gui.run_pressed == False): #There we are waiiting for attaching the map
self.gui.window.update()
self.world = World(self.gui.world)
tx, ty = self.world.get_karel()
#self.world.print_world()
self.direction = 1
self.x = tx
self.y = ty
def move(self):
oldx = self.x
oldy = self.y
if self.front_is_clear() == False:
self.gui.bug()
else:
if self.direction == 0:
self.x += 1;
if self.direction == 2:
self.x -= 1;
if self.direction == 1:
self.y += 1;
if self.direction == 3:
self.y -= 1;
self.world.world[oldx][oldy] = self.block; #Restoring previous box in WORLD
self.gui.render_object(self.block, oldx, oldy) #Restoring previous box in GUI
self.block = self.world.world[self.x][self.y] #Remembering previous and current box
#self.world.world[self.x][self.y] = 'K'; #Moving Karel in World
self.gui.render_object('K', self.x, self.y) #Moving Karel to next box GUI
for i in range(int(15000 / self.gui.speed)):
self.gui.window.update()
def turn_left(self):
self.direction = (self.direction + 1) % 4;
self.gui.direct = self.direction
def next_possition(self):
curx = self.x
cury = self.y
if self.direction == 0:
curx += 1;
if self.direction == 2:
curx -= 1;
if self.direction == 1:
cury += 1;
if self.direction == 3:
cury -= 1;
return (curx, cury)
def front_is_clear(self):
futx, futy = self.next_possition()
height = len(self.world.world)
width = len(self.world.world[0])
if futx >= height or futy >= width or self.world.world[futx][futy] == -1:
return False
return True
def beepers_present(self):
return int(self.block) >= 1;
def pick_beeper(self):
if self.beepers_present() == True:
self.block = max(self.world.world[self.x][self.y] - 1, 0)
self.world.world[self.x][self.y] = self.block;
#self.gui.render_object(self.world.world[self.x][self.y], self.x, self.y)
self.gui.render_object('K', self.x, self.y)
else:
self.gui.bug()
def put_beeper(self):
self.world.world[self.x][self.y] += 1
self.block = self.world.world[self.x][self.y]
self.gui.render_object(self.block, self.x, self.y)
self.gui.render_object('K', self.x, self.y)
def wait(self):
self.gui.window.mainloop()