forked from dtngochan/JDice_Team3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiceParser.java
More file actions
243 lines (209 loc) · 7.55 KB
/
DiceParser.java
File metadata and controls
243 lines (209 loc) · 7.55 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import java.util.*;
import java.util.logging.*;
/*
JDice: Java Dice Rolling Program
Copyright (C) 2006 Andrew D. Hilton (adhilton@cis.upenn.edu)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
public class DiceParser {
private static final Logger logger = Logger.getLogger(DiceParser.class.getName());
/* this is a helper class to manage the input "stream"*/
private static class StringStream {
StringBuffer buff;
public StringStream(String s) {
buff = new StringBuffer(s);
}
private void munchWhiteSpace() {
int index = 0;
char curr;
while (index < buff.length()) {
curr = buff.charAt(index);
if (!Character.isWhitespace(curr))
break;
index++;
}
buff = buff.delete(0, index);
}
public boolean isEmpty() {
munchWhiteSpace();
return buff.toString().equals("");
}
public Integer getInt() {
return readInt();
}
public Integer readInt() {
int index = 0;
char curr;
munchWhiteSpace();
while (index < buff.length()) {
curr = buff.charAt(index);
if (!Character.isDigit(curr))
break;
index++;
}
try {
Integer ans;
ans = Integer.parseInt(buff.substring(0, index));
buff = buff.delete(0, index);
return ans;
} catch (Exception e) {
return null;
}
}
public Integer readSgnInt() {
munchWhiteSpace();
StringStream state = save();
if (checkAndEat("+")) {
Integer ans = readInt();
if (ans != null)
return ans;
restore(state);
return null;
}
if (checkAndEat("-")) {
Integer ans = readInt();
if (ans != null)
return -ans;
restore(state);
return null;
}
return readInt();
}
public boolean checkAndEat(String s) {
munchWhiteSpace();
if (buff.indexOf(s) == 0) {
buff = buff.delete(0, s.length());
return true;
}
return false;
}
public StringStream save() {
return new StringStream(buff.toString());
}
public void restore(StringStream ss) {
this.buff = new StringBuffer(ss.buff);
}
public String toString() {
return buff.toString();
}
}
/**
* Phân tích cú pháp chuỗi nhập vào thành một danh sách các đối tượng DieRoll.
*
* @param s Chuỗi nhập vào cần phân tích cú pháp
* @return Một vector chứa các đối tượng DieRoll hoặc null nếu không thể phân tích
*/
public static Vector<DieRoll> parseRoll(String s) {
logger.info("Bắt đầu phân tích cú pháp cho chuỗi: " + s); // Log đầu vào
// Kiểm tra tính hợp lệ của chuỗi
if (!isValidDiceInput(s)) {
logger.warning("Chuỗi nhập vào không hợp lệ: " + s);
return null;
}
StringStream ss = new StringStream(s.toLowerCase());
Vector<DieRoll> v = parseRollInner(ss, new Vector<DieRoll>());
if (ss.isEmpty()) {
logger.info("Phân tích thành công với kết quả: " + v);
return v;
} else {
logger.warning("Không thể phân tích chuỗi: " + s);
return null;
}
}
private static Vector<DieRoll> parseRollInner(StringStream ss, Vector<DieRoll> v) {
Vector<DieRoll> r = parseXDice(ss);
if (r == null) {
return null;
}
v.addAll(r);
if (ss.checkAndEat(";")) {
return parseRollInner(ss, v);
}
return v;
}
private static Vector<DieRoll> parseXDice(StringStream ss) {
StringStream saved = ss.save();
Integer x = ss.getInt();
int num = (x == null) ? 1 : x;
if (ss.checkAndEat("x")) {
num = x;
} else {
num = 1;
ss.restore(saved);
}
DieRoll dr = parseDice(ss);
if (dr == null) {
return null;
}
Vector<DieRoll> ans = new Vector<DieRoll>();
for (int i = 0; i < num; i++) {
ans.add(dr);
}
return ans;
}
/**
* Phương thức này phân tích cú pháp cho một viên xúc xắc (bao gồm bonus và dtail).
*
* @param ss Chuỗi đầu vào cần phân tích
* @return Một đối tượng DieRoll chứa thông tin về số lượng xúc xắc, số mặt và bonus
*/
private static DieRoll parseDice(StringStream ss) {
return parseDTail(parseDiceInner(ss), ss);
}
private static DieRoll parseDiceInner(StringStream ss) {
Integer diceCount = ss.getInt(); // Đổi tên biến từ 'num' thành 'diceCount'
int sides = 0;
int rolls = (diceCount == null) ? 1 : diceCount;
if (ss.checkAndEat("d")) {
sides = ss.getInt(); // Kiểm tra nếu có 'd' và lấy số mặt xúc xắc
} else {
return null;
}
// Đọc bonus (nếu có)
int bonus = ss.readSgnInt() != null ? ss.readSgnInt() : 0;
return new DieRoll(rolls, sides, bonus);
}
private static DieRoll parseDTail(DieRoll r1, StringStream ss) {
if (r1 == null)
return null;
if (ss.checkAndEat("&")) {
DieRoll d2 = parseDice(ss);
return parseDTail(new DiceSum(r1, d2), ss);
} else {
return r1;
}
}
private static void test(String s) {
Vector<DieRoll> v = parseRoll(s);
if (v == null) {
logger.warning("Failure: " + s); // Sử dụng logger.warning thay vì System.out.println cho thất bại
} else {
logger.info("Results for " + s + ":"); // Sử dụng logger.info thay vì System.out.println cho kết quả thành công
for (DieRoll dr : v) {
logger.info(dr.toString() + ": " + dr.makeRoll()); // Log kết quả mỗi lần roll xúc xắc
}
}
}
public static void main(String[] args) {
test("d6");
test("2d6");
test("d6+5");
test("4X3d8-5");
test("12d10+5 & 4d6+2");
test("d6 ; 2d4+3");
test("4d6+3 ; 8d12 -15 ; 9d10 & 3d6 & 4d12 +17");
test("4d6 + xyzzy");
test("hi");
test("4d4d4");
}
}