Scientyfic World

Write Structured content faster — Get your copy of DITA Decoded today.

Elevator Control Challenge

Level: 1 | Floors: 4 | Challenge: Loading...
📄 elevator.js
JavaScript
Welcome! Apply code to start.

Elevator Control Challenge - Developer Guide

The Elevator Control Challenge is a programming game where you write JavaScript code to control an elevator system. Your goal is to efficiently transport passengers between floors while meeting specific challenge objectives. The game progressively increases in difficulty with more floors, more complex challenges, and higher performance requirements.

Game Rules & Mechanics

Basic Elevator System

Elevator Properties:

  • Single elevator serving multiple floors (4-10 floors depending on level)
  • Maximum capacity: 8 passengers
  • Movement speed: 0.85 floors per second
  • Door opening duration: 2.5 seconds per stop

Passenger Behavior:

  • Passengers spawn randomly on floors with random destinations
  • They press floor buttons (up/down) to call the elevator
  • Passengers board only if the elevator is moving in their desired direction
  • Wait time is tracked from spawn until they reach their destination

Floor System:

  • Ground floor is Floor 0, top floors increase numerically
  • Each floor has up/down call buttons (except top floor has no up, ground floor has no down)
  • Call buttons remain active until all passengers going that direction are picked up

Challenge Types

The game rotates through three main challenge types:

1. Transport X People

Objective: Transport a target number of people (e.g., “Transport 7 people”)

  • Win Condition: Reach the target number of transported passengers
  • Fail Condition: None (can take as long as needed)
  • Strategy Focus: Basic efficiency and passenger pickup

2. Transport X People in Y Time

Objective: Transport passengers within a time limit (e.g., “Transport 5 people in 74 seconds”)

  • Win Condition: Reach target passengers AND stay under time limit
  • Fail Condition: Time expires before reaching passenger target
  • Strategy Focus: Speed optimization and time management

3. Average Wait Time Challenge

Objective: Transport passengers while keeping average wait time low (e.g., “Transport 5 people with average wait time below 23.5s”)

  • Win Condition: Reach target passengers AND maintain low average wait time
  • Fail Condition: Average wait time exceeds limit when target is reached, or heuristic early failure if performance is very poor
  • Strategy Focus: Minimizing passenger wait times through smart routing

Level Progression

  • Floors: Start with 4 floors, increase by 1 every 2 levels (max 10 floors)
  • Difficulty: Higher levels have more passengers to transport, tighter time limits, and stricter wait time requirements
  • Challenges: Cycle through the three challenge types as levels progress

Code Structure & API

Main Code Format

Your code must return an object with init and update functions:

({
    init: function(elevators, floors, log) {
        // Initialize your elevator logic here
        // This runs once when the level starts
    },
    
    update: function(dt, elevators, floors, log) {
        // Optional: Update logic that runs every frame
        // dt = delta time in milliseconds since last frame
    }
})

Elevator API

Core Methods:

  • elevator.getCurrentFloor() – Returns current floor number (0-based)
  • elevator.goToFloor(floorNum) – Adds floor to destination queue
  • elevator.destinationQueue() – Returns array of queued floor numbers
  • elevator.getCurrentLevel() – Returns current game level
  • elevator.getChallenge() – Returns current challenge object
  • elevator.getStats() – Returns game statistics object

Event Listeners:

  • elevator.on('idle', function() {...}) – Triggered when elevator has no destinations and is not moving
  • elevator.on('stopped_at_floor', function(floorNum) {...}) – Triggered when elevator stops at a floor

Properties (Read-Only):

  • elevator.passengers – Array of current passengers
  • elevator.state – Current state: ‘idle’, ‘moving_up’, ‘moving_down’, ‘doors_open’
  • elevator.direction – Current direction: ‘up’, ‘down’, ‘none’

Floor API

Methods:

  • floor.hasWaitingPeople() – Returns true if people are waiting
  • floor.getWaitingPeople(direction) – Returns array of waiting passengers
    • direction can be ‘up’, ‘down’, or ‘any’

Event Listeners:

  • floor.on('up_button_pressed', function() {...}) – Triggered when up button pressed
  • floor.on('down_button_pressed', function() {...}) – Triggered when down button pressed

Properties:

  • floor.level – Floor number (0-based)
  • floor.waitingPeople – Array of people waiting on this floor

Logging & Statistics

Logging:

log('Your message here'); // Appears in game log

Statistics Object:

{
    transportedThisLevel: 0,      // People successfully transported
    elapsedTimeThisLevel: 0,      // Time in milliseconds
    movesThisLevel: 0,            // Number of elevator movements
    totalWaitTimeThisLevel: 0,    // Sum of all passenger wait times (ms)
    maxWaitTimeThisLevel: 0       // Longest individual wait time (ms)
}

Common Pitfalls & Tips

Pitfalls to Avoid

  1. Ignoring Direction: Not considering elevator direction leads to inefficient back-and-forth movement
  2. Queue Flooding: Adding too many destinations without considering current load
  3. Idle Deadlock: Not handling the idle state properly, causing the elevator to get stuck
  4. Button Spam: Responding to every button press without considering efficiency

Optimization Tips

  1. Batch Pickups: Try to pick up multiple passengers going the same direction
  2. Predictive Queuing: Add destinations for passenger destinations as soon as they board
  3. Smart Idling: When idle, position the elevator strategically (e.g., middle floors)
  4. Emergency Handling: Have fallback logic for when your primary strategy fails

Debugging Strategies

  1. Use Logging Extensively: Log elevator state, decisions, and passenger counts
  2. Monitor Statistics: Watch the stats display to understand performance bottlenecks
  3. Test Edge Cases: What happens with only 1 floor? What about when elevator is full?
  4. Challenge-Specific Logic: Adapt your strategy based on elevator.getChallenge().type

Challenge-Specific Strategies

For Transport X People

  • Focus on basic efficiency
  • Don’t worry about speed, just make sure you pick up everyone
  • Simple closest-floor algorithm works well

For Transport X in Y Time

  • Prioritize speed over everything
  • Minimize stops and direction changes
  • Consider skipping floors with only 1 person if you’re behind schedule

For Average Wait Time

  • Prioritize floors that have been waiting longest
  • Avoid letting any floor wait too long
  • Consider splitting passengers across multiple trips if it reduces wait times

Testing Your Code

  1. Start with Level 1: Make sure basic functionality works
  2. Test Each Challenge Type: Verify your code handles all three challenge variants
  3. Edge Case Testing: Test with full elevator, single passenger, etc.
  4. Performance Monitoring: Watch the statistics to identify bottlenecks
  5. Multi-Level Testing: Let the game progress to test higher difficulty levels

Remember: The game automatically advances levels when you succeed, and restarts the current level if you fail. Use this to iteratively improve your algorithm!