munch
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

50 lines
1.2 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.description = "Not much of anything, really.";
  15. this.exits = [null,null,null,null,null,null,null,null];
  16. }
  17. function opposite(direction) {
  18. return (direction + 4) % 8;
  19. }
  20. function connectLocations(loc1,loc2,loc1Exit) {
  21. if (loc1.exits[loc1Exit] != null) {
  22. alert(loc1.name + " is already connected to " + loc1.exits[loc1Exit].name);
  23. return;
  24. } else if (loc2.exits[opposite(loc1Exit)] != null) {
  25. alert(loc2.name + " is already connected to " + loc2.exits[opposite(loc1Exit)].name);
  26. return;
  27. } else {
  28. if (loc1Exit >= 0 && loc1Exit <= 7) {
  29. loc1.exits[loc1Exit] = loc2;
  30. loc2.exits[opposite(loc1Exit)] = loc1;
  31. }
  32. }
  33. }
  34. function createWorld() {
  35. let bedroom = new Location("Bedroom");
  36. let bathroom = new Location("Bathroom");
  37. let livingroom = new Location("Living Room");
  38. connectLocations(bedroom,bathroom,EAST);
  39. connectLocations(bedroom,livingroom,NORTH);
  40. return bedroom;
  41. }