-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_base_mechanics.html
185 lines (154 loc) · 3.46 KB
/
1_base_mechanics.html
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<div id="grid"></div>
<div id="coords"></div>
<div id="lastaction"></div>
</body>
<script>
var numRows = 6
var numCols = 6
function getData(){
var xPos = 1
var yPos = 1
var width = 100
var height = 100
var data = new Array()
for(var row = 0; row < numRows; row ++){
data.push(new Array())
for(var col = 0; col < numCols; col ++){
data[row].push({
x: xPos,
y: yPos,
width: width,
height: height
})
xPos += width
}
xPos = 1
yPos += height
}
return data
}
var gridData = getData()
function drawGrid(){
var grid = d3.select("#grid")
.append("svg")
.attr("width", (100 * numCols + 2) + "px")
.attr("height", (100 * numRows + 2) + "px")
var row = grid.selectAll(".row")
.data(gridData)
.enter()
.append("g")
.attr("class", "row")
var column = row.selectAll(".square")
.data(function(d) { return d })
.enter()
.append("rect")
.attr("class", "square")
.attr("x", function(d){ return d.x })
.attr("y", function(d){ return d.y })
.attr("width", function(d){ return d.width })
.attr("height", function(d){ return d.height })
.style("fill", "#fff")
.style("stroke", "#222");
}
function drawBall(xPos, yPos) {
d3.select(".circle")
.remove()
.exit()
var ball = d3.select("#grid")
.select("svg")
.append("g")
.attr("class", "circle")
.append("circle")
.attr("cx", (50 + 100 * (xPos - 1)) + "px")
.attr("cy", (50 + 100 * (yPos - 1)) + "px")
.attr("r", "25px")
.style("fill", "red")
.style("stroke", "black")
.style("stroke-width", "3")
}
function printCoords(coords){
d3.select("#coords")
.select("p")
.remove()
.exit()
d3.select("#coords")
.append("p")
.text("(" + coords + ")")
}
function printLastAction(lastaction){
d3.select("#lastaction")
.select("p")
.remove()
.exit()
d3.select("#lastaction")
.append("p")
.text("Last Action: " + lastaction)
if(collision){
console.log("collision!")
d3.select("#lastaction")
.select("p")
.style("color", "red")
}
}
function doAllTheStuff(){
var xPos = Math.floor(Math.random() * numCols) + 1
var yPos = Math.floor(Math.random() * numRows) + 1
var coords = [xPos, yPos]
drawBall(xPos, yPos)
}
function getNextAction(){
actions = ["n", "s", "w", "e"]
return actions[Math.floor(Math.random() * actions.length)];
}
function takeAction(coords, action){
if(action == 'n'){
if (coords[1] == 1){
collision = true;
return coords
}
else
return [coords[0], coords[1] - 1]
} else if (action == 's'){
if(coords[1] == numRows){
collision = true;
return coords
}
else
return [coords[0], coords[1] + 1]
} else if (action == 'w'){
if(coords[0] == 1){
collision = true
return coords
}
else
return [coords[0] - 1, coords[1]]
} else if (action == 'e'){
if(coords[0] == numCols){
collision = true
return coords
}
else
return [coords[0] + 1, coords[1]]
}
}
function playGame(){
collision = false
action = getNextAction()
coords = takeAction(coords, action)
drawBall(coords[0], coords[1])
printCoords(coords)
printLastAction(action)
}
var collision = false
var coords = [1, numRows] //Starting co-ords
drawGrid()
drawBall(coords[0], coords[1])
printCoords(coords)
d3.interval(playGame, 1000)
</script>
</html>