Browse Source

Randomize next floor

master
Ashton Charbonneau 3 years ago
parent
commit
eaa245ba55
1 changed files with 5 additions and 4 deletions
  1. 5
    4
      elevator.js

+ 5
- 4
elevator.js View File

31
 		// returns the closest floor from that list in the given
31
 		// returns the closest floor from that list in the given
32
 		// direction that is not part of the ignored floors list. Ties
32
 		// direction that is not part of the ignored floors list. Ties
33
 		// are broken by even/oddness of the current floor.
33
 		// are broken by even/oddness of the current floor.
34
+		// TODO: fix direction shadowing/usage
34
 		function getClosestFloor(currentFloor, floorList, ignoredFloors = new Set(), direction = IDLE) {
35
 		function getClosestFloor(currentFloor, floorList, ignoredFloors = new Set(), direction = IDLE) {
35
 			const isEven = (currentFloor % 2 == 0);
36
 			const isEven = (currentFloor % 2 == 0);
36
 
37
 
46
 					closestFloor = floorNum;
47
 					closestFloor = floorNum;
47
 					distance = newDistance;
48
 					distance = newDistance;
48
 				} else if (newDistance == distance && !(ignoredFloors.has(floorNum))) {
49
 				} else if (newDistance == distance && !(ignoredFloors.has(floorNum))) {
49
-					if (isEven && direction == UP) {
50
+					if ((isEven && direction == UP) ||
51
+					    (!isEven && direction == DOWN) ||
52
+					    (direction == IDLE && Math.random() < 0.5)) {
50
 						closestFloor = floorNum;
53
 						closestFloor = floorNum;
51
-					} else if (!isEven && direction == DOWN) {
52
-						closestFloor = floorNum;
53
-					}
54
+						}
54
 				}
55
 				}
55
 			});
56
 			});
56
 
57
 

Loading…
Cancel
Save