Advertisement
Guest User

Main.java

a guest
Jun 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package com.example.test;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     private static Map<Integer, Location> locations = new HashMap<Integer, Location>();
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         locations.put(0, new Location(0, "You are sitting in front of a computer learning Java"));
  15.         locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building"));
  16.         locations.put(2, new Location(2, "You are at the top of a hill"));
  17.         locations.put(3, new Location(3, "You are inside a building, a well house for a small spring"));
  18.         locations.put(4, new Location(4, "You are in a valley beside a stream"));
  19.         locations.put(5, new Location(5, "You are in the forest"));
  20.  
  21.         locations.get(1).addExit("W", 2);
  22.         locations.get(1).addExit("E", 3);
  23.         locations.get(1).addExit("S", 4);
  24.         locations.get(1).addExit("N", 5);
  25.  
  26.         locations.get(2).addExit("N", 5);
  27.  
  28.         locations.get(3).addExit("W", 1);
  29.  
  30.         locations.get(4).addExit("N", 1);
  31.         locations.get(4).addExit("W", 2);
  32.  
  33.         locations.get(5).addExit("S", 1);
  34.         locations.get(5).addExit("W", 2);
  35.  
  36.  
  37.  
  38.  
  39.         Map<String, String> vocabulary = new HashMap<String, String>();
  40.         vocabulary.put("QUIT", "Q");
  41.         vocabulary.put("NORTH", "N");
  42.         vocabulary.put("SOUTH", "S");
  43.         vocabulary.put("WEST", "W");
  44.         vocabulary.put("EAST", "E");
  45.  
  46.  
  47.  
  48.  
  49.         int loc = 1;
  50.         while(true) {
  51.             System.out.println(locations.get(loc).getDescription());
  52.             if(loc == 0) {
  53.                 break;
  54.             }
  55.  
  56.             Map<String, Integer> exits = locations.get(loc).getExits();
  57.             System.out.print("Available exits are ");
  58.             for(String exit: exits.keySet()) {
  59.                 System.out.print(exit + ", ");
  60.             }
  61.             System.out.println();
  62.  
  63.             String direction = scanner.nextLine().toUpperCase();
  64.             if(direction.length() > 1) {
  65.                 String[] words = direction.split(" ");
  66.                 for(String word: words) {
  67.                     if(vocabulary.containsKey(word)) {
  68.                         direction = vocabulary.get(word);
  69.                         break;
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             if(exits.containsKey(direction)) {
  75.                 loc = exits.get(direction);
  76.             } else {
  77.                 System.out.println("You cannot go in that direction");
  78.             }
  79.  
  80.         }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement