-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess.java
83 lines (81 loc) · 2.41 KB
/
Chess.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
import java.io.*;
import java.nio.file.FileVisitResult;
import java.util.NoSuchElementException;
public class Chess {
//read layout file, add pieces
public static void layOut(String l){
//first clear Board
Board.theBoard().clear();
try {
File file = new File(l);
BufferedReader r = new BufferedReader(new FileReader(file));
String rs;
while((rs = r.readLine()) != null){
if(rs.isEmpty()||rs.charAt(0)=='#'){
continue;
}else{
String[] sa = rs.trim().split("=");
String loc = sa[0];
//may not place two pieces in the same location
Piece p = Piece.createPiece(sa[1]);
Board.theBoard().addPiece(p, loc);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new NoSuchElementException("File not find");
} catch (IOException e) {
e.printStackTrace();
throw new NoSuchElementException("File not find");
} catch (Exception e) {
e.printStackTrace();
}
}
//read moves file, move pieces
public static void playMoves(String m){
try {
File file = new File(m);
BufferedReader r = new BufferedReader(new FileReader(file));
String rs;
while((rs = r.readLine()) != null){
if(rs.isEmpty()||rs.charAt(0)=='#'){
continue;
}else{
String[] sa = rs.trim().split("-");
String from = sa[0];
String to = sa[1];
Board.theBoard().movePiece(from, to);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new NoSuchElementException("File not find");
} catch (IOException e) {
e.printStackTrace();
throw new NoSuchElementException("File not find");
}
}
public static void main(String[] args){
if (args.length != 2) {
System.out.println("Usage: java Chess layout moves");
}
Piece.registerPiece(new KingFactory());
Piece.registerPiece(new QueenFactory());
Piece.registerPiece(new KnightFactory());
Piece.registerPiece(new BishopFactory());
Piece.registerPiece(new RookFactory());
Piece.registerPiece(new PawnFactory());
Board.theBoard().registerListener(new Logger());
// args[0] is the layout file name
// args[1] is the moves file name
// Put your code to read the layout file and moves files
// here.
layOut(args[0]);
System.out.println("Initial board:");
Board.theBoard().iterate(new BoardPrinter());
playMoves(args[1]);
// Leave the following code at the end of the simulation:
System.out.println("Final board:");
Board.theBoard().iterate(new BoardPrinter());
}
}