-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
88 lines (75 loc) · 2.6 KB
/
Main.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
83
84
85
86
87
88
package amazons;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import static amazons.Utils.error;
import ucb.util.CommandArgs;
/** The main class for the Amazons game.
* @author P. N. Hilfinger
*/
public class Main {
/** The main program. ARGS may contain the option --display. */
public static void main(String... args) {
CommandArgs options =
new CommandArgs("--display --log={0,1} --={0,2}", args);
if (!options.ok()) {
System.err.println("Usage: java amazons.Main [--display]"
+ " [--log=FILE] [INPUT [OUTPUT]]");
System.exit(1);
}
List<String> files = options.get("--");
if (!files.isEmpty()) {
try {
System.setIn(new FileInputStream(files.get(0)));
if (files.size() > 1) {
FileOutputStream out = new FileOutputStream(files.get(1));
System.setOut(new PrintStream(out, true));
}
} catch (IOException excp) {
System.err.printf("Could not open file: %s%n",
excp.getMessage());
System.exit(1);
}
}
Controller control = getController(options);
System.out.println("Amazons 61B, version 1.0");
try {
control.play();
System.exit(0);
} catch (IllegalStateException excp) {
System.err.printf("Internal error: %s%n", excp.getMessage());
System.exit(1);
}
}
/** Return an appropriate Controller as indicated by OPTIONS. */
private static Controller getController(CommandArgs options) {
Player manualPlayer;
GUI gui;
PrintStream log;
View view;
Reporter reporter;
if (options.contains("--display")) {
gui = new GUI("Amazons");
reporter = gui;
gui.display(true);
manualPlayer = new GUIPlayer(gui);
view = gui;
} else {
gui = null;
reporter = new TextReporter();
manualPlayer = new TextPlayer();
view = new NullView();
}
log = null;
if (options.contains("--log")) {
try {
log = new PrintStream(options.getFirst("--log"));
} catch (IOException excp) {
throw error("Could not open log file");
}
}
return new Controller(view, log, reporter, manualPlayer, new AI());
}
}