FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
aima-python/js/gridworld.js at master · aaronbcj/aima-python · GitHub
aaronbcj
/
aima-python
Public
forked from
aimacode/aima-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
aima-python
/
js
/
gridworld.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
126 lines (115 loc) · 3.96 KB
Breadcrumbs
aima-python
/
js
/
gridworld.js
Copy path
File metadata and controls
126 lines (115 loc) · 3.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
var
latest_output_area
=
"NONE"
;
// Jquery object for the DOM element of output area which was used most recently
function
handle_output
(
out
,
block
)
{
var
output
=
out
.
content
.
data
[
"text/html"
]
;
latest_output_area
.
html
(
output
)
;
}
function
handle_click
(
canvas
,
coord
)
{
console
.
log
(
canvas
,
coord
)
;
latest_output_area
=
$
(
canvas
)
.
parents
(
'.output_subarea'
)
;
$
(
canvas
)
.
parents
(
'.output_subarea'
)
var
world_object_name
=
canvas
.
dataset
.
world_name
;
var
command
=
world_object_name
+
".handle_click("
+
JSON
.
stringify
(
coord
)
+
")"
;
console
.
log
(
"Executing Command: "
+
command
)
;
var
kernel
=
IPython
.
notebook
.
kernel
;
var
callbacks
=
{
'iopub'
:
{
'output'
:
handle_output
}
}
;
kernel
.
execute
(
command
,
callbacks
)
;
}
;
function
generateGridWorld
(
state
,
size
,
elements
)
{
// Declaring array to store image object
var
$imgArray
=
new
Object
(
)
,
hasImg
=
false
;
// Loading images LOOP
$
.
each
(
elements
,
function
(
i
,
val
)
{
// filtering for type img
if
(
val
[
"type"
]
==
"img"
)
{
// setting image load
hasImg
=
true
;
$imgArray
[
i
]
=
$
(
'<img />'
)
.
attr
(
{
height
:
size
,
width
:
size
,
src
:
val
[
"source"
]
}
)
.
data
(
{
name
:
i
,
loaded
:
false
}
)
.
load
(
function
(
)
{
// Check for all image loaded
var
execute
=
true
;
$
(
this
)
.
data
(
"loaded"
,
true
)
;
$
.
each
(
$imgArray
,
function
(
i
,
val
)
{
if
(
!
$
(
this
)
.
data
(
"loaded"
)
)
{
execute
=
false
;
// exit on unloaded image
return
false
;
}
}
)
;
if
(
execute
)
{
// Converting loaded image to canvas covering block size.
$
.
each
(
$imgArray
,
function
(
i
,
val
)
{
$imgArray
[
i
]
=
$
(
'<canvas />'
)
.
attr
(
{
width
:
size
,
height
:
size
}
)
.
get
(
0
)
;
$imgArray
[
i
]
.
getContext
(
'2d'
)
.
drawImage
(
val
.
get
(
0
)
,
0
,
0
,
size
,
size
)
;
}
)
;
// initialize the world
initializeWorld
(
)
;
}
}
)
;
}
}
)
;
if
(
!
hasImg
)
{
initializeWorld
(
)
;
}
function
initializeWorld
(
)
{
var
$parentDiv
=
$
(
'div.map-grid-world'
)
;
// remove object reference
$
(
'div.map-grid-world'
)
.
removeClass
(
'map-grid-world'
)
;
// get some info about the canvas
var
row
=
state
.
length
;
var
column
=
state
[
0
]
.
length
;
var
canvas
=
$parentDiv
.
find
(
'canvas'
)
.
get
(
0
)
;
var
ctx
=
canvas
.
getContext
(
'2d'
)
;
canvas
.
width
=
size
*
column
;
canvas
.
height
=
size
*
row
;
//Initialize previous positions
for
(
var
i
=
0
;
i
<
row
;
++
i
)
{
for
(
var
j
=
0
;
j
<
column
;
++
j
)
{
if
(
elements
[
state
[
i
]
[
j
]
[
"val"
]
]
[
"type"
]
==
"color"
)
{
if
(
state
[
i
]
[
j
]
[
"val"
]
==
"default"
)
{
blockCreate
(
'black'
,
elements
[
state
[
i
]
[
j
]
[
"val"
]
]
[
"source"
]
,
i
,
j
)
;
}
else
{
blockCreate
(
'transparent'
,
elements
[
state
[
i
]
[
j
]
[
"val"
]
]
[
"source"
]
,
i
,
j
)
;
}
}
else
if
(
elements
[
state
[
i
]
[
j
]
[
"val"
]
]
[
"type"
]
===
"img"
)
{
pattern
=
ctx
.
createPattern
(
$imgArray
[
state
[
i
]
[
j
]
[
"val"
]
]
,
"repeat"
)
;
blockCreate
(
'transparent'
,
pattern
,
i
,
j
)
;
}
}
}
// function to create a block of given parameters
function
blockCreate
(
borderColor
,
fillColor
,
x
,
y
)
{
ctx
.
fillStyle
=
fillColor
;
ctx
.
fillRect
(
y
*
size
,
x
*
size
,
size
,
size
)
;
ctx
.
lineWidth
=
2
;
ctx
.
strokeStyle
=
borderColor
;
ctx
.
strokeRect
(
(
y
*
size
)
+
1
,
(
x
*
size
)
+
1
,
size
-
2
,
size
-
2
)
;
}
// click event, using jQuery for cross-browser convenience
$
(
canvas
)
.
click
(
function
(
e
)
{
// calculate grid square numbers
var
gy
=
~
~
(
e
.
offsetX
/
size
)
;
var
gx
=
~
~
(
e
.
offsetY
/
size
)
;
//handle click send data
var
coord
=
new
Array
(
)
;
coord
.
push
(
gx
,
gy
)
handle_click
(
canvas
,
coord
)
}
)
;
// Display tooltip and change on mousemove and mouseout
var
nameElement
=
$parentDiv
.
find
(
'span'
)
;
$
(
canvas
)
.
mousemove
(
function
(
e
)
{
//calculate grid square numbers
var
gy
=
~
~
(
e
.
offsetX
/
size
)
;
var
gx
=
~
~
(
e
.
offsetY
/
size
)
;
// updating tooltip
if
(
gx
>=
0
&&
gx
<
row
&&
gy
>=
0
&&
gy
<
column
)
{
nameElement
.
html
(
state
[
gx
]
[
gy
]
[
"tooltip"
]
)
;
}
}
)
.
mouseout
(
function
(
)
{
nameElement
.
html
(
""
)
;
}
)
;
}
;
}
;
// function call
generateGridWorld
(
gridArray
,
size
,
elements
)
;
Back
|
FazBrowse Home
|
New Git URL