-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.java
82 lines (62 loc) · 2.34 KB
/
Interpreter.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
*
*/
/** Interpreter is the main class that runs a bork game.
* @author Team Red
*
*/
import java.util.Scanner;
public class Interpreter {
private static GameState state; // not strictly necessary; GameState is
// singleton
public static String USAGE_MSG =
"Usage: Interpreter borkFile.bork|saveFile.sav.";
public static void main(String args[]) {
String command;
Scanner commandLine = new Scanner(System.in);
System.out.println("What adventure file would you like to play?");
System.out.print("> ");
String filename = commandLine.nextLine();
try {
state = GameState.instance();
if (filename.endsWith(".bork")) {
state.initialize(new Dungeon(filename));
System.out.println("\nWelcome to " +
state.getDungeon().getName() + "!");
} else if (filename.endsWith(".sav")) {
state.restore(filename);
System.out.println("\nWelcome back to " +
state.getDungeon().getName() + "!");
} else {
System.err.println(USAGE_MSG);
System.exit(2);
}
System.out.print("\n" +
state.getAdventurersCurrentRoom().describe() + "\n");
command = promptUser(commandLine);
while (!command.equals("q")) {
System.out.print(
CommandFactory.instance().parse(command).execute());
//Determines if adventurer has won or if adventurer is dead and acts accordingly (both end the game)
if(state.getAdventurersScore() == Integer.MAX_VALUE)
{
System.out.println("Congratulations, you have beaten the dungeon!");
break;
}
if(state.getAdventurerIsDead())
{
System.out.println("You have died.");
break;
}
command = promptUser(commandLine);
}
System.out.println("Bye!");
} catch(Exception e) {
e.printStackTrace();
}
}
private static String promptUser(Scanner commandLine) {
System.out.print("> ");
return commandLine.nextLine();
}
}