-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
152 lines (125 loc) · 4.65 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Skeleton code copyright (C) 2008, 2022 Paul N. Hilfinger and the
* Regents of the University of California. Do not distribute this or any
* derivative work without permission. */
package ataxx;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import static ataxx.Utils.*;
import ucb.util.CommandArgs;
/** The main program for Ataxx.
* @author P. N. Hilfinger */
public class Main {
/** Location of usage message resource. */
static final String USAGE = "ataxx/Usage.txt";
/** Run Ataxx game. Options (in ARGS0):
* --display: Use GUI.
* --timing: Display think times for AI.
* --version: Print version number and exit.
* --log: Print commands.
* --strict: Strict mode---players errors cause error exit.
* --debug: Set level of debugging information.
* Trailing arguments are input files; the standard input is the
* default.
*/
public static void main(String[] args0) {
CommandArgs args =
new CommandArgs("--display{0,1} --strict --version --timing --log"
+ " --debug=(\\d+){0,1} --=(.*){0,}", args0);
System.out.println("CS61B Ataxx! Version 3.0");
if (!args.ok()) {
usage();
return;
}
if (args.contains("--version")) {
System.err.printf("Version %s%n", Defaults.VERSION);
System.exit(0);
}
_strict = args.contains("--strict");
boolean log = args.contains("--log");
if (args.contains("--debug")) {
Utils.setMessageLevel(args.getInt("--debug"));
}
Game game;
if (args.contains("--display")) {
GUI display = new GUI("Ataxx!");
game = new Game(display, display, display, log);
display.pack();
display.setVisible(true);
} else {
TextSource source;
ArrayList<Reader> inReaders = new ArrayList<>();
if (args.get("--").isEmpty()) {
inReaders.add(new InputStreamReader(System.in));
} else {
for (String name : args.get("--")) {
if (name.equals("-")) {
inReaders.add(new InputStreamReader(System.in));
} else {
try {
inReaders.add(new FileReader(name));
} catch (IOException excp) {
System.err.printf("Could not open %s", name);
System.exit(1);
}
}
}
}
game = new Game(new TextSource(inReaders),
(b) -> { }, new TextReporter(), log);
}
System.exit(game.play());
}
/** Print usage message. */
private static void usage() {
printHelpResource(USAGE, System.err);
}
/* STRICT MODE */
/** Return true iff --strict flag supplied. */
static boolean isStrict() {
return _strict;
}
/* TIMING */
/** Start timing an operation. */
static void startTiming() {
if (_timing) {
_startTime = System.currentTimeMillis();
}
}
/** End the timing started with the last call to startTiming().
* Report result if we are timing. */
static void endTiming() {
if (_timing) {
long time = System.currentTimeMillis() - _startTime;
System.err.printf("[%d msec]%n", time);
_maxTime = Math.max(_maxTime, time);
_totalTime += time;
_numTimedOps += 1;
}
}
/** Report total time statistics, if timing. */
static void reportTotalTimes() {
if (_timing && _numTimedOps > 0) {
System.err.printf("[Total time: %d msec for %d operations. "
+ "Avg: %d msec/operation. "
+ "Max: %d msec]%n", _totalTime,
_numTimedOps, _totalTime / _numTimedOps,
_maxTime);
}
}
/** True iff AIs should time. */
private static boolean _timing;
/** True iff using strict mode (in which errors detected by the
* manual player terminate the program with an error code of 2. */
private static boolean _strict;
/** Accumulated time. */
private static long _totalTime;
/** Last start time. */
private static long _startTime;
/** Number of operations timed. */
private static int _numTimedOps;
/** Maximum operation time. */
private static long _maxTime;
}