crunch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.1 KiB

  1. "use strict";
  2. let NORTH = 0;
  3. let NORTH_EAST = 1;
  4. let EAST = 2;
  5. let SOUTH_EAST = 3;
  6. let SOUTH = 4;
  7. let SOUTH_WEST = 5;
  8. let WEST = 6;
  9. let NORTH_WEST = 7;
  10. /*jshint browser: true*/
  11. /*jshint devel: true*/
  12. function Location(name="Nowhere") {
  13. this.name = name;
  14. this.exits = [null,null,null,null,null,null,null,null];
  15. }
  16. function opposite(direction) {
  17. return (direction + 4) % 8;
  18. }
  19. function connectLocations(loc1,loc2,loc1Exit) {
  20. if (loc1.exits[loc1Exit] != null) {
  21. alert(loc1.name + " is already connected to " + loc1.exits[loc1Exit].name);
  22. return;
  23. } else if (loc2.exits[opposite(loc1Exit)] != null) {
  24. alert(loc2.name + " is already connected to " + loc2.exits[opposite(loc1Exit)].name);
  25. return;
  26. } else {
  27. if (loc1Exit >= 0 && loc1Exit <= 7) {
  28. loc1.exits[loc1Exit] = loc2;
  29. loc2.exits[opposite(loc1Exit)] = loc1;
  30. }
  31. }
  32. }
  33. function createWorld() {
  34. let bedroom = new Location("Bedroom");
  35. let bathroom = new Location("Bathroom");
  36. let livingroom = new Location("Living Room");
  37. connectLocations(bedroom,bathroom,EAST);
  38. connectLocations(bedroom,livingroom,NORTH);
  39. return bedroom;
  40. }