Elevator Saga
Saved games stay in this browser only.
About the game
Elevator Saga is a game you play by writing code. The top half of the screen is a building seen from the side, with floors, call buttons and one or more elevators; little people appear on the floors, press up or down and wait. The bottom half is a code editor. Nothing moves on its own: the elevators go where your JavaScript tells them to go.
Your program is an object with two functions. init runs once at the start of
a challenge and receives the elevators and floors, so you can attach handlers
to events such as an elevator becoming idle, a passenger pressing a button
inside it or someone calling it from a floor. update runs on every tick if
you want finer control. Each elevator has a queue of floors to visit, a load
factor that tells how full it is and a few other properties described in the
built-in documentation.
There are nineteen challenges. The first asks you to move fifteen people between three floors in a minute with a single elevator, and the default code almost does it. Later ones add floors, elevators of different sizes and new conditions: keep everyone's waiting time under a limit, or move a crowd using only a fixed number of elevator trips. The last one is a perpetual demo that just runs your code. Magnus Wolffelt released the game in 2015 and it is still a favourite exercise among programmers.
Playing it here
Click Play. The game page is wide, so the player shows it shrunk to fit; the Full screen button in the corner shows it at its real size, which makes the code much easier to read. Edit the code, then press Apply to start the current challenge with it. When a challenge is passed, a link takes you to the next one. Your code is saved in this browser a moment after you stop typing, so it is still there next time; Reset puts back the simple starting code, and Undo reset brings yours back.
The Help and Documentation links open the game's own manual, with the full list of functions and events, inside the player; "Back to the game" returns. This is the game as published by the author, with the analytics script and the link to the test suite removed and the Oswald font served from this site. Writing code needs a physical keyboard, so the game is meant for a computer.
Controls
| Input | Action |
|---|---|
| Code editor | write the JavaScript that drives the elevators |
| Apply | run the challenge with your code |
| Start / Pause | pause or resume the simulation |
| - and + | slow down or speed up time |
| Save, Reset, Undo reset | keep, discard or restore your code |
The editor indents with Tab and closes brackets for you.
A few things worth knowing
Read the errors. If your code does not run, a message under the building
says why. The browser's developer console is also handy for console.log
debugging.
Answer the buttons. The starting code only shuttles between floors 0 and
- Listening to
floor_button_pressedinside the elevator and to the up and down buttons on each floor is the first big improvement.
Do not waste trips. Check whether a floor is already in an elevator's
destinationQueue before adding it again, and send the emptiest elevator to
new calls.
Speed up time. Long challenges are easier to test at a higher speed; the results are the same.
Credits and licence
Elevator Saga was created by Magnus Wolffelt, with contributions from other programmers, and is released under the MIT licence, published in his repository on GitHub. It uses jQuery, Lodash, Riot, CodeMirror, highlight.js, Font Awesome and the Oswald font, each under its own free licence. The licence texts are linked from this page.