-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
333 lines (299 loc) · 7.44 KB
/
Parser.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package parser;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.NoSuchElementException;
/* KONTROL KODLARI */
/*
* OLAY KODLARI:
*
* -parse() returns
* 10: castling
* 20: save
* 40: resign
* 99: error codes
*
* -parseMenu() returns
* 30: load
* 100: quit
* 101: play
* 102: avail commands
*/
/* PROMOTION KODLARI
* 1: rook
* 2: knight
* 3: bishop
* 4: queen
*/
/* CASTLING KODLARI
* 1: left
* 2: right
*/
public class Parser {
//fieldlar. gereksiz.
private Scanner s;
private String str;
private ArrayList<ArrayList<Integer>> coor = new ArrayList<ArrayList<Integer>>();
private static Parser p;
//constructor iki boyutlu array'i hazýrlýyor
//ve yeni scanner oluþturuyor.
private Parser(){
coor.add(new ArrayList<Integer>());
coor.add(new ArrayList<Integer>());
s = new Scanner(System.in);
}
//promotion komutunu parse eder
//bir piyonun tahtanýn diðer ucuna gitmesiyle tetikle
public ArrayList<ArrayList<Integer>> parseProm() {
resetParser(coor);
System.out.print("promotion command: ");
try {
str = s.nextLine();
} catch (NoSuchElementException e) {
System.err.println("EOF");
System.exit(-1);
}
if(str.substring(0, 4).equals("prom") &&
Character.isWhitespace(str.charAt(4))) {
switch(str.substring(5, str.length()).toLowerCase()){
case "rook":
coor.get(0).add(1);
break;
case "knight":
coor.get(0).add(2);
break;
case "bishop":
coor.get(0).add(3);
break;
case "queen":
coor.get(0).add(4);
break;
default:
System.err.println("invalid input - \"prom @ChessPiece\"");
resetParser(coor);
coor.get(0).add(99);
}
} else {
System.err.println("invalid input - \"prom @ChessPiece\"");
resetParser(coor);
coor.get(0).add(99);
}
return coor;
}
//parse the initial menu
public ArrayList<ArrayList<Integer>> parseMenu() {
resetParser(coor);
System.out.print("command: ");
try {
str = s.nextLine();
} catch (NoSuchElementException e) {
System.err.println("EOF");
System.exit(-1);
}
//parse initial menu options
if(str.length() == 1 && Character.isDigit(str.charAt(0))){
int i = Integer.parseInt(str);
switch(i){
case 1:
coor.get(0).add(101);
break;
case 2:
coor.get(0).add(102);
break;
case 3:
coor.get(0).add(30);
return coor;
case 4:
coor.get(0).add(100);
break;
default:
System.err.println("invalid input - select 1 to 3");
resetParser(coor);
coor.get(0).add(99);
break;
}
return coor;
}
System.err.println("invalid input - enter a number of length 1");
resetParser(coor);
coor.get(0).add(99);
return coor;
}
//giriþleri parse eden method.
//iki boyutlu arrayde gidiþ ve dönüþ koordinatlarý dönderir
public ArrayList<ArrayList<Integer>> parse(){
resetParser(coor);
System.out.print("command: ");
try {
str = s.nextLine();
} catch (NoSuchElementException e) {
System.err.println("EOF");
System.exit(-1);
}
//giriþin tam olarak NUMARA-HARF-BOÞLUK-NUMARA-HARF þeklinde
//olmasý için uzunluk ve tür kontrolleri
if(str.length() < 4){
System.err.println("invalid input - move string length min 5");
resetParser(coor);
coor.get(0).add(99);
return coor;
}
//pes etme komutunu parse eder
//olay kodu 30.
if(str.equalsIgnoreCase("resign")){
coor.get(0).add(40);
return coor;
}
else if(str.equalsIgnoreCase("save")){
coor.get(0).add(20);
return coor;
}
//rok komutunu parse eder
//olay kodu 10.
//TODO: short O-O, long O-O-O
else if(str.substring(0, 4).equalsIgnoreCase("cast") &&
Character.isWhitespace(str.charAt(4)) &&
Character.isAlphabetic(str.charAt(5)) ){
coor.get(0).add(10);
switch(str.charAt(5)){
case 'L':
case 'l':
coor.get(1).add(1);
break;
case 'R':
case 'r':
coor.get(1).add(2);
break;
default:
System.err.println("invalid input - \"cast (L)\" or \"cast (R)\"");
resetParser(coor);
coor.get(0).add(99);
}
return coor;
}
else if(Character.isAlphabetic(str.charAt(0)) &&
Character.isDigit(str.charAt(1)) &&
Character.isWhitespace(str.charAt(2)) &&
Character.isAlphabetic(str.charAt(3)) &&
Character.isDigit(str.charAt(4)) ){
//switch ifadeleri harflerin iki boyutlu array'de karþýlýk gelen
//rakamlara çevrilmesi için yazýldý. SOURCE için
//(8-..) çünkü oyun tahtasý sol üstte 8 ve A'dan baþlýyor
coor.get(0).add(8 - Integer.parseInt(str.substring(1, 2)));
coor.get(1).add(8 - Integer.parseInt(str.substring(4, 5)));
/*FROM*/
switch(str.charAt(0)){
case 'A':
case 'a':
coor.get(0).add(0);;
break;
case 'B':
case 'b':
coor.get(0).add(1);;
break;
case 'C':
case 'c':
coor.get(0).add(2);
break;
case 'D':
case 'd':
coor.get(0).add(3);
break;
case 'E':
case 'e':
coor.get(0).add(4);
break;
case 'F':
case 'f':
coor.get(0).add(5);
break;
case 'G':
case 'g':
coor.get(0).add(6);
break;
case 'H':
case 'h':
coor.get(0).add(7);
break;
default:
System.err.println("invalid input in SOURCE - valid chars: A to H");
resetParser(coor);
coor.get(0).add(99);
}
/***********************************************************/
//ayný switch ifadesi DESTINATION için
/*TO*/
switch(str.charAt(3)){
case 'A':
case 'a':
coor.get(1).add(0);
break;
case 'B':
case 'b':
coor.get(1).add(1);
break;
case 'C':
case 'c':
coor.get(1).add(2);
break;
case 'D':
case 'd':
coor.get(1).add(3);
break;
case 'E':
case 'e':
coor.get(1).add(4);
break;
case 'F':
case 'f':
coor.get(1).add(5);
break;
case 'G':
case 'g':
coor.get(1).add(6);
break;
case 'H':
case 'h':
coor.get(1).add(7);
break;
default:
System.err.println("invalid input in DESTINATION - valid chars: A to H");
resetParser(coor);
coor.get(0).add(99);
break;
}
if( (coor.get(0).get(0) < 0 || coor.get(0).get(0) > 7) ||
(coor.get(1).get(0) < 0 || coor.get(1).get(0) > 7) ){
System.err.println("invalid input - numbers cannot exceed 8 or go below 0");
resetParser(coor);
coor.get(0).add(99);
}
else if(coor.get(0).size() == 2 &&
coor.get(0).get(0) == coor.get(1).get(0) &&
coor.get(0).get(1) == coor.get(1).get(1) ){
System.err.println("invalid input - you cannot enter same coordinates");
resetParser(coor);
coor.get(0).add(99);
}
} else {
System.err.println("invalid input - you can move a piece (C#_C#),\nmake a promotion (prom @ChessPiece),\nor castling cast (L) or (cast (R)),\nor (resign)");
resetParser(coor);
coor.get(0).add(99);
}
return coor;
}
//tekrar parse yapýlmasý için array'i temizleyen method
public void resetParser(ArrayList<ArrayList<Integer>> coor){
coor.get(0).clear();
coor.get(1).clear();
}
//sadece bi tane parser objesi oluþturmak için
//getInstance() methodu
public static Parser getInstance(){
if(p == null)
p = new Parser();
return p;
}
public void close(){
s.close();
}
} //Parser class'ýnýn bitiþi.