Skip to content

Instantly share code, notes, and snippets.

@buty4649
Created January 31, 2015 05:46
Show Gist options
  • Save buty4649/05d62f927aafdfd931a1 to your computer and use it in GitHub Desktop.
Save buty4649/05d62f927aafdfd931a1 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
var FLOOR_NUM = floors.floorNum;
var elevator1 = elevators[0]; // 初号機
var elevator2 = elevators[1]; // 弐号機
elevator1
.on("idle", function() {
elevator1.goToFloor(0);
});
.on("floor_button_pressed", function(floorNum) {
elevator1.goToFloor(floorNum);
});
elevator2
.on("idle", function() {
elevator2.goToFloor(0);
});
.on("floor_button_pressed", function(floorNum) {
elevator2.goToFloor(floorNum);
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
@buty4649
Copy link
Author

#5

{
    init: function(elevators, floors) {
        var MAX_FLOOR = 5;  // 0階開始だよ

        // エレベータ4機あるし各階に停めておけばいいんじゃね?案
        elevators.forEach(function(e, i){
            e.on("idle", function() {
                e.goToFloor(i%2);
            })
            .on("floor_button_pressed", function(floorNum) {
                e.goToFloor(floorNum);
            });
        });
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}

@buty4649
Copy link
Author

#6

{
    init: function(elevators, floors) {
        var MAX_FLOOR = 3;  // 0階開始だよ

        // エレベータ4機あるし各階に停めておけばいいんじゃね?案
        elevators.forEach(function(e, i){
            e.on("idle", function() {
                e.goToFloor(0);
            })
            .on("floor_button_pressed", function(floorNum) {
                e.goToFloor(floorNum);
            });
        });
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}

@buty4649
Copy link
Author

#7

なんでこれでうまくいくかよくわからん!

{
    init: function(elevators, floors) {
        var MAX_FLOOR = 2;     // 0階開始だよ

        elevators.forEach(function(e, i){
            e.on("idle", function() {
                if(e.currentFloor!=0) e.goToFloor(0);
                else e.goToFloor(MAX_FLOOR);
            })
            .on("floor_button_pressed", function(floorNum) {
                if(e.loadFactor() < 0.5) {
                    e.stop();
                    return;
                }
                e.goToFloor(floorNum);
            });
        });
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}

@buty4649
Copy link
Author

#8

{
    init: function(elevators, floors) {
        var FLOOR_NUM    = 5;  // 0階開始だよ
        var ELEVATOR_NUM = 2;  // エレベータの数だよ

        elevators.forEach(function(e, i){
            e.on("idle", function() {
                e.goToFloor(i * (FLOOR_NUM % ELEVATOR_NUM));
            })
            .on("floor_button_pressed", function(floorNum) {
                e.goToFloor(floorNum);
            });
        });

        floors.forEach(function(f, i) {
            f.on("up_button_pressed", function() {
                elevators[0].goToFloor(i);
            })
            .on("down_button_pressed", function() {
                elevators[1].goToFloor(i);
            });
        });
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}

@buty4649
Copy link
Author

#9

{
    init: function(elevators, floors) {
        var FLOOR_NUM    = 6;  // 0階開始だよ
        var ELEVATOR_NUM = 3;  // エレベータの数だよ

        elevators.forEach(function(e, i){
            e.on("idle", function() {
                e.goToFloor(i * (FLOOR_NUM / ELEVATOR_NUM));
            })
            .on("floor_button_pressed", function(floorNum) {
                e.goToFloor(floorNum);
            });
        });

        floors.forEach(function(f, i) {
            f.on("up_button_pressed", function() {
                if(elevators[1].loadFactor() < 0.4)
                    elevators[1].goToFloor(i);
            })
            .on("down_button_pressed", function() {
                if(elevators[2].loadFactor() < 0.4)
                    elevators[2].goToFloor(i);
            });
        });
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}

@buty4649
Copy link
Author

#10

イイ乱数を引いてクリアしてしまったw
もう少しちゃんと調整しないと安定クリアできないなぁ。

{
    init: function(elevators, floors) {
        var FLOOR_NUM    = 12; // 0階開始だよ
        var ELEVATOR_NUM = 2;  // エレベータの数だよ

        var upperElevator = function() {
            for(n=0; n<ELEVATOR_NUM; n++) {
                if(elevators[n].goingUpIndicator())
                    return elevators[n];
            }
            return elevators[0];
        };

        var downElevator = function() {
            for(n=0; n<ELEVATOR_NUM; n++) {
                if(elevators[n].goingDownIndicator())
                    return elevators[n];
            }
            return elevators[0];
        };

        var WaitFloorFlag = (function() {
            var WaitFloorFlag = function(num) {
                this.flag = [];
            };
            var proto = WaitFloorFlag.prototype;
            proto.setFlag = function(floor) {
                this.flag[floor] = true;
            };
            proto.resetFlag = function(floor) {
                this.flag[floor] = false;
            };
            proto.isSet = function(floor) {
                return this.flag[floor];
            };
            return WaitFloorFlag;
        })();
        var upperFlag = new WaitFloorFlag(FLOOR_NUM);
        var downFlag  = new WaitFloorFlag(FLOOR_NUM);

        elevators[0].goToFloor(0);
        elevators[0].goingUpIndicator(true);
        elevators[0].goingDownIndicator(false);
        elevators[1].goToFloor(FLOOR_NUM);
        elevators[1].goingUpIndicator(true);
        elevators[1].goingDownIndicator(false);

        elevators.forEach(function(e, i){
            e.on("idle", function() {
                if(e.currentFloor() == 0) {
                    e.goingUpIndicator(true);
                    e.goingDownIndicator(false);
                } else if(e.currentFloor() == FLOOR_NUM) {
                    e.goingUpIndicator(false);
                    e.goingDownIndicator(true);
                }

                if(e.goingUpIndicator()) {
                    for(n=e.currentFloor()+1; n <= FLOOR_NUM; n++) {
                        if(upperFlag.isSet(n)) {
                            e.goToFloor(n);
                            return;
                        }
                    }
                    if(e.currentFloor() < FLOOR_NUM) {
                        e.goToFloor(FLOOR_NUM);
                    }
                } else {
                    for(n=e.currentFloor()-1; n >= 0; n--) {
                        if(downFlag.isSet(n)) {
                            e.goToFloor(n);
                            return;
                        }
                    }
                    if(e.currentFloor() > 0) {
                        e.goToFloor(0);
                    }
                }
            })
            .on("floor_button_pressed", function(floorNum) {
                // 0階の時はある程度乗るまで待つ
                if(e.currentFloor() > 0) {
                    e.goToFloor(floorNum);
                } else {
                    if(e.loadFactor() > 0.4) return;
                    e.goToFloor(FLOOR_NUM);
                }
            })
            .on("passing_floor", function(floorNum, direction) {
                var pressed = e.getPressedFloors();
                if(pressed.length > 0) {
                    if(pressed.indexOf(floorNum) >= 0) {
                        e.goToFloor(floorNum, true);
                        return;
                    }
                }
                if(e.loadFactor() > 0.6) return;
                if(direction == "up") {
                    if(upperFlag.isSet(floorNum)) {
                        upperFlag.resetFlag(floorNum);
                        e.goToFloor(floorNum, true);
                    }
                } else {
                    if(downFlag.isSet(floorNum)) {
                        downFlag.resetFlag(floorNum);
                        e.goToFloor(floorNum, true);
                    }
                }
            })
            .on("stopped_at_floor", function(floorNum) {
                if(e.currentFloor() == 0) {
                    e.goingUpIndicator(true);
                    e.goingDownIndicator(false);
                } else if(e.currentFloor() == FLOOR_NUM) {
                    e.goingUpIndicator(false);
                    e.goingDownIndicator(true);
                }

                var queue = e.destinationQueue.sort(
                    e.goingUpIndicator()
                    ? function(a,b){ return a - b; }
                    : function(a,b){ return b - a; }
                ).filter(
                    e.goingUpIndicator()
                    ? function(x, i){ return e.currentFloor() < x; }
                    : function(x, i){ return e.currentFloor() > x; }
                );
                e.destinationQueue = queue;
                e.checkDestinationQueue();
            });
        });

        floors.forEach(function(f, i) {
            f.on("up_button_pressed", function() {
                upperFlag.setFlag(i);
            })
            .on("down_button_pressed", function() {
                downFlag.setFlag(i);
            });
        });
    },
    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