-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelevatorsaga.js
More file actions
660 lines (528 loc) · 21.7 KB
/
Copy pathelevatorsaga.js
File metadata and controls
660 lines (528 loc) · 21.7 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// {
// init: function(elevators, floors) {
// // Do stuff with the elevators and floors, which are both arrays of objects
// },
// update: function(dt, elevators, floors) {
// // Do more stuff with the elevators and floors
// // dt is the number of game seconds that passed since the last time update was called
// }
// }
// init is called when the challenge starts (put in event listeners and logic)
// update is called repeatedly during the challenge
//level 1 completed code:
{
init: function(elevators, floors) {
var elevator = elevators[0]; // Let's use the first elevator
// Whenever the elevator is idle (has no more queued destinations) ...
elevator.on("idle", function() {
// let's go to all the floors (or did we forget one?)
elevator.goToFloor(0);
elevator.goToFloor(1);
elevator.goToFloor(2);
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
transported 15 ppl
elapsed time 52 sec
transported/s .291 ppl/sec
avg waiting time 6.1 s
max waiting time 12.3 s
moves 27
elevator1.goToFloor(0);
elevator1.goToFloor(1);
elevator1.goToFloor(2);
elevator1.goToFloor(3);
elevator1.goToFloor(4);
elevator1.goToFloor(5);
elevator1.goToFloor(6);
elevator1.goToFloor(7);
{
init: function(elevators, floors) {
var elevator1 = elevators[0]; // Let's use the first elevator
var elevator2 = elevators[1];
var elevator3 = elevators[2];
var elevator4 = elevators[3];
// Whenever the elevator is idle (has no more queued destinations) ...
elevator1.on("idle", function() {
// let's go to all the floors (or did we forget one?)
elevator1.goToFloor(0);
elevator1.goToFloor(1);
elevator1.goToFloor(2);
elevator1.goToFloor(3);
elevator1.goToFloor(4);
elevator1.goToFloor(5);
});
elevator2.on("idle", function() {
elevator2.goToFloor(5);
elevator2.goToFloor(4);
elevator2.goToFloor(3);
elevator2.goToFloor(2);
elevator2.goToFloor(1);
elevator2.goToFloor(0);
});
elevator3.on("idle", function() {
elevator3.goToFloor(0);
elevator3.goToFloor(1);
elevator3.goToFloor(2);
elevator3.goToFloor(3);
elevator3.goToFloor(4);
elevator3.goToFloor(5);
});
elevator4.on("idle", function() {
elevator4.goToFloor(5);
elevator4.goToFloor(4);
elevator4.goToFloor(3);
elevator4.goToFloor(2);
elevator4.goToFloor(1);
elevator4.goToFloor(0);
});
elevator1.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator1.goToFloor(floorNum, true);
})
elevator2.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator2.goToFloor(floorNum, true);
})
elevator3.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator3.goToFloor(floorNum, true);
})
elevator4.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator4.goToFloor(floorNum, true);
})
// if we are on the 0 floor and we are idle, go up to each floor
// if we are on the 4th floor and we are idle, go down to each floor
// if we are on floor 1, 2, 3 and we are idle, go the direction that the people in the elevator want to go
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
// level 5
{
init: function(elevators, floors) {
var elevator1 = elevators[0]; // Let's use the first elevator
var elevator2 = elevators[1];
var elevator3 = elevators[2];
var elevator4 = elevators[3];
// Whenever the elevator is idle (has no more queued destinations) ...
elevator1.on("idle", function() {
// let's go to all the floors (or did we forget one?)
elevator1.goToFloor(0);
//elevator1.goToFloor(1);
elevator1.goToFloor(2);
//elevator1.goToFloor(3);
elevator1.goToFloor(4);
//elevator1.goToFloor(5);
});
elevator2.on("idle", function() {
elevator2.goToFloor(5);
//elevator2.goToFloor(4);
elevator2.goToFloor(3);
//elevator2.goToFloor(2);
elevator2.goToFloor(1);
elevator2.goToFloor(0);
});
elevator3.on("idle", function() {
elevator3.goToFloor(0);
elevator3.goToFloor(1);
//elevator3.goToFloor(2);
elevator3.goToFloor(3);
//elevator3.goToFloor(4);
elevator3.goToFloor(5);
});
elevator4.on("idle", function() {
//elevator4.goToFloor(5);
elevator4.goToFloor(4);
//elevator4.goToFloor(3);
elevator4.goToFloor(2);
//elevator4.goToFloor(1);
elevator4.goToFloor(0);
});
elevator1.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator1.goToFloor(floorNum);
})
elevator2.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator2.goToFloor(floorNum);
})
elevator3.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator3.goToFloor(floorNum);
})
elevator4.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator4.goToFloor(floorNum);
})
// if we are on the 0 floor and we are idle, go up to each floor
// if we are on the 4th floor and we are idle, go down to each floor
// if we are on floor 1, 2, 3 and we are idle, go the direction that the people in the elevator want to go
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
// level 6
{
init: function(elevators, floors) {
var elevator1 = elevators[0]; // Let's use the first elevator
var elevator2 = elevators[1];
// Whenever the elevator is idle (has no more queued destinations) ...
elevator1.on("idle", function() {
// let's go to all the floors (or did we forget one?)
elevator1.goToFloor(0);
//elevator1.goToFloor(1);
//elevator1.goToFloor(2);
//elevator1.goToFloor(3);
});
elevator2.on("idle", function() {
elevator2.goToFloor(3);
//elevator2.goToFloor(2);
//elevator2.goToFloor(1);
//elevator2.goToFloor(0);
});
elevator1.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator1.goToFloor(floorNum);
})
elevator2.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator2.goToFloor(floorNum);
})
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
// level 7
{
init: function(elevators, floors) {
var elevator1 = elevators[0]; // Let's use the first elevator
var elevator2 = elevators[1];
var elevator3 = elevators[2];
// Whenever the elevator is idle (has no more queued destinations) ...
elevator1.on("idle", function() {
// let's go to all the floors (or did we forget one?)
elevator1.goToFloor(0);
//elevator1.goToFloor(1);
//elevator1.goToFloor(2);
//elevator1.goToFloor(3);
});
elevator2.on("idle", function() {
elevator2.goToFloor(0);
//elevator2.goToFloor(2);
//elevator2.goToFloor(1);
//elevator2.goToFloor(0);
});
elevator3.on("idle", function() {
elevator3.goToFloor(1);
//elevator2.goToFloor(2);
//elevator2.goToFloor(1);
//elevator2.goToFloor(0);
});
elevator1.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator1.goToFloor(floorNum);
})
elevator2.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator2.goToFloor(floorNum);
})
elevator3.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator3.goToFloor(floorNum);
})
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
// how to cluster riders?
// is the elevator going up or down?
// are people already in the elevator going to a floor that is adjacent to the floor you want to go to?
{
init: function(elevators, floors) {
var elevators = [0,1,2]
elevators.forEach(function(elevator) {
elevator.on("idle", function() {
// let's go to all the floors (or did we forget one?)
elevator.goToFloor(0);
elevator.goToFloor(1);
elevator.goToFloor(2);
});
elevator.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator.goToFloor(floorNum);
})
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
//////////////////////////////////////////////////////////
{
init: function(elevators, floors) {
var downQueue = [];
var upQueue = [];
var idleElevators = []
var topFloor = floors.length - 1;
// go to the floor that a person inside the elevator, and set the status of the elevator to either 'going up' or 'going down'
var goToAndSetIndicator = function(elevator, floorNum) {
if (floorNum < elevator.currentFloor()) {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(false);
} else {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
}
elevator.goToFloor(floorNum);
}
//
var callElevator = function(queue, floorNum) {
// if there is an idle elevator, wake it up
if (idleElevators.length > 0) {
elevator = idleElevators.shift();
goToAndSetIndicator(elevator, floorNum);
} else {
// otherwise add it to the corresponding queue and wait for an elevator to pass
if (queue.indexOf(floorNum) < 0) queue.push(floorNum);
queue = downQueue.sort();
}
}
// iterate through each of the elevators and listen for events
_.each(elevators, function(elevator) {
// if an elevator is idle, check to see if there is anyone waiting in the queue. go to the floor that is first in the queue and set the elevator indicators to 'up' or 'down'
elevator.on("idle", function() {
// see if there's anyone waiting
if (upQueue.length + downQueue.length > 0) {
// if we were going up, go to the highest floor with a person waiting
// otherwise, go to the lowest floor with a person waiting
if (elevator.goingUpIndicator()) {
floorNum = (downQueue.length > 0 ? downQueue.pop() : upQueue.pop())
} else {
floorNum = (upQueue.length > 0 ? upQueue.shift() : downQueue.shift())
}
goToAndSetIndicator(elevator, floorNum);
} else {
// if nobody was waiting, record that this elevator is idle, so it can be woken up later
idleElevators.push(elevator);
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(false);
}
});
// when somebody inside the elevator pushes a button to go to a new floor:
elevator.on("floor_button_pressed", function(floorNum) {
// add the new floor to the queue if it wasn't already requested
if (elevator.destinationQueue.indexOf(floorNum) < 0 ) {
elevator.destinationQueue.push(floorNum);
// sort to make sure we visit floors in order
elevator.destinationQueue = elevator.destinationQueue.sort();
if (elevator.goingDownIndicator()) {
elevator.destinationQueue = elevator.destinationQueue.reverse();
}
// apply the changes
elevator.checkDestinationQueue();
}
});
elevator.on("stopped_at_floor", function(floorNum) {
if (floorNum == 0) {
// start moving up once we reach the bottom
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(true);
} else if (floorNum == topFloor) {
// start moving down once we reach the top
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
}
else if (elevator.destinationQueue.length == 0) {
// if this was our final stop, pick up anybody here
// if we end up idling, then these will both get set to false
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
}
// remove this floor from the corresponding queue if we picked someone up
if (elevator.goingUpIndicator()) {
index = upQueue.indexOf(floorNum);
if (index >= 0) upQueue.splice(index, 1);
} else if (elevator.goingDownIndicator()) {
index = downQueue.indexOf(floorNum);
if (index >= 0) downQueue.splice(index, 1);
}
});
elevator.on("passing_floor", function(floorNum, direction) {
// check if there is someone here who wants to go our direction
queue = direction == "up" ? upQueue : downQueue;
index = queue.indexOf(floorNum);
if (index >= 0 && elevator.loadFactor() < 0.7) {
// if yes, and we have room, stop for them
elevator.goToFloor(floorNum, true);
}
});
});
_.each(floors, function(floor) {
// when someone on a floor pushes the up button, pass in the upQue and the number of the floor to call an elevator
floor.on("up_button_pressed", function() {
callElevator(upQueue, floor.floorNum());
});
floor.on("down_button_pressed", function() {
callElevator(downQueue, floor.floorNum());
});
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
///////////
{
init: function(elevators, floors) {
var elevatorCount = elevators.length;
var floorCount = floors.length;
var downQueue = [];
var upQueue = [];
var idleElevatorCount = []
// function to go a requested floor
var goToFloorRequested = function(floorNum, elevator) {
// set indicators based on where the elevator is relative to where its going
if (elevator.currentFloor() > floorNum) {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(false);
} else {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
};
elevator.goToFloor(floorNum);
};
// function to get an elevator to use
var callElevatorToFloor = function(queue, floorNum) {
// if there is an idle elevator, use it for the request
if (idleElevatorCount.length > 0) {
elevator = idleElevatorCount.shift();
goToFloorRequested(floorNum, elevator);
} else {
// otherwise push that floorNum to the corresponding queue and wait for an elevator going that direction
// otherwise add it to the corresponding queue and wait for an elevator to pass
if (queue.indexOf(floorNum) < 0) {
queue.push(floorNum);
}
queue = downQueue.sort();
};
};
// for every single elevator:
for (var i = 0; i < elevatorCount; i++) {
elevators[i].on("idle", function() {
var elevator = this;
if (upQueue.length + downQueue.length === 0) {
if (elevator.goingUpIndicator()) {
// if we were going up, go to the highest floor with a person waiting
if (downQueue.length > 0) {
floorNum = downQueue.pop();
} else {
floorNum = upQueue.pop();
};
} else {
// shift takes the first index from an array
// pop takes the last index from an array
// otherwise, go to the lowest floor with a person waiting
if (upQueue.length > 0) {
floorNum = upQueue.shift();
} else {
floorNum = downQueue.shift();
};
};
} else {
// if there's nobody waiting for an elevator, set the elevator to idle
idleElevatorCount.push(elevator);
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(false);
};
});
// when someone goes into an elevator and presses a floor to go to, send the right elevator to that floor
elevators[i].on("floor_button_pressed", function(floorNum) {
var elevator = this;
// add the new floor to the queue if it wasn't already requested
if (elevator.destinationQueue.indexOf(floorNum) < 0 ) {
elevator.destinationQueue.push(floorNum);
// sort to make sure we visit floors in order
elevator.destinationQueue = elevator.destinationQueue.sort();
if (elevator.goingDownIndicator()) {
elevator.destinationQueue = elevator.destinationQueue.reverse();
};
// apply the changes
elevator.checkDestinationQueue();
};
});
elevators[i].on("stopped_at_floor", function(floorNum) {
var elevator = this;
// elevator.goingDownIndicator(true);
// elevator.goingUpIndicator(true);
if (floorNum == 0) {
// start moving up once we reach the bottom
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(true);
} else if (floorNum == floorCount) {
// start moving down once we reach the top
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
} else if (elevator.destinationQueue.length == 0) {
// if this was our final stop, pick up anybody here
// if the elevator ends up being idle, then these will both get set to false
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
};
// remove this floor from the corresponding queue if we picked someone up
if (elevator.goingUpIndicator()) {
index = upQueue.indexOf(floorNum);
if (index >= 0) {
upQueue.splice(index, 1);
};
} else if (elevator.goingDownIndicator()) {
index = downQueue.indexOf(floorNum);
if (index >= 0) {
downQueue.splice(index, 1);
};
};
});
elevators[i].on("passing_floor", function(floorNum, direction) {
var elevator = this;
if (direction == "up") {
queue = upQueue;
} else {
queue = downQueue;
};
// check if they're going to a floor already in the queue
index = queue.indexOf(floorNum);
if (index >= 0 && elevator.loadFactor() < 0.8) {
// if yes, and we have room, stop for them
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
elevator.goToFloor(floorNum, true);
};
});
};
// for every single floor
for (var j = 0; j < floors.length; j++) {
// floor object properties
//floor.floorNum(); // gets the floor number of the floor object
// when someone presses the up or down button at a floor, call an elevator to that floor
floors[j].on("up_button_pressed", function() {
var floor = this;
callElevatorToFloor(upQueue, floor.floorNum());
});
floors[j].on("down_button_pressed", function() {
var floor = this;
callElevatorToFloor(downQueue, floor.floorNum());
});
};
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}