-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.py
More file actions
162 lines (140 loc) · 4.9 KB
/
Copy pathProject.py
File metadata and controls
162 lines (140 loc) · 4.9 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from graphics import *
from time import sleep
win = GraphWin('Picobot',600,660)
CoPage = [[ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ],
[ 1,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,1,0,1 ],
[ 1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1 ],
[ 1,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,1 ],
[ 1,0,0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,1 ],
[ 1,0,1,1,1,0,1,1,1,1,0,1,0,1,0,1,0,1,0,1,1,0,0,0,1 ],
[ 1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1 ],
[ 1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0,0,1,0,0,0,1 ],
[ 1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,1,0,1,0,1,1,1 ],
[ 1,1,0,1,0,1,0,0,0,1,0,1,0,1,1,1,0,0,1,0,1,0,0,0,1 ],
[ 1,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1 ],
[ 1,1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,1,1,1,0,1 ],
[ 1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1 ],
[ 1,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,1,0,1,1,1,0,1,0,1 ],
[ 1,0,1,1,1,1,1,1,0,1,0,0,1,1,1,0,1,0,0,0,1,0,1,0,1 ],
[ 1,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,1,0,1 ],
[ 1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0,0,1,0,1,0,1,0,1 ],
[ 1,0,0,0,0,1,0,0,0,1,1,1,0,1,1,1,1,0,1,0,1,0,1,0,1 ],
[ 1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1,0,1 ],
[ 1,0,0,1,0,1,0,1,1,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,1 ],
[ 1,0,1,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1 ],
[ 1,0,1,0,0,1,0,1,0,0,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1 ],
[ 1,0,1,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1 ],
[ 1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,1,0,0,0,1,1 ],
[ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ]]
def saveRules(inFileName):
' a = state and b = surroundings and c = direction and d = new state '
inFile = open(inFileName,'r')
DicOfRules = {}
n = 1
for line in inFile:
xs = line.split()
a = xs[0]
b = xs[1]
c = xs[3]
d = xs[4]
DicOfRules[n] = a,b,c,d
n += 1
return DicOfRules
inFile.close()
def makemove(rule):
fstate = 0
x = 5
y = 1
CoOfBot = (x,y)
drawSquare(x, y, 'green')
def check_1(t):
dic = {'N' : 1,'E' : 1,'W' : 1,'S' : 1,'x' :0, 'X' : 0}
if int(t[0]) == fstate:
if t[1][0] == '*' or dic[t[1][0]] == CoPage[x][y+1]:
if t[1][1] == '*' or dic[t[1][1]] == CoPage[x+1][y]:
if t[1][2] == '*' or dic[t[1][2]] == CoPage[x-1][y]:
if t[1][3] == '*' or dic[t[1][3]] == CoPage[x][y-1]:
return True
return False
return False
return False
return False
return False
drule = rule.keys()
e = 1
lst = []
while e in drule:
if check_1(rule[e]):
nx, ny = x, y
if rule[e][2] == 'N':
ny = y+1
if rule[e][2] == 'E':
nx = x+1
if rule[e][2] == 'W':
nx = x-1
if rule[e][2] == 'S':
ny = y-1
fstate = int(rule[e][3])
if (x,y) not in lst:
lst.append((x,y))
drawSquare(x, y, 'gray')
drawSquare(nx, ny, 'green')
sleep(0.04)
x, y = nx, ny
e += 1
if e > len(drule):
e = 1
if len(lst) >= 279:
break
return CoOfBot
def drawSquare(x, y, color):
p1 = Point(y * 24, (x + 1) * 24)
p2 = Point((y + 1) * 24, x * 24)
q = Rectangle(p1, p2)
q.setOutline(color)
q.setFill(color)
q.draw(win)
def draw():
for i in range(25):
for j in range (25):
if CoPage[j][i] == 1:
p1 = Point(i * 24,(j + 1) * 24)
p2 = Point((i + 1) * 24,j * 24)
q = Rectangle(p1,p2)
q.setOutline('blue')
q.setFill('blue')
q.draw(win)
def drawStart():
square = Rectangle(Point(250,620),Point(350,640))
square.setFill('light blue')
square.setOutline('light blue')
message = Text(Point(300,630),'START')
message.setTextColor('black')
square.draw(win)
message.draw(win)
def check(x,y):
if x <= 350 and x >= 250:
if y >= 620 and y <= 640:
return True
return False
return False
dic = saveRules('rules.txt')
draw()
drawStart()
drawSquare(5, 1, 'green')
Picobot = Text(Point(475,630),'" Picobot "')
Picobot.setTextColor('orange')
Picobot.setStyle('italic')
Picobot.setSize(15)
Picobot.draw(win)
message = Text(Point(125,630),'Click on START')
message.setTextColor('orange')
message.setStyle('italic')
message.setSize(15)
message.draw(win)
p1 = win.getMouse()
if check(p1.getX(),p1.getY()):
makemove(dic)
message.setText('Click anywhere to quit')
win.getMouse()
win.close()