-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
93 lines (75 loc) · 2.44 KB
/
Test.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
import chess.ChessGame;
import chess.ChessException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test {
// Returns true if the game is valid
public static boolean validateGame(String gameMoves) {
ChessGame game = new ChessGame();
String[] moves = gameMoves.split(" ");
for(String move : moves) {
if(move.equals("0-1") || move.equals("1-0") || move.trim().equals(".")) {
continue;
}
try {
if(move.indexOf(".") != -1) {
move = move.substring(move.indexOf(".")+1);
}
game.makeMove(move);
} catch (ChessException e) {
return false;
} catch(Exception e) {
System.err.println(e);
}
}
return true;
}
public static List<String> getGames(String filename) {
File file = new File(filename);
Scanner reader;
List<String> list = new LinkedList<>();
try {
reader = new Scanner(file);
boolean start = false;
StringBuilder game = new StringBuilder();
while(reader.hasNextLine()) {
String line = reader.nextLine();
if(line.indexOf("1.") != -1) {
start = true;
}
if(line.trim().equals("")) {
start = false;
list.add(game.toString());
game = new StringBuilder();
}
if(start) {
game.append(line.replace("+", ""));
game.append(" ");
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static List<Integer> checkApplication(String fileName) {
List<String> games = getGames(fileName);
List<Integer> list = new LinkedList<>();
int index = 1;
for(String game : games) {
if(!validateGame(game)) {
list.add(index);
}
index++;
}
return list;
}
public static void main(String[] args) {
System.out.println(checkApplication("Korchnoi.txt"));
}
}