Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- class Main {
- static Pattern movePattern = Pattern.compile("move (.*)");
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Room roomA = new Room("Ryan's Preferred Room", "With Smash Bros & .io's for days.");
- Room roomB = new Room("Room B", "A barren room......");
- roomA.addExit(new Exit(Exit.Direction.NORTH, roomB));
- roomB.addExit(new Exit(Exit.Direction.SOUTH, roomA));
- // print(roomA.toString());
- // print(roomB.toString());
- Room currentRoom = roomA;
- boolean quit = false;
- while (!quit) {
- printRoom(currentRoom);
- print("==================================");
- print("What do you wanna do?");
- String command = scanner.nextLine();
- print("");
- Matcher moveMatcher = movePattern.matcher(command);
- if (moveMatcher.matches()) {
- String userDirection = moveMatcher.group(1);
- if (isValidDirection(userDirection)) {
- Exit.Direction direction = Exit.Direction.findDirection(userDirection.toUpperCase());
- Room destination = currentRoom.getRoomInDirection(direction);
- currentRoom = destination;
- } else {
- print(userDirection + " is not a valid direction.");
- }
- } else {
- print("Command not recognized.");
- print("");
- }
- }
- }
- static boolean isValidDirection(String direction) {
- if (direction.equalsIgnoreCase("N") || direction.equalsIgnoreCase("E") || direction.equalsIgnoreCase("S") || direction.equalsIgnoreCase("W")) {
- return true;
- } else {
- return false;
- }
- }
- static void printRoom(Room room) {
- print("");
- print("");
- print("");
- for (int i = 0; i < room.name.length(); i++) {
- System.out.print("=");
- }
- print("");
- print(room.name);
- for (int i = 0; i < room.name.length(); i++) {
- System.out.print("=");
- }
- print("");
- print(room.description);
- print("");
- print("Exits:");
- for (Exit exit : room.exits) {
- print(" " + exit.toString());
- }
- }
- static void print(String output) {
- System.out.println(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement