-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
225 lines (173 loc) · 5.55 KB
/
script.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var c = 0;
var aiPos = 0;
var walls = 0;
var avoided = 0;
var crash = 0;
//window reload pop-up
function tick()
{
c++;
document.getElementById('stepsDone').value = c;
moveWall(); //move wall to the left
checkCollision();
experience();
}
var tickInterval;
function runSim(state)
{
if(state == 1)//run simulation
{
if(c > 100)
{
runSim('0'); //force stop
}else{
tickInterval = setInterval("tick();", 20);
}
}else{
//stop simulation
clearInterval(tickInterval);
c = 0;
document.getElementById('stepsDone').value = c;
document.getElementById('wall').style.left = null;
document.getElementById('wall').style.right = '0px';
document.getElementById('ai').style.marginTop = "50px";
aiPos = 0;
walls = 0;
crash = 0;
avoided = 0;
}
}
function moveWall()
{
var getWallX = document.getElementById('wall').offsetLeft;
var getWallY = document.getElementById('wall').offsetTop;
var getAIX = document.getElementById('sensor_2').offsetLeft+500;
var getAIY = document.getElementById('ai').offsetTop;
var successRate = Math.floor((avoided/(avoided+crash)*100));
document.getElementById('topDebug').innerHTML = " ["+c+"]<br> Wall ("+getWallX+","+getWallY+")<br> AI ("+getAIX+","+getAIY+")<br> Walls: "+walls+" Avoided: "+avoided+" Crash: "+crash+" Success rate: "+successRate+"%";
if(getWallX <= 0)
{
walls++;
}else{
getWallX = getWallX-20;
document.getElementById('wall').style.left = getWallX+'px';
}
}
function moveCar(direction)
{
//auto correct
if(aiPos < 50){aiPos = 50;}
if(aiPos > 200){aiPos = 200;}
if(direction == 'down')
{
aiPos=aiPos+10;
}else{
aiPos=aiPos-10;
}
document.getElementById('ai').style.marginTop = aiPos+"px";
}
function checkCollision()
{
var getWallX = document.getElementById('wall').offsetLeft;
var getAIX = document.getElementById('sensor_2').offsetLeft+500;
var getWallY = document.getElementById('wall').offsetTop+100;
var getAIY = document.getElementById('ai').offsetTop;
if(getWallX < getAIX && getAIY >= getWallY-100 && getAIY < getWallY || getWallX < getAIX && getWallY-100 > getAIY && getWallY-100 < getAIY+50)
{
//moveCar('down');
document.getElementById('sensor_2').style.backgroundColor = 'red';
//decide if the car was hit
if(getWallX < 100){crash++;}
}else{
document.getElementById('sensor_2').style.backgroundColor = 'white';
if(getWallX < 100){avoided++;}
}
}
var lastWallCount = 0;
var lastAvoided = 0;
var lastCrash = 0;
var tryzone = 0;
function experience()
{
var aizone;
var wallzone;
//get wall and ai zone
var getWallY = document.getElementById('wall').offsetTop;
var getAIY = document.getElementById('ai').offsetTop;
var getWallCenter = (getWallY-100)+50;
if(getWallCenter <= 150)
{
//zone 0
document.getElementById('t_wall_zone').innerHTML = "0";
wallzone = "0";
}else{
//zone 1
document.getElementById('t_wall_zone').innerHTML = "1";
wallzone = "1";
}
var getAICenter = (getAIY-100)+25;
if(getAICenter <= 150)
{
//zone 0
document.getElementById('t_ai_zone').innerHTML = "0";
aizone = "0";
}else{
//zone 1
document.getElementById('t_ai_zone').innerHTML = "1";
aizone = "1";
}
//trying
document.getElementById('t_trying').innerHTML = tryzone;
//read from experience 'database'
var buildvar = aizone+wallzone+tryzone;
var experienceDB = document.getElementById('succ_'+buildvar).innerHTML;
//read from 'DB' and decide
if(tryzone == 0)
{
buildvarOther = aizone+wallzone+1;
experienceDBOther = document.getElementById('succ_'+buildvarOther).innerHTML;
if(parseInt(experienceDBOther) > parseInt(experienceDB)+parseInt(10))
{
buildvar = buildvarOther;
experienceDB = document.getElementById('succ_'+buildvar).innerHTML;
tryzone = 1;
}
}
if(tryzone == 1)
{
buildvarOther = aizone+wallzone+0;
experienceDBOther = document.getElementById('succ_'+buildvarOther).innerHTML;
if(parseInt(experienceDBOther) > parseInt(experienceDB)+parseInt(10))
{
buildvar = buildvarOther;
experienceDB = document.getElementById('succ_'+buildvar).innerHTML;
tryzone = 0;
}
}
//move AI
if(tryzone == 0){moveCar('up');}else{moveCar('down');}
//update DB only when wall is leftmost
if(lastWallCount != walls)
{
//do update
if(lastAvoided != avoided)
{
experienceDB = parseInt(experienceDB)+parseInt(1);
document.getElementById('succ_'+buildvar).innerHTML = experienceDB;
lastAvoided = avoided;
}
if(lastCrash != crash)
{
experienceDB = parseInt(experienceDB)-parseInt(1);
document.getElementById('succ_'+buildvar).innerHTML = experienceDB;
lastCrash = crash;
}
lastWallCount = walls;
tryzone = Math.floor(Math.random() * 2);//random between 0 and 1
//randomize wall position
var randomWallYPos = Math.floor(Math.random() * (200 + 1) + 0);
document.getElementById('wall').style.marginTop = randomWallYPos+"px";
document.getElementById('wall').style.left = null;
document.getElementById('wall').style.right = '0px';
}
}