Last active
September 3, 2016 14:56
-
-
Save vnegrisolo/ac3504ebfcbbfe19a4eb to your computer and use it in GitHub Desktop.
This is a nice logic challenge and a javascript training tool http://play.elevatorsaga.com/
This file contains 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
{ | |
Elevator: function(elevator) { | |
this.bindEvents = function() { | |
elevator.on("idle", function() { | |
elevator.goToFloor(0); | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
elevator.goToFloor(floorNum); | |
}); | |
}; | |
this.goToFloor = function(floorNum) { | |
elevator.destinationQueue.push(floorNum); | |
elevator.checkDestinationQueue(); | |
}; | |
this.score = function() { | |
return 1 / elevator.destinationQueue.length; | |
}; | |
}, | |
Elevators: function(elevators) { | |
this.bestOne = function(floorNum) { | |
var index = 0; | |
var score = 0; | |
elevators.forEach(function(elevator, i) { | |
if(score < elevator.score()) { | |
index = i; | |
score = elevator.score(); | |
} | |
}); | |
return elevators[index]; | |
}; | |
}, | |
Floor: function(floor, elevators) { | |
this.bindEvents = function() { | |
floor.on("up_button_pressed", function() { | |
elevators.bestOne(this.floorNum()).goToFloor(this.floorNum()); | |
}); | |
floor.on("down_button_pressed", function() { | |
elevators.bestOne(this.floorNum()).goToFloor(this.floorNum()); | |
}); | |
}; | |
}, | |
Floors: function(floors) { | |
}, | |
init: function(elevators, floors) { | |
var self = this; | |
elevators = elevators.map(function(elevator) { return new self.Elevator(elevator); }); | |
elevators.forEach(function(elevator) { elevator.bindEvents(); }); | |
elevators = new this.Elevators(elevators); | |
floors = floors.map(function(floor) { return new self.Floor(floor, elevators); }); | |
floors.forEach(function(floor) { floor.bindEvents(); }); | |
floors = new this.Floors(floors); | |
}, | |
update: function(dt, elevators, floors) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment