浏览代码

Add directions

This commit adds indicators to the elevator. Passes challenges 1-5.
master
父节点
当前提交
dc6add77a5
共有 1 个文件被更改,包括 79 次插入53 次删除
  1. 79
    53
      elevator.js

+ 79
- 53
elevator.js 查看文件

1
-{
1
+eval({
2
 	init: function(elevators, floors) {
2
 	init: function(elevators, floors) {
3
-		console.clear();
4
-
5
 		// Get elevator/building info
3
 		// Get elevator/building info
6
 		let elevatorCount = elevators.length;
4
 		let elevatorCount = elevators.length;
7
 		let floorCount    = floors.length;
5
 		let floorCount    = floors.length;
8
 		let topFloor      = floorCount - 1;
6
 		let topFloor      = floorCount - 1;
9
 		let bottomFloor   = 0;
7
 		let bottomFloor   = 0;
10
 
8
 
9
+		console.clear();
11
 		console.log("Starting (" + floorCount + " floors; " + elevatorCount + " elevator" + ((floorCount == 1) ? "s" : "") +")...");
10
 		console.log("Starting (" + floorCount + " floors; " + elevatorCount + " elevator" + ((floorCount == 1) ? "s" : "") +")...");
12
 
11
 
13
 		// Direction constants
12
 		// Direction constants
15
 		const DOWN = 1;
14
 		const DOWN = 1;
16
 		const IDLE = 2;
15
 		const IDLE = 2;
17
 
16
 
18
-		// Create a data structure for floor requests
17
+		// Data structure for floor requests
19
 		let requests = [new Set(), new Set()];
18
 		let requests = [new Set(), new Set()];
20
 
19
 
21
 		// ----
20
 		// ----
22
 		// Functions
21
 		// Functions
23
 		// ----
22
 		// ----
24
 
23
 
25
-		function getClosestFloor(currentFloor, floorList, ignoredFloors = new Set(), direction = IDLE){
24
+		// getNextFloor chooses the best destination for an elevator
25
+		// when given the index of the elevator, along with all
26
+		// destinations, requests, and rest floors.
27
+		function getNextFloor(elevators, elevatorIndex, destinations, requests, restFloors) {
28
+			let elevator = elevators[elevatorIndex];
29
+			let floorNum = restFloors[1];
30
+
31
+			if (requests[UP].size == 0 && requests[DOWN].size == 0 && destinations.size == 0) {
32
+				// No people; send to rest floor
33
+			} else if (destinations.size == 0) {
34
+				// No destinations; send to pick up passengers
35
+				try {
36
+					floorNum = getClosestFloor(elevator.currentFloor(), new Set([...requests[UP], ...requests[DOWN]]));
37
+				} catch (e) {
38
+					console.log("ERROR: Requests set was empty");
39
+				}
40
+			} else {
41
+				// Prioritize dropping off current passengers
42
+				try {
43
+					floorNum = getClosestFloor(elevator.currentFloor(), destinations);
44
+				} catch (e) {
45
+					console.log("ERROR: Destinations set was empty");
46
+				}
47
+			}
48
+			return floorNum;
49
+		}
50
+
51
+		// getClosestFloor takes a floor, a list of floors, an optional
52
+		// list of ignored floors, and an optional direction. It
53
+		// returns the closest floor from that list in the given
54
+		// direction that is not part of the ignored floors list.
55
+		function getClosestFloor(currentFloor, floorList, ignoredFloors = new Set(), direction = IDLE) {
26
 			let closestFloor = currentFloor;
56
 			let closestFloor = currentFloor;
27
 			let distance     = floorCount;
57
 			let distance     = floorCount;
28
 			let newDistance  = distance;
58
 			let newDistance  = distance;
31
 				switch (direction) {
61
 				switch (direction) {
32
 					case UP:
62
 					case UP:
33
 						newDistance = floorNum - currentFloor;
63
 						newDistance = floorNum - currentFloor;
64
+						break;
34
 					case DOWN:
65
 					case DOWN:
35
 						newDistance = currentFloor - floorNum;
66
 						newDistance = currentFloor - floorNum;
67
+						break;
36
 					case IDLE:
68
 					case IDLE:
37
 						newDistance = Math.abs(floorNum - currentFloor);
69
 						newDistance = Math.abs(floorNum - currentFloor);
38
 				}
70
 				}
40
 					closestFloor = floorNum;
72
 					closestFloor = floorNum;
41
 					distance = newDistance;
73
 					distance = newDistance;
42
 				}
74
 				}
43
-			})
75
+			});
44
 
76
 
45
 			if (closestFloor != currentFloor) {
77
 			if (closestFloor != currentFloor) {
46
 				return closestFloor;
78
 				return closestFloor;
49
 			}
81
 			}
50
 		}
82
 		}
51
 
83
 
52
-		function sendToNext(elevators, elevatorIndex, destinations, requests, restFloors) {
53
-			elevator = elevators[elevatorIndex]
54
-			if (elevator.loadFactor() < 0.8) {
55
-				if (requests[UP].size > 0 || requests[DOWN].size > 0 || destinations.size > 0) {
56
-					let allStops = new Set([...destinations, ...requests[UP], ...requests[DOWN]]);
57
-					try {
58
-						floorNum = getClosestFloor(elevator.currentFloor(), allStops)
59
-						//console.log("> Sending elevator[" + elevatorIndex + "] to floor[" + floorNum + "]");
60
-						elevator.goToFloor(floorNum);
61
-					} catch (e) {
62
-						//console.log("> No closest floor. Sending elevator[" + elevatorIndex + "] to rest floor[" + restFloors[1] + "]");
63
-						elevator.goToFloor(restFloors[1]);
64
-					}
65
-				} else {
66
-					//console.log("> Sending elevator[" + elevatorIndex + "] to rest floor[" + restFloors[1] + "]");
67
-					elevator.goToFloor(restFloors[1]);
68
-				}
69
-			} else {
70
-				try {
71
-					floorNum = getClosestFloor(elevator.currentFloor(), destinations)
72
-					console.log("> Sending elevator[" + elevatorIndex + "] to destination floor[" + floorNum + "]");
73
-					elevator.goToFloor(floorNum);
74
-				} catch (e) {
75
-					//console.log("> No closest floor. Sending elevator[" + elevatorIndex + "] to rest floor[" + restFloors[1] + "]");
76
-					elevator.goToFloor(restFloors[1]);
77
-				}
78
-			}
79
-		}
80
-
84
+		// getClosestElevator takes a floor number and returns the
85
+		// index of the closest elevator.
81
 		function getClosestElevator(floorNum) {
86
 		function getClosestElevator(floorNum) {
82
 			let closestElevator = 0;
87
 			let closestElevator = 0;
83
 			let distance        = floorCount;
88
 			let distance        = floorCount;
95
 			return closestElevator;
100
 			return closestElevator;
96
 		}
101
 		}
97
 
102
 
98
-		function setDirection(elevator, direction){
103
+		// getDirection get the direction (up or down) that an elevator
104
+		// will need to travel to get to a specified floor
105
+		function getDirection(currentFloor, floorNum) {
106
+			let distance = currentFloor - floorNum;
107
+			if (distance > 0 || currentFloor == topFloor) {
108
+				return DOWN;
109
+			} else if (distance < 0 || currentFloor == 0) {
110
+				return UP;
111
+			} else {
112
+				return IDLE;
113
+			}
114
+		}
115
+
116
+		// setDirection changes the indicator lights on a specified
117
+		// elevator to match the given direction.
118
+		function setDirection(elevator, direction) {
99
 			switch (direction) {
119
 			switch (direction) {
100
 				case UP:
120
 				case UP:
101
 					elevator.goingUpIndicator(true);
121
 					elevator.goingUpIndicator(true);
102
 					elevator.goingDownIndicator(false);
122
 					elevator.goingDownIndicator(false);
103
-					break
123
+					break;
104
 				case DOWN:
124
 				case DOWN:
105
 					elevator.goingUpIndicator(false);
125
 					elevator.goingUpIndicator(false);
106
 					elevator.goingDownIndicator(true);
126
 					elevator.goingDownIndicator(true);
107
-					break
127
+					break;
108
 				default:
128
 				default:
109
 					elevator.goingUpIndicator(true);
129
 					elevator.goingUpIndicator(true);
110
 					elevator.goingDownIndicator(true);
130
 					elevator.goingDownIndicator(true);
131
 		// ----
151
 		// ----
132
 
152
 
133
 		elevators.forEach(function (elevator, elevatorIndex) {
153
 		elevators.forEach(function (elevator, elevatorIndex) {
134
-
135
 			// Elevator info and variables
154
 			// Elevator info and variables
136
 			let destinations = new Set();
155
 			let destinations = new Set();
156
+
137
 			const restFloors = [
157
 			const restFloors = [
138
 				elevatorIndex * Math.floor(floorCount / (elevatorCount + 1)),
158
 				elevatorIndex * Math.floor(floorCount / (elevatorCount + 1)),
139
 				(elevatorIndex + 1) * Math.floor(floorCount / (elevatorCount + 1)),
159
 				(elevatorIndex + 1) * Math.floor(floorCount / (elevatorCount + 1)),
140
 				(elevatorIndex + 2) * Math.floor(floorCount / (elevatorCount + 1))
160
 				(elevatorIndex + 2) * Math.floor(floorCount / (elevatorCount + 1))
141
-			]
161
+			];
162
+
163
+			// Next primary stop
164
+			let nextFloor = restFloors[0];
142
 
165
 
143
 			// Start
166
 			// Start
144
-			setDirection(elevator, UP); // Starts on the ground floor
145
-			console.log("> Elevator[" + elevatorIndex + "] rest floors are " + restFloors[0] + ", " + restFloors[1] + ", and " + restFloors[2] + ". Starting at " + restFloors[0] + ".")
146
-			elevator.goToFloor(restFloors[0]); // Nearest rest floor
167
+			setDirection(elevator, UP); // Starts on the ground floor, must be going up
168
+			console.log("> Elevator[" + elevatorIndex + "] rest floors are " + restFloors[0] + ", " + restFloors[1] + ", and " + restFloors[2] + ". Starting at " + restFloors[0] + ".");
169
+			elevator.goToFloor(nextFloor); // Nearest rest floor
147
 
170
 
148
 			// Idle
171
 			// Idle
149
 			elevator.on("idle", function() {
172
 			elevator.on("idle", function() {
150
 				//console.log("> Elevator[" + elevatorIndex + "] is idle")
173
 				//console.log("> Elevator[" + elevatorIndex + "] is idle")
151
-				setDirection(elevator, IDLE);
152
-				if (requests[UP].size == 0 && requests[DOWN].size == 0 && destinations.size == 0) {
153
-					elevator.goToFloor(restFloors[1]);
154
-				} else {
155
-					sendToNext(elevators, elevatorIndex, destinations, requests, restFloors);
156
-				}
174
+				nextFloor = getNextFloor(elevators, elevatorIndex, destinations, requests, restFloors);
175
+				elevator.goToFloor(nextFloor);
157
 			});
176
 			});
158
 
177
 
159
 			// Passing
178
 			// Passing
160
 			elevator.on("passing_floor", function(floorNum, direction) {
179
 			elevator.on("passing_floor", function(floorNum, direction) {
161
-				if (elevator.loadFactor() < 1 && (requests[UP].has(floorNum) || requests[DOWN].has(floorNum))) {
180
+				if (destinations.has(floorNum)
181
+				  || (elevator.loadFactor() < 1 && elevator.destinationDirection() == "up" && requests[UP].has(floorNum))
182
+				  || (elevator.loadFactor() < 1 && elevator.destinationDirection() == "down" && requests[DOWN].has(floorNum))) {
162
 					elevator.goToFloor(floorNum, true);
183
 					elevator.goToFloor(floorNum, true);
163
 				}
184
 				}
164
 			});
185
 			});
172
 			// Stopped
193
 			// Stopped
173
 			elevator.on("stopped_at_floor", function(floorNum) {
194
 			elevator.on("stopped_at_floor", function(floorNum) {
174
 				//console.log("> elevator[" + elevatorIndex + "] stopped at floor[" + floorNum + "]");
195
 				//console.log("> elevator[" + elevatorIndex + "] stopped at floor[" + floorNum + "]");
196
+				setDirection(elevator, getDirection(elevator.currentFloor(), nextFloor));
175
 				destinations.delete(floorNum);
197
 				destinations.delete(floorNum);
176
-				requests[UP].delete(floorNum);
177
-				requests[DOWN].delete(floorNum);
198
+				if (elevator.goingUpIndicator() == true) {
199
+					requests[UP].delete(floorNum);
200
+				}
201
+				if (elevator.goingDownIndicator() == true) {
202
+					requests[DOWN].delete(floorNum);
203
+				}
178
 			});
204
 			});
179
 		});
205
 		});
180
 	},
206
 	},
181
 
207
 
182
 	update: function(dt, elevators, floors) {}
208
 	update: function(dt, elevators, floors) {}
183
-}
209
+});

正在加载...
取消
保存