Created
August 16, 2015 18:35
-
-
Save francois/b4447e41ccd91b8ad56c to your computer and use it in GitHub Desktop.
These represent successive solutions to the [Elevator Saga](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
{ | |
init: function(elevators, floors) { | |
var waitingFloors = []; | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function() { | |
waitingFloors.push(floor.floorNum()); | |
}); | |
floor.on("down_button_pressed", function() { | |
waitingFloors.push(floor.floorNum()); | |
}); | |
}); | |
elevators.forEach(function(elevator) { | |
elevator.on("idle", function() { | |
if (elevator.getPressedFloors().length === 0) { | |
if (waitingFloors.length === 0) { | |
elevator.goToFloor(0); | |
} else { | |
var targetFloor = waitingFloors[0]; | |
var newWaitingFloors = []; | |
waitingFloors.forEach(function(fl) { if(fl === targetFloor) {} else { newWaitingFloors.push(fl); } }) | |
waitingFloors = newWaitingFloors; | |
elevator.goToFloor(targetFloor); | |
} | |
} else { | |
elevator.goToFloor(elevator.getPressedFloors()[0]); | |
} | |
}); | |
}); | |
}, | |
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