Skip to content

Commit 0c66b8f

Browse files
antmarakisnorvig
authored andcommitted
Update grid.ipynb (#459)
1 parent 52eb90e commit 0c66b8f

File tree

1 file changed

+157
-5
lines changed

1 file changed

+157
-5
lines changed

grid.ipynb

Lines changed: 157 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,150 @@
1010
"source": [
1111
"# Grid\n",
1212
"\n",
13-
"The functions here are used often when dealing with 2D grids (like in TicTacToe).\n",
13+
"The functions here are used often when dealing with 2D grids (like in TicTacToe)."
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"### Heading\n",
21+
"\n",
22+
"With the `turn_heading`, `turn_left` and `turn_right` functions an agent can turn around in a grid. In a 2D grid the orientations normally are:\n",
23+
"\n",
24+
"* North: (0,1)\n",
25+
"* South: (0,-1)\n",
26+
"* East: (1,0)\n",
27+
"* West: (-1,0)\n",
28+
"\n",
29+
"In code:"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"metadata": {
36+
"collapsed": true
37+
},
38+
"outputs": [],
39+
"source": [
40+
"orientations = [(1, 0), (0, 1), (-1, 0), (0, -1)]"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"We signify a left turn with a +1 and a right turn with a -1.\n",
48+
"\n",
49+
"The functions `turn_left` and `turn_right` call `turn_heading`, which then turns the agent around according to the input.\n",
50+
"\n",
51+
"First the code for `turn_heading`:"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 3,
57+
"metadata": {
58+
"collapsed": false
59+
},
60+
"outputs": [],
61+
"source": [
62+
"def turn_heading(heading, inc, headings=orientations):\n",
63+
" return headings[(headings.index(heading) + inc) % len(headings)]"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"We can now use the function to turn left:"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 4,
76+
"metadata": {
77+
"collapsed": false
78+
},
79+
"outputs": [
80+
{
81+
"name": "stdout",
82+
"output_type": "stream",
83+
"text": [
84+
"(-1, 0)\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"print(turn_heading((0, 1), 1))"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"We were facing north and we turned left, so we are now facing west.\n",
97+
"\n",
98+
"Let's now take a look at the other two functions, which automate this process:"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 5,
104+
"metadata": {
105+
"collapsed": true
106+
},
107+
"outputs": [],
108+
"source": [
109+
"def turn_right(heading):\n",
110+
" return turn_heading(heading, -1)\n",
111+
"\n",
112+
"def turn_left(heading):\n",
113+
" return turn_heading(heading, +1)"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"The first one turns the agent right, so it passes -1 to `turn_heading`, while the second one turns the agent left, so it passes +1.\n",
14121
"\n",
122+
"Let's see what happens when we are facing north and want to turn left and right:"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 6,
128+
"metadata": {
129+
"collapsed": false
130+
},
131+
"outputs": [
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"(-1, 0)\n",
137+
"(1, 0)\n"
138+
]
139+
}
140+
],
141+
"source": [
142+
"print(turn_left((0, 1)))\n",
143+
"print(turn_right((0, 1)))"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"When we turn left from north we end up facing west, while on the other hand if we turn right we end up facing east."
151+
]
152+
},
153+
{
154+
"cell_type": "markdown",
155+
"metadata": {},
156+
"source": [
15157
"### Distance\n",
16158
"\n",
17159
"The function returns the Euclidean Distance between two points in the 2D space."
@@ -139,7 +281,9 @@
139281
"cell_type": "code",
140282
"execution_count": 5,
141283
"metadata": {
142-
"collapsed": true
284+
"collapsed": true,
285+
"deletable": true,
286+
"editable": true
143287
},
144288
"outputs": [],
145289
"source": [
@@ -154,7 +298,10 @@
154298
},
155299
{
156300
"cell_type": "markdown",
157-
"metadata": {},
301+
"metadata": {
302+
"deletable": true,
303+
"editable": true
304+
},
158305
"source": [
159306
"For example:"
160307
]
@@ -163,7 +310,9 @@
163310
"cell_type": "code",
164311
"execution_count": 6,
165312
"metadata": {
166-
"collapsed": false
313+
"collapsed": false,
314+
"deletable": true,
315+
"editable": true
167316
},
168317
"outputs": [
169318
{
@@ -180,7 +329,10 @@
180329
},
181330
{
182331
"cell_type": "markdown",
183-
"metadata": {},
332+
"metadata": {
333+
"deletable": true,
334+
"editable": true
335+
},
184336
"source": [
185337
"The vector we wanted to clip was the tuple (-1, 10). The lowest allowed values were (0, 0) and the highest (9, 9). So, the result is the tuple (0,9)."
186338
]

0 commit comments

Comments
 (0)