-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (52 loc) · 2.12 KB
/
Main.java
File metadata and controls
64 lines (52 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.dj;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
private static com.dj.Locations locations = new com.dj.Locations();
public static void main(String[] args) {
// Change the program to allow players to type full words, or phrases, then move to the
// correct location based upon their input.
// The player should be able to type commands such as "Go West", "run South", or just "East"
// and the program will move to the appropriate location if there is one. As at present, an
// attempt to move in an invalid direction should print a message and remain in the same place.
//
// Single letter commands (N, W, S, E, Q) should still be available.
Scanner scanner = new Scanner(System.in);
Map<String, String> vocabulary = new HashMap<String, String>();
vocabulary.put("QUIT", "Q");
vocabulary.put("NORTH", "N");
vocabulary.put("SOUTH", "S");
vocabulary.put("WEST", "W");
vocabulary.put("EAST", "E");
int loc = 64;
// int loc = 1;
while(true) {
System.out.println(locations.get(loc).getDescription());
if(loc == 0) {
break;
}
Map<String, Integer> exits = locations.get(loc).getExits();
System.out.print("Available exits are ");
for(String exit: exits.keySet()) {
System.out.print(exit + ", ");
}
System.out.println();
String direction = scanner.nextLine().toUpperCase();
if(direction.length() > 1) {
String[] words = direction.split(" ");
for(String word: words) {
if(vocabulary.containsKey(word)) {
direction = vocabulary.get(word);
break;
}
}
}
if(exits.containsKey(direction)) {
loc = exits.get(direction);
} else {
System.out.println("You cannot go in that direction");
}
}
}
}