本文最后更新于 541 天前,其中的信息可能已经有所发展或是发生改变。
游玩网址
下面的代码均是我自己写的
1到7关:(通过率不是100%,但是多试几次总能通过)
{
init: function(elevators, floors) {
let floor_dest = [];
for (let i = 0; i < floors.length; i++) {
floor_dest.push("stopped");
}
function floor_logic(direction, f, es) {
floor_dest[f.floorNum()] = direction;
}
function floor_func(direction, f, es) {
return function() {floor_logic(direction, f, es)};
}
const es = elevators;
for (let floor of floors) {
floor.on("up_button_pressed", floor_func("up", floor, es));
floor.on("down_button_pressed", floor_func("down", floor, es));
}
for (let elevator of elevators) {
elevator.on("idle", function() {
elevator.goToFloor(0);
});
elevator.on("passing_floor", function(floorNum, direction) {
if (floor_dest[floorNum] == direction && elevator.loadFactor < 1) {
floor_dest[floorNum] = "stopped";
elevator.goToFloor(floorNum, true);
}
});
elevator.on("floor_button_pressed", function(floorNum) {
if (!elevator.destinationQueue.includes(floorNum)) {
elevator.destinationQueue.push(floorNum);
}
const n = elevator.destinationQueue.length;
const a = elevator.destinationQueue;
function dist(x, y) {
return Math.abs(x - y);
}
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const y = elevator.currentFloor();
if (dist(a[i], y) > dist(a[j], y)) {
const tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
elevator.destinationQueue = a;
elevator.checkDestinationQueue();
});
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
8-10关(通过率不是100 %,但总能通过)先来先服务算法
{
init: function(elevators, floors) {
let floor_queue = [];
function floor_logic(direction, f, es) {
floor_queue.push(f.floorNum());
}
function floor_func(direction, f, es) {
return function() {floor_logic(direction, f, es)};
}
const es = elevators;
for (let floor of floors) {
floor.on("up_button_pressed", floor_func("up", floor, es));
floor.on("down_button_pressed", floor_func("down", floor, es));
}
for (let elevator of elevators) {
elevator.on("idle", function() {
if (floor_queue.length > 0) {
elevator.goToFloor(floor_queue.shift());
}
});
elevator.on("floor_button_pressed", function(floorNum) {
if (!elevator.destinationQueue.includes(floorNum)) {
elevator.destinationQueue.push(floorNum);
}
const n = elevator.destinationQueue.length;
const a = elevator.destinationQueue;
function dist(x, y) {
return Math.abs(x - y);
}
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const y = elevator.currentFloor();
if (dist(a[i], y) > dist(a[j], y)) {
const tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
elevator.destinationQueue = a;
elevator.checkDestinationQueue();
});
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}