package ch.epfl.maze.physical.stragegies.picker; import ch.epfl.maze.util.Direction; import java.util.Set; /** * A decision maker cycling in each Direction starting from a given one. * * @author Pacien TRAN-GIRARD */ public interface CyclePicker extends BlindPicker { /** * Returns the cycle starting Direction. * * @return The starting Direction */ Direction getStartingDirection(); /** * Returns the Rotation Direction. * * @return The Rotation Direction */ Direction getRotationDirection(); @Override default Direction pick(Set choices) { if (choices.isEmpty()) return FALLBACK_DIRECTION; if (this.getStartingDirection() == Direction.NONE) return FALLBACK_DIRECTION; if (this.getRotationDirection() == Direction.NONE) return FALLBACK_DIRECTION; Direction dir = this.getStartingDirection(); while (!choices.contains(dir)) dir = dir.unRelativeDirection(this.getRotationDirection()); return dir; } /** * Counts the number of rotations to rotates to the given Direction * * @param direction The Direction * @return The number of rotations */ default int countRotations(Direction direction) { if (direction == Direction.NONE) return 0; if (this.getStartingDirection() == Direction.NONE) return 0; if (this.getRotationDirection() == Direction.NONE) return 0; Direction dir = this.getStartingDirection(); int counter = 0; while (dir != direction) { dir = dir.unRelativeDirection(this.getRotationDirection()); counter += 1; } return counter; } }