Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Pattern;
  3. import java.util.regex.Matcher;
  4.  
  5. class Main {
  6.  
  7. static Pattern movePattern = Pattern.compile("move (.*)");
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. Room roomA = new Room("Ryan's Preferred Room", "With Smash Bros & .io's for days.");
  13. Room roomB = new Room("Room B", "A barren room......");
  14.  
  15. roomA.addExit(new Exit(Exit.Direction.NORTH, roomB));
  16. roomB.addExit(new Exit(Exit.Direction.SOUTH, roomA));
  17.  
  18. // print(roomA.toString());
  19. // print(roomB.toString());
  20. Room currentRoom = roomA;
  21.  
  22. boolean quit = false;
  23. while (!quit) {
  24. printRoom(currentRoom);
  25. print("==================================");
  26. print("What do you wanna do?");
  27.  
  28. String command = scanner.nextLine();
  29. print("");
  30.  
  31. Matcher moveMatcher = movePattern.matcher(command);
  32.  
  33. if (moveMatcher.matches()) {
  34. String userDirection = moveMatcher.group(1);
  35. if (isValidDirection(userDirection)) {
  36. Exit.Direction direction = Exit.Direction.findDirection(userDirection.toUpperCase());
  37. Room destination = currentRoom.getRoomInDirection(direction);
  38. currentRoom = destination;
  39. } else {
  40. print(userDirection + " is not a valid direction.");
  41. }
  42. } else {
  43. print("Command not recognized.");
  44. print("");
  45. }
  46. }
  47. }
  48.  
  49. static boolean isValidDirection(String direction) {
  50. if (direction.equalsIgnoreCase("N") || direction.equalsIgnoreCase("E") || direction.equalsIgnoreCase("S") || direction.equalsIgnoreCase("W")) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56.  
  57. static void printRoom(Room room) {
  58. print("");
  59. print("");
  60. print("");
  61. for (int i = 0; i < room.name.length(); i++) {
  62. System.out.print("=");
  63. }
  64. print("");
  65. print(room.name);
  66. for (int i = 0; i < room.name.length(); i++) {
  67. System.out.print("=");
  68. }
  69. print("");
  70. print(room.description);
  71. print("");
  72. print("Exits:");
  73. for (Exit exit : room.exits) {
  74. print(" " + exit.toString());
  75. }
  76. }
  77.  
  78. static void print(String output) {
  79. System.out.println(output);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement