Last active
January 22, 2016 17:43
-
-
Save diN0bot/bca526f080ad56d39a4c to your computer and use it in GitHub Desktop.
ELEVATOR GAME
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
init: function(elevators, floors) { | |
var Model = function () { | |
this.af = []; | |
} | |
Model.prototype.next = function(e) { | |
if (this.af.length > 0) { | |
var n = null; | |
if (e.currentFloor() === 0) { n = this.pop_top(); } | |
else { n = this.pop_bot(); } | |
//if (e.currentFloor() < n ) { elevator.goingDownIndicator(false); elevator.goingUpIndicator(true); } | |
//else if (e.currentFloor() > n) { elevator.goingDownIndicator(true); elevator.goingUpIndicator(false); } | |
//else { elevator.goingDownIndicator(true); elevator.goingUpIndicator(true); console.log("BOTH TRUE"); } | |
e.goToFloor(n); | |
} | |
} | |
Model.prototype.add = function(f) { | |
if (this.af.indexOf(f) < 0) { | |
this.af.push(f); | |
this.af.sort(); | |
} | |
} | |
Model.prototype.pop_top = function() { | |
if (this.af.length <= 0) { console.log("ERROR: pop_top called but af is empty"); } | |
return this.af.pop(); | |
} | |
Model.prototype.pop_bot = function() { | |
if (this.af.length <= 0) { console.log("ERROR: pop_bot called but af is empty"); } | |
var f = this.af[0]; | |
this.af.splice(0, 1); | |
return f; | |
} | |
Model.prototype.peek_top = function() { | |
return this.af[this.af.length - 1]; | |
} | |
Model.prototype.peek_bot = function() { | |
return this.af[0]; | |
} | |
Model.prototype.remove_floor = function(n) { // does nothing if n not in af. return true if removed item. | |
if (this.af.indexOf(n) > -1) { | |
this.af.splice(this.af.indexOf(n), 1); | |
return true; | |
} | |
return false; | |
} | |
Model.prototype.pass = function(n, e) { | |
if (this.af.indexOf(n) > -1) { | |
e.goToFloor(n, true); | |
} | |
} | |
var setupElevator = function(elevator) { | |
var model = new Model(); | |
elevator.model = model; | |
elevator.on("idle", function() { | |
model.next(elevator); | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
model.add(floorNum); | |
}), | |
elevator.on("passing_floor", function(floorNum, direction) { | |
if (direction === elevator.destinationDirection()) { | |
model.pass(floorNum, elevator); | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
model.remove_floor(floorNum); | |
}); | |
} | |
for (var edx = 0; edx < elevators.length; edx++) { | |
var elevator = elevators[edx]; | |
setupElevator(elevator); | |
} | |
var setupFloor = function(floor) { | |
var closest = function(es, n) { // elevators, floorNum | |
var closest = null; | |
for (var edx = 0; edx < es.length; edx++) { | |
var e = es[edx]; | |
if (closest == null) { closest = e; } | |
else { | |
var cdist = Math.abs(closest.currentFloor() - n); | |
var edist = Math.abs(e.currentFloor() - n); | |
if (edist < cdist) { closest = e; } | |
} | |
} | |
return closest; | |
} | |
var characterists = function(e, n, dir) { // elevator, floorNum | |
var dist = Math.abs(e.currentFloor() - n); | |
var is_close = dist < floors.length / 2; | |
var is_same_dir = ( | |
(dir === "up" && e.currentFloor() <= n && e.destinationDirection() === "up") || | |
(dir === "down" && e.currentFloor() >= n && e.destinationDirection() === "down")); | |
var is_next_step = ( | |
(e.destinationDirection() === "up" && e.model.peek_top() <= n) || | |
(e.destinationDirection() === "down" && e.model.peek_bot() >= n)); | |
var is_stopped = e.destinationDirection() === "stopped"; | |
return {close_far: is_close ? "close" : "far", | |
relation: is_same_dir ? "same_dir" : is_next_step ? "next_step" : is_stopped ? "stopped" : "no_good"}; | |
} | |
var go_to_closest = function(es, close_far, relation, n, dir) { | |
var e = closest(es[close_far][relation], n); | |
console.log("------------------------"); | |
console.log(e.destinationDirection()+ " "+e.currentFloor()+" "+close_far+" "+relation); | |
console.log(n+" "+dir); | |
e.model.add(n); | |
e.model.next(e); | |
} | |
var on_button_pressed = function(dir) { | |
var n = floor.floorNum(); | |
var eles = {close: {same_dir: [], next_step: [], stopped: [], no_good: []}, | |
far: {same_dir: [], next_step: [], stopped: [], no_good: []}}; | |
for (var edx = 0; edx < elevators.length; edx++) { | |
var e = elevators[edx]; | |
var c = characterists(e, n, dir); | |
eles[c.close_far][c.relation].push(e) | |
} | |
if (eles.close.same_dir.length > 0) { go_to_closest(eles, "close", "same_dir", n, dir); } | |
else if (eles.close.stopped.length > 0) { go_to_closest(eles, "close", "stopped", n, dir); } | |
else if (eles.close.next_step.length > 0) { go_to_closest(eles, "close", "next_step", n, dir); } | |
else if (eles.far.same_dir.length > 0) { go_to_closest(eles, "far", "same_dir", n, dir); } | |
else if (eles.far.stopped.length > 0) { go_to_closest(eles, "far", "stopped", n, dir); } | |
else if (eles.far.next_step.length > 0) { go_to_closest(eles, "far", "next_step", n, dir); } | |
else { console.log("NO ELE PICKUP "+n); elevators[Math.floor(Math.random() * elevators.length)].model.add(n); } | |
} | |
floor.on("up_button_pressed", function() { | |
on_button_pressed("up"); | |
}) | |
floor.on("down_button_pressed", function() { | |
on_button_pressed("down"); | |
}) | |
} | |
for (var floorNum = 0; floorNum < floors.length; floorNum++) { | |
var floor = floors[floorNum]; | |
setupFloor(floor); | |
} | |
}, | |
update: function(dt, elevators, floors) { | |
// We normally don't need to do anything here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍