elevator saga的游戏代码
本文最后更新于 541 天前,其中的信息可能已经有所发展或是发生改变。

游玩网址

https://play.elevatorsaga.com

下面的代码均是我自己写的

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
        }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇