RandomSwitchesAlgorithm.java 760 B

12345678910111213141516171819202122232425262728293031323334
  1. package exampleAlgorithms;
  2. import api.CpsAlgorithm;
  3. import ui.controller.Control;
  4. import ui.model.Model;
  5. import classes.*;
  6. public class RandomSwitchesAlgorithm implements CpsAlgorithm {
  7. @Override
  8. public void runAlgorithm(Model model, Control controller) {
  9. randomSwitches(model);
  10. }
  11. /**
  12. * Sets every Switch to a random state.
  13. *
  14. * @param model
  15. * the Model
  16. */
  17. private void randomSwitches(Model model) {
  18. for (HolonSwitch s : model.getSwitches()) {
  19. // Set to Manual Mode
  20. s.setManualMode(true);
  21. // Generate a random number between 0 and 1
  22. double randomDouble = Math.random();
  23. if (randomDouble < 0.5) {
  24. s.setManualState(true);
  25. } else {
  26. s.setManualState(false);
  27. }
  28. }
  29. }
  30. }